Skip to content

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 EFF or ALL. A report of an exact rule that did not establish every premise (an OPT report whose rewrite is not proven) is shown where a heuristic rule would run.
  • A heuristic rule runs when select / extend-select names its code or name. With lint.heuristic = true (--heuristic), the heuristic rules that are not opt-in also run by default and for a prefix or ALL.
  • The opt-in rules marked ๐Ÿ”Œ run only when select / extend-select names 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 --fix when 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. โ‰ˆ ๐Ÿ”Œ ๐Ÿงช