Rules¶
heki has 54 rules in four families. A rule is exact when it reports only what it established and was certified on the corpus, and heuristic (โ) when a report can be wrong about the code: Rule Precision says how a rule is certified and what each one measured. Each rule's page says what a report states.
- The exact rules run by default, and for a prefix such as
EFForALL. A report of an exact rule that did not establish every premise (anOPTreport whose rewrite is not proven) is shown where a heuristic rule would run. - A heuristic rule runs when
select/extend-selectnames its code or name. Withlint.heuristic = true(--heuristic), the heuristic rules that are not opt-in also run by default and for a prefix orALL. - The opt-in rules marked ๐ run only when
select/extend-selectnames their exact code or name. - The contract rules marked ๐ run when their contract is declared, whatever their certainty.
Legend¶
- ๐ ๏ธ The fix is applied by
heki check --fixwhen heki proves it safe. - ๐ง A proven-safe rewrite applied only with
--fix --include-size-dependent(it can be slower for tiny inputs). - โ Heuristic rule: a report can be wrong about the code. Every output labels its reports
heuristic. - ๐ Opt-in rule.
- ๐ Contract rule: it reports only what a declared contract asks for.
- ๐งช Experimental rule. Experimental features can still change in behavior or precision, or be removed, in any release, and each change is listed in the release notes. An opt-in rule that is not experimental is stable and only off by default. See Experimental Features.
Mutation & State (MUT)¶
Shared state written behind its owner's back: stale caches, write-ownership contracts, shared return values, and effect regressions against a saved snapshot.
| Code | Name | Message | |
|---|---|---|---|
| MUT101 | cached-property-stale-mutation | Mutating an attribute read by @cached_property without invalidating the cache. |
๐ ๐งช |
| MUT103 | summary-effect-regression | Interprocedural mutation/escape/effect regression against saved summary snapshot. | โ ๐ |
| MUT106 | iterator-double-consumption | Opt-in: consuming a single-pass iterator or generator a second time (the second pass sees no items). | โ ๐ |
| MUT107 | hash-attr-mutation | Opt-in: a method other than the initializers rebinds or changes in place an attribute that the __hash__ of its instances reads. |
โ ๐ |
| MUT108 | readonly-type-mutation | Opt-in: changing in place a field that a standard type declares read-only (frozen dataclass / NamedTuple / dataclass_transform fields, Final / ReadOnly attributes, ReadOnly TypedDict keys), directly, through an alias, or in a helper. |
โ ๐ |
| MUT110 | readonly-param-mutation | Handing a Sequence / Mapping / AbstractSet / ReadOnly parameter, as itself, to a function whose body (or a helper's) changes it in place at a named line. |
|
| MUT111 | cached-property-stale-read | Opt-in: reading a @cached_property that a method of its class family can leave stale (a write MUT101 reports). |
๐ ๐งช |
| MUT112 | conditional-ownership-mutation | Opt-in: changing in place a value that is a new object on one path and borrowed state (a parameter, an attribute, a global, or what a function returns of such state) on another. | โ ๐ ๐งช |
| MUT113 | iterated-container-mutation | Opt-in: a dict, set, or deque of the instance changed while a method iterates it, by another method while a generator is suspended in the loop or by a function the loop's body calls (the next step raises RuntimeError). |
โ ๐ ๐งช |
| MUT114 | cached-result-mutation | Changing in place the list, dict, or set that a functools.lru_cache / functools.cache function returned (the cache hands the same object to later calls with the same arguments), directly or through the functions that return it. |
|
| MUT115 | shallow-copy-nested-mutation | Opt-in: changing in place an object below a shallow copy the function made of state that outlives the call (dict(x), x.copy(), copy.copy(x), model_copy()): the copied value holds the same object, so the change is seen through it too. |
โ ๐ |
| MUT118 | sentinel-lost-in-deepcopy | copy.deepcopy of an instance holding an object() sentinel in an attribute that a function tests against the sentinel by identity (the copy holds a new object, which the test does not match). |
โ |
| MUT119 | change-lost-on-new-object | An in-place change of a builtin container applied to a new object that nothing else holds and no name keeps (list(xs).sort(), env = os.environ.copy().update(...), obj.settings[k] = v with a getter returning a new dict): the change is lost when the statement ends. |
|
| MUT120 | readonly-view-mutation | Opt-in: changing a value held through a read-only view (a protocol or class whose members are all properties without a setter) where a type checker does not see it: a helper that does not see the view's type rebinds one of its properties, or code changes in place the container a property returns. | โ ๐ ๐งช |
| MUT201 | write-owner-violation | Writing a guarded attribute/container (directly, via alias, or via helper) outside declared write-owners. |
โ ๐ |
| MUT202 | readonly-return-mutation | Mutating a shared object returned by a declared readonly-returns producer, directly or through the functions that return it. |
โ ๐ |
| MUT203 | read-owner-violation | Reading a guarded attribute (directly, via alias, getattr, or __dict__) outside declared read-owners. |
โ ๐ |
| MUT204 | call-owner-violation | Calling a guarded function or method, or constructing a guarded class (directly, via a bound method, getattr, or a helper), outside declared call-owners. |
โ ๐ |
| MUT205 | unresolved-guarded-write | Strict mode of a write-owners contract (unresolved = "report"): writing an attribute named like a target on an object of unknown class, or a computed attribute name on a guarded class. |
โ ๐ |
| MUT206 | unresolved-guarded-read | Strict mode of a read-owners contract (unresolved = "report"): reading an attribute named like a target on an object of unknown class. |
โ ๐ |
| MUT207 | unresolved-guarded-call | Strict mode of a call-owners contract (unresolved = "report"): calling a method named like a target on an object of unknown class. |
โ ๐ |
| MUT208 | write-without-invalidation | Writing a declared invalidation target (directly, via alias, or via helper) without reaching one of its invalidators on every path to the function's return. |
โ ๐ |
Execution Effects (EFF)¶
Work that runs where it should not: blocking calls on the event loop, clients and queries inside loops.
| Code | Name | Message | |
|---|---|---|---|
| EFF101 | async-blocking-call | A call in async def that always runs a network, database, subprocess, or sleep call that waits on the event loop; its heuristic reports, a call that can reach a blocking call. |
|
| EFF102 | loop-unpooled-client | Allocating an unpooled HTTP/DB/RPC client or sessionless request inside a loop. | โ |
| EFF103 | loop-db-query | A loop over data whose every iteration sends a database statement; its heuristic reports, a loop that can run a database query on every iteration. | |
| EFF105 | async-blocking-fs | Opt-in: blocking local filesystem I/O reachable from async def. |
โ ๐ |
| EFF106 | lock-or-tx-held-across-io | Opt-in: await or blocking network I/O while holding a threading lock or a DB transaction. |
โ ๐ |
| EFF107 | unretained-async-task | Opt-in: an asyncio.create_task result bound to a local variable but never awaited, returned, or stored. |
โ ๐ |
| EFF109 | lock-reacquired-while-held | Opt-in: a non-reentrant lock (threading.Lock, asyncio.Lock) taken again, directly or through a call, while it is held (the thread or task waits for itself). |
โ ๐ ๐งช |
| EFF110 | exclusive-object-fanout | Opt-in: an object allowing one operation at a time (asyncio.StreamReader, an asyncpg connection, a WebSocket) used by tasks started together (asyncio.gather, a task group). |
โ ๐ ๐งช |
| EFF112 | set-order-output | Opt-in: the iteration order of a set of strings, which changes between runs, reaches a file, a stream, a serializer, or a raised exception's message across a call. | โ ๐ ๐งช |
| EFF114 | recursion-error-as-value | Opt-in: an except clause around a recursion (a call cycle reached from its try body) takes the RecursionError and goes on without raising, so the result depends on the stack depth. |
โ ๐ ๐งช |
| EFF115 | async-def-as-sync-callback | Opt-in: an async def handed to a library call that calls it without awaiting what it returns (a thread's target, an executor's function, an atexit or signal handler, a filter predicate), so the call only creates a coroutine and the body does not run. |
โ ๐ ๐งช |
| EFF116 | generator-never-iterated | Opt-in: a call of a generator function whose generator nothing iterates (a statement, a value only tested for truth, a function handed to a thread, an atexit handler, or a filter predicate), so its body does not run. |
โ ๐ ๐งช |
| EFF117 | unlocked-write-of-guarded-state | State that another writer changes holding a thread lock (threading.Lock, RLock, Condition, Semaphore), changed here holding none, where a thread heki resolves can run one of the writes: an update can be lost. |
โ |
| EFF201 | effect-boundary-violation | Reaching a forbidden effect (directly or through calls) from a declared effect-boundaries scope. |
โ ๐ |
Data Pipelines (DATA)¶
Quadratic re-allocation of DataFrames and arrays across loop iterations.
| Code | Name | Message | |
|---|---|---|---|
| DATA101 | loop-carried-concat | Loop-carried quadratic pandas.concat / numpy.vstack / numpy.append accumulation. |
Complexity Rewrites (OPT)¶
Quadratic patterns heki can rewrite, each proven safe by the alias analysis before a fix is offered.
| Code | Name | Message | |
|---|---|---|---|
| OPT101 | list-quadratic-concat | Unconditionally profitable list quadratic concatenation (x = x + [e] -> x.append(e)). |
๐ ๏ธ |
| OPT102 | set-dict-quadratic-merge | Unconditionally profitable set/dict quadratic merge ({**d, ...} with d first). |
๐ ๏ธ |
| OPT103 | immutable-quadratic-concat | Opt-in: size-dependent immutable quadratic accumulation (bytes/tuple/attr str). |
๐ |
| OPT110 | loop-linear-membership | Opt-in: size-dependent e in lst inside a loop -> hoist set outside loop. |
๐ง ๐ |
| OPT111 | loop-linear-dedup | Opt-in: size-dependent if v not in seen: seen.append(v) -> auxiliary set. |
๐ง ๐ |
| OPT112 | loop-linear-list-lookup | Size-dependent list.index / list.remove / list.count inside a loop (repeated linear scans). |
โ |
| OPT113 | nested-loop-equijoin | Size-dependent nested-loop equi-join (for a in A: for b in B: if a.k == b.k) -> dict index. |
|
| OPT120 | list-front-pop | Opt-in: size-dependent q.pop(0) / del q[0] / q = q[1:] -> collections.deque. |
๐ง ๐ |
| OPT121 | list-front-insert | Opt-in: size-dependent q.insert(0, v) -> collections.deque. |
๐ง ๐ |
| OPT130 | loop-invariant-container-rebuild | Opt-in: size-dependent loop-invariant container reconstruction (e in set(keys)). |
๐ง ๐ |
Imports (IMP)¶
Imports a contract does not allow, modules importing each other, and code or dependencies the package loads that installing it does not provide, each counted as Python loads them.
| Code | Name | Message | |
|---|---|---|---|
| IMP101 | import-cycle | Opt-in: project modules that import each other while they load (an import cycle), reported once per group at the import closing a shortest cycle. | โ ๐ ๐งช |
| IMP102 | circular-import-failure | Opt-in: a from M import name that raises ImportError when a module of its import cycle is imported before the others, because M is still loading and has not bound name. |
โ ๐ ๐งช |
| IMP104 | development-code-import | Opt-in: a module of the package the project distributes imports test code, or a module outside the source roots of a src layout, by an import that runs. | โ ๐ ๐งช |
| IMP201 | import-boundary-violation | An import that an import-boundaries contract does not allow: a forbidden module, or one outside the allowed imports. |
โ ๐ ๐งช |
| IMP202 | unresolved-guarded-import | Strict mode of an import-boundaries contract (unresolved = "report"): an import whose module heki cannot name, which may be one the contract does not allow. |
โ ๐ ๐งช |
| IMP301 | development-dependency-at-import-time | Opt-in: a package module loads, while it loads, a module whose distribution the project declares only for development (a dependency group), not in its dependencies or extras. | โ ๐ ๐งช |
| IMP302 | optional-dependency-at-import-time | Opt-in: importing a top-level package or an entry point's module loads, while it loads, a module whose distribution the project declares only in extras, so the import fails without them. | โ ๐ ๐งช |