Getting Started¶
This page runs heki on a project, reads a diagnostic, applies a fix, and picks the rules.
Run it¶
From the root of a project (the directory with pyproject.toml):
The first command runs the exact rules that are not opt-in, and the rules of the contracts you declare. An exact rule is certified: every report it made on the development corpus and on a frozen holdout corpus was checked by hand, and none was false (Rule Precision). Without configuration, it runs:
MUT110(readonly-param-mutation)MUT114(cached-result-mutation)MUT119(change-lost-on-new-object)EFF101(async-blocking-call)EFF103(loop-db-query)DATA101(loop-carried-concat)OPT101(list-quadratic-concat)OPT102(set-dict-quadratic-merge)OPT113(nested-loop-equijoin)
MUT101, MUT111, and the size-dependent OPT rules (OPT103, OPT110, OPT111, OPT120,
OPT121, OPT130) are exact too, but run only when select or extend-select names them (in pyproject.toml or on the command
line, by code or name), or, for the five with a rewrite (not OPT103), when
--include-size-dependent is given: their reports were rarely harmful (Rule Precision).
A report of an exact rule that did not establish every premise of its claim, such as an OPT
rewrite heki could not prove safe, is heuristic. A text run says how many heuristic rules and
reports it left out. --heuristic adds them; they can be wrong about the code, and every output
labels them.
heki checks every Python file under the current directory, except the excluded and ignored ones. Pass directories to narrow it, or files to look at: a file is checked in its project, so heki still analyzes the whole project and shows the reports that touch the file.
Ask what fits the project¶
heki recommend (experimental) analyzes the project as heki check does and says which rules
would report here, what heki could not see, and the contracts the code already keeps, with the
keys to add to [tool.heki]. It writes no file. A run of heki check that shows nothing, on a
project without contracts or a baseline, ends with a line that points to it:
See Configuration Advice. To adopt heki in a repository step by step, see Adopting heki.
Read a diagnostic¶
app/api.py:5:12: EFF101 (async-blocking-call) [error] `app.users.load_user` always runs the waiting call `requests.get` on the event loop (1 call down, on every path)
|
5 | user = load_user(user_id)
| ^
= related: app/api.py:5:12: `handler` calls `app.users.load_user`
= related: app/users.py:5:12: `load_user` calls `requests.get`
= help: offload it with `asyncio.to_thread`, or use an async client
| Part | Meaning |
|---|---|
app/api.py:5:12 |
where to look |
EFF101 (async-blocking-call) |
the rule's code and name |
[error] |
its severity: error for a bug that can be critical (a wrong result, an exception, a lost update, a deadlock, an event loop held up for every other task, as here, a broken contract), warning for slower code or for a bug that is rarely harmful. An exact error makes heki check fail; a warning is shown without failing it, unless --fail-on warning (lint.fail-on = "warning") is set. A heuristic report adds heuristic ([error, heuristic]): it can be wrong about the code, as a premise of its claim can be open, so it counts as a warning and fails the run only with --fail-on warning |
| the message | what heki established, in one sentence: here, that the call to load_user always runs requests.get, a call that waits on the network, on the event loop |
related lines |
the hops of the chain of calls behind it, to follow it across files; editors show them as related information you can jump to |
help line |
what to do about it |
heki rule EFF101 explains the rule, its exclusions, and what to do instead. The
rules reference has the same text for every rule.
Fix what heki can prove¶
--fix applies the rewrites heki proves safe and always faster (OPT101, OPT102), and
--diff shows them first:
$ heki check --diff
--- a/app/report.py
+++ b/app/report.py
@@ -1,5 +1,5 @@
def rows_of(items):
rows = []
for item in items:
- rows = rows + [item]
+ rows.append(item)
return rows
Would fix 1 issue(s) in 1 file(s).
$ heki check --fix
Fixed 1 issue(s) in 1 file(s).
--fix then reports what is left, such as the EFF101 above, which has no automatic fix.
When heki cannot prove a fix safe, the help says why, nothing is rewritten, and the report is heuristic whatever the rule's certainty. Here the list is also handed to other code inside the loop:
$ heki check --heuristic app/shared.py
app/shared.py:4:9: OPT101 (list-quadratic-concat) [warning, heuristic] `rows = rows + [...]` copies the whole list each time it runs in the loop
|
4 | rows = rows + [item]
| ^
= help: append in place with `rows.append(...)` / `rows.extend(...)` [not auto-fixable: Unique(rows): it may be shared: passed to code heki cannot see (line 5)]
Found 1 diagnostic(s) in 1 file(s).
Fixes and Safety lists which rules are fixed, and what a fix must prove.
Silence a diagnostic¶
Append a suppression comment to the reported line:
The other forms (# heki: ignore, # noqa: EFF101) and per-file-ignores are in
Inline suppressions.
Choose the rules¶
$ heki check --select EFF,MUT # only these families
$ heki check --extend-select EFF105 # add an opt-in rule
$ heki check --ignore OPT113 # everything except one rule
Codes, code prefixes, and rule names (async-blocking-call) are accepted. A prefix does not
enable the opt-in rules, nor the heuristic ones without --heuristic: see
Rule selection.
Output formats¶
| Format | Prints |
|---|---|
full (default; github in GitHub Actions) |
each diagnostic with its source line, call chain, and help |
concise |
one line per diagnostic: the message, without the help |
json |
every field: also the help, the evidence, the blocker that stops a fix, and the fix edits (fields) |
github |
GitHub Actions annotations with the message (CI) |
sarif |
the message, the location, the call chain, and the help (properties.help), for code-scanning dashboards (no evidence or blocker) |
Without --output-format, HEKI_OUTPUT_FORMAT chooses the format, and in GitHub Actions
(GITHUB_ACTIONS=true) it is github.
Every format, --diff, and the errors of files that cannot be read or parsed print a file's
path relative to the working directory when the file is inside it (otherwise as given), as Ruff
does, and with / separators on every platform, Windows included.
Colors¶
On a terminal, the full and concise output and --diff are colored as Ruff colors its
output: paths in bold, rule codes in red, the severity, the source frame and its caret, and the
lines a diff removes and adds. The same variables and option as Ruff's decide it, each row over
the rows above it:
| Setting | Colors |
|---|---|
| stdout is a terminal / a pipe or a file | on / off |
NO_COLOR set (to anything), or CLICOLOR=0 |
off |
FORCE_COLOR set to anything but the empty string, or CLICOLOR_FORCE to anything but 0 |
on (for a CI log that shows colors) |
--color always / --color never |
on / off |
json, github, and sarif output never holds colors, and the summary and the errors heki
writes to stderr are not colored.
Exit codes¶
| Code | Meaning |
|---|---|
0 |
no diagnostics, or only warnings and heuristic reports (they are printed) |
1 |
an exact error found, or any report with --fail-on warning (lint.fail-on = "warning"), among the Ratchet regressions when a baseline is used; also when --update-baseline refuses a grown entry, a baseline entry matched nothing with report-unused-suppressions, or --diff would change files |
2 |
configuration error, unknown rule, or a file that cannot be read or has a syntax error; with files given (heki check src/app.py, --touched), only one of those files fails the run, and the others are named in a note |