Skip to content

Python

The Python rules report style choices that no formatter or type checker makes, and they run on every file, whatever it imports. The python skill in skills/ holds the rest of the style and the read-only Python review workflow.

Rules Safe fix Unsafe fix Manual fix
50 0 15 35
Group Rules
Names module-private-name, vague-module-name, generic-verb-name, import-alias, wildcard-import
Search error-message-prefix, deprecated-without-decorator, interpolated-key, hasattr-probe
Shims and aliases pass-through-function, symbol-alias, getattr-shim, parameter-alias, defensive-type-coercion, import-fallback, dual-key-fallback, fallback-on-exception
Modules import-side-effect, module-too-large
Types typing-cast, dict-str-any, id-without-newtype, record-without-namedtuple, json-without-type, same-type-conversion, float-around-number
Functions multi-line-call-argument, repeated-block, function-too-large, look-alike-definition
Tests mock-import, test-skip, test-rerun, test-sleep, test-suppressed-health-check
Comprehensions comprehension-merges-dicts, unpacked-comprehension, comprehension-two-fors, comprehension-multi-line, comprehension-among-arguments
Standard library setdefault-without-defaultdict, counting-loop-without-counter, strptime-without-fromisoformat, elapsed-without-perf-counter, toml-without-tomllib
Speed and memory front-insert-in-loop, orjson-loads-decoded, slotted-base-without-slots

Name each module after what it holds, leave the underscore off module level names, give each verb its object, and import each symbol under its real name.

Rule Severity Fix What it finds
module-private-name warning none A module level name that starts with an underscore hides code that other modules may need.
vague-module-name warning none A module named utils, helpers, common, config or types does not say what it holds.
generic-verb-name warning none A public function named with a bare verb, such as load or clean, does not say what it acts on, so a search for the name lands on every module that loads or cleans something else.
import-alias warning none Renaming a symbol or module on import gives one definition two names across files, so a plain-text search for the alias never lands on its definition.
wildcard-import warning none A wildcard import (from module import *) injects every public name from another module into the current namespace, so plain-text search cannot tell where a symbol came from and new exports can silently shadow local names.

Put the words a search uses where the search lands, so a message from a log or a deprecated name leads to its source in one search.

Rule Severity Fix What it finds
error-message-prefix warning none An error message that starts with a value has no fixed words at its start, so a line copied from a log does not search back to the raise.
deprecated-without-decorator warning unsafe A function that calls warnings.warn(..., DeprecationWarning) in its body warns only when it runs.
interpolated-key warning none A name built from parts with an f-string, such as an event name or a module path, is in no source file as a whole, so a search for the name seen in a log or a config finds nothing.
hasattr-probe warning none Probing objects at runtime with hasattr(obj, "name") hides attribute contracts behind string duck-typing and keeps legacy attribute fallbacks alive where static search and type checkers cannot verify them.

Keep one name, one import path, and one key per concept. Delete pass-through wrappers, symbol aliases, dynamic __getattr__ shims, parameter aliases, fallback imports, dual-key lookups, and silent fallback handlers instead of leaving compatibility shims behind.

Rule Severity Fix What it finds
pass-through-function warning none A function or method that only passes its parameters to one call adds a name without adding behavior.
symbol-alias warning none Aliasing a class, function, type, or public attribute under a second name leaves two names for one thing in the codebase.
getattr-shim warning none A module-level getattr hook or a class getattr that delegates straight to an inner attribute hides real definitions behind runtime dispatch, breaking static search and type checking.
parameter-alias warning none Accepting two parameter names for the same input and falling back from one to the other keeps a legacy parameter alias alive in your API.
defensive-type-coercion warning none Coercing a parameter in place with if isinstance(param, ...): param = ... lets two representations of the same concept spread through internal functions.
import-fallback warning none A try/except ImportError block that falls back to a second import or sets a module to None keeps a dead compatibility shim alive when you control the runtime environment.
dual-key-fallback warning none Chaining .get() or getattr() across two different keys on the same object keeps a legacy field alias alive inside application logic, and or silently discards legitimate falsy values such as 0, False, or "".
fallback-on-exception warning none Catching a broad Exception without binding, logging, or re-raising it and calling a second implementation silently hides bugs behind a legacy fallback path.

Keep calls and environment reads out of the top level of a module so an import runs no work, and keep module statement counts under the size ratchet.

Rule Severity Fix What it finds
import-side-effect warning none A call at module level runs whenever the module is imported.
module-too-large warning none Modules that grow past the fixed 300-statement limit accumulate duplicated pipelines and dead code unnoticed.

State the real type in a signature, so the type checker can check the keys and the values the function uses.

Rule Severity Fix What it finds
typing-cast warning none cast() overrides the type checker without any check at run time.
dict-str-any warning none dict[str, Any] in a signature hides which keys and values the function works with.
id-without-newtype warning none A function that takes two or more IDs as plain str or int lets a caller swap them without a type error.
record-without-namedtuple warning none A frozen dataclass or attrs class that only holds fields can be a NamedTuple, which is cheaper to define and build.
json-without-type warning none Returning the result of json.loads gives the caller Any, so nothing names the shape of the data.
same-type-conversion warning unsafe float(), int(), str() or bool() around a value that already has that type does nothing.
float-around-number warning unsafe A number from a float64 array or a Series is a float already, and so is arithmetic on it.

Keep multi-line expressions out of call argument lists so each step has a name, collapse repeated statement groups into a loop over the varying part, and keep function statement counts under the size ratchet.

Rule Severity Fix What it finds
multi-line-call-argument warning unsafe A call that runs over several lines inside another call's arguments is hard to read.
repeated-block warning none Three or more structurally identical groups of at least two statements in one function that differ in one name part repeat the same logic by copy-paste.
function-too-large warning none Functions that grow past the fixed 50-statement limit accumulate copy-pasted branches and temporary variables that hide the data flow.
look-alike-definition warning none Two or more functions or classes in the same module that share most of their structure after setting aside variable names and literal values duplicate the same logic.

Test with real objects and let every test run once, because a mock, a skip, a rerun or a sleep lets a test pass while the code is wrong.

Rule Severity Fix What it finds
mock-import warning none A test with a mock checks the mock and not the real code.
test-skip warning none A skipped test never runs, so it proves nothing.
test-rerun warning none Rerunning a test until it passes hides why it fails some of the time.
test-sleep warning none A test that waits a fixed time is slow and still fails when the wait is too short.
test-suppressed-health-check warning none A property test that turns off a Hypothesis check of correctness can pass while it checks the wrong thing.

Write a loop when a comprehension merges dicts, unpacks, holds two for clauses or runs over several lines, and give a comprehension a name before you pass it with other arguments, because both are easier to read.

Rule Severity Fix What it finds
comprehension-merges-dicts warning unsafe A comprehension that merges dicts with ** for every item builds a new dict for each item and is hard to read.
unpacked-comprehension warning unsafe A dict literal that unpacks a comprehension with ** is hard to read.
comprehension-two-fors warning unsafe A comprehension with more than one for is hard to read.
comprehension-multi-line warning unsafe A comprehension that runs over more than one line is hard to read.
comprehension-among-arguments warning unsafe A comprehension that is one of several arguments of a call makes the call hard to read.

Use what the standard library already has before you write it by hand.

Rule Severity Fix What it finds
setdefault-without-defaultdict warning none setdefault with an empty list, set or dict builds a new empty value on every call.
counting-loop-without-counter warning unsafe A loop whose only job is to count items with a dict does what collections.Counter does in one call.
strptime-without-fromisoformat warning unsafe strptime with an ISO 8601 format does what fromisoformat does without a format.
elapsed-without-perf-counter warning none Measuring a duration with time.time() uses the system clock, which can be set back.
toml-without-tomllib warning unsafe The standard library reads TOML since Python 3.11.

Leave out work that the measured alternative skips, when the code can always take the alternative.

Rule Severity Fix What it finds
front-insert-in-loop warning unsafe Each insert(0, x) moves every item already in the list, so a loop of them gets slow.
orjson-loads-decoded warning unsafe orjson reads UTF-8 bytes directly, so decoding them to a str first only adds work.
slotted-base-without-slots warning unsafe A subclass without slots gives its instances a dict again, so the memory that the base class saved is lost.

Projects can keep citations in References sections and reserve source words for prose.

Rule Severity Fix What it finds
citation-outside-references warning none Move each author and year citation or et al. in a Python comment or docstring to a numpydoc References section.
source-derived-name warning none Name a declaration or a string option in Literal[...] after its purpose.