development-code-import (IMP104)¶
Family: Imports (IMP) · Heuristic · Opt-in · Experimental · Severity error (a bug) · No automatic fix
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.
Heuristic
This rule is heuristic: a premise of its claim can be open ("What a report states" lists them), so a report can be wrong about the code. It runs only when select or extend-select names its code or name. It has not been measured yet. See Rule Precision.
Experimental
It is new: every report rests on established premises only, but its reports are heuristic until a review of its reports on real code bases certifies its claim, and which modules count as package and development code can still change. 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.
What it does¶
Reports an import, in a module of the package the project distributes, of development code: test
code beside the package, or a module outside the source roots of a src layout. The import runs: it
is in the module body, a lazy import, or inside a function. An import under TYPE_CHECKING never
runs, and an import whose missing module a handler takes (except ModuleNotFoundError: with a
fallback) expects the module to be missing; neither is reported. One report is given per function and module imported, at its first import: the
names of one from statement are one import.
heki finds it without configuration, from the project's layout and pyproject.toml:
- Package code is a module in the tree of the package the project distributes: in a src
layout, below a source root (
src/acme/io.py); in a flat layout, below a top-level package named like the project ([project] name, normalized) or named by the build backend's settings ([tool.setuptools] packages,[tool.hatch.build.targets.wheel] packages,[tool.uv.build-backend] module-name). Test modules, the files pytest collects tests or fixtures from by default (under atests/ortest/directory,test_*.py,*_test.py,conftest.py), are not package code: those in the package's tree install with it, and are neither package nor development code here. - Development code is what no installed distribution holds: a test module outside the
package's tree (
tests/beside the package), and, in a src layout, every module outside the source roots (scripts/,tools/,examples/, run from the project root).
What a report states¶
A report states that the module of the package it is in imports, at the reported statement, a module of the project that is development code, outside the tree of the package the project distributes. It says where the code is: a build that adds that code to the distribution makes the import work.
Its premises, each established or no report:
- Package code: the module of the import is package code, as above. A workspace's members and a flat layout whose packages are named nowhere heki reads are neither: their modules give no report.
- Development code: the module imported is development code, as above.
- Module: the import loads that module, its name resolved against the project's modules (for
from a import c, the packagea, which it loads in any case). - Runs: the import is in the module body, a lazy import, or in a function, not under
TYPE_CHECKING. - No handler: no handler may take its
ModuleNotFoundError, which an installation without the module raises: noexceptclause namingModuleNotFoundError,ImportError,Exception,BaseException, a type heki cannot tell is another builtin exception, or a bareexcept, whose body may end without raising, nowithblock around it, and nofinallybody that may leave without raising the error again. A clause whose body raises on every path (raise) takes nothing, and a clause that certainly matches hides the clauses after it.
Why is this bad?¶
An installed package usually holds only its own modules. Test helpers, conftest.py, and the
scripts of a src layout stay in the repository, so an import of them works in the project's own
checkout and fails for everyone who installs the package. The tests pass, because they run where
the development code is.
Example¶
# src/acme/report.py
from tests.factories import make_order # IMP104
def sample_report() -> str:
return render(make_order())
Use instead:
# src/acme/report.py
from acme.samples import make_order
def sample_report() -> str:
return render(make_order())
Fix safety¶
No automatic fix.
Options¶
- Its reports are errors:
lint.severity = { IMP104 = "warning" }makes them warnings.
Known limitations¶
- Which files a build ships is read only from the settings listed above: a build that leaves out part of the package's tree, or adds files from outside it, is not known.
- Imports heki does not see (
exec,runpy, a module loaded from a file path) are not judged.