development-dependency-at-import-time (IMP301)¶
Family: Imports (IMP) · Heuristic · Opt-in · Experimental · Severity error (a bug) · No automatic fix
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.
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 distributions provide a module 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, run while a module of the package the project distributes loads, of a module
whose distribution the project declares only for development. The import is in the package
module's body, or in the body of a project module it loads while it loads. The project declares
the distribution in a development group and in none of [project] dependencies and
[project.optional-dependencies], so installing the package does not ask for it.
Development groups are PEP 735 [dependency-groups] (with the groups an include-group takes
in), [tool.uv] dev-dependencies, [tool.poetry.group.<name>.dependencies],
[tool.poetry.dev-dependencies], and [tool.pdm.dev-dependencies]. Package code is the code of
the package the project distributes, as IMP104 decides it.
One report is given per module and module imported, at its first import (the names of one from
statement are one import), naming the package module whose load runs it when that is another module,
with the imports leading there as related locations.
What a report states¶
A report states that loading the package module it names runs the reported import whenever it loads, and that the project declares every distribution that may provide the imported module only for development.
Its premises, each established or no report:
- Package code: the module whose load runs the import is package code.
- Chain: the reported import, and each import leading to it from that module, is in a module body (or a class body it runs, or a constant by-name import there), runs on every path through the body, is no lazy import, and loads the module shown, its parent packages first.
- No handler: no handler may take the
ModuleNotFoundErrorof any of them, which a module that is not installed 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 one, and nofinallybody that may leave without raising the error again. A clause whose body raises on every path (raise,raise RuntimeError(...) from e) takes nothing, and a clause that certainly matches hides the clauses after it. - Distribution: the distributions that may provide the imported module are, when
[tool.heki.imports] module-distributionsnames the module or a package holding it, the ones it lists; otherwise the ones the catalog lists for it (PyYAMLprovidesyaml,pytestprovides_pytest) and the ones named like the module or a package holding it, with names compared as PEP 503 normalizes them (ruamel.yamlisruamel-yaml). The project declares at least one of them, and each one it declares is in no dependency and no extra. The project's own name in a group or an extra (dev = ["acme[test]"]) adds that extra, no distribution. - No copy: without
module-distributions, the project holds no module or package (a namespace package too) of the imported module's top-level name in a directory without__init__.py(setuptools/_vendor/), which asys.pathentry the code adds may name: the import may find that copy. - Known dependencies: the project's
pyproject.tomlhas a[project]or[tool.poetry]table, and[project] dynamiclists neitherdependenciesnoroptional-dependencies.
Why is this bad?¶
A dependency group is installed only in the project's own environments: uv sync, pdm install,
or poetry install add it for development, and installing the package does not. A package module
that imports such a distribution while it loads works in the project's checkout and its tests, and
raises ImportError for users, unless something else they install happens to depend on it.
Example¶
# acme/clock.py
from freezegun import freeze_time # IMP301
def frozen(at: str):
return freeze_time(at)
Use instead:
or declare freezegun in [project] dependencies (or in an extra, which IMP302
then judges), or give the import a fallback under except ModuleNotFoundError:.
Fix safety¶
No automatic fix.
Options¶
[tool.heki.imports] module-distributionsnames the distributions that may provide a module whose name differs from theirs, or settles a module several distributions may provide:module-distributions = { cv2 = ["opencv-python-headless"] }.- Its reports are errors:
lint.severity = { IMP301 = "warning" }makes them warnings.
Known limitations¶
- A module whose distribution is named neither like it nor by the catalog or
module-distributionsis not judged, and a distribution named like a module may not be the one that provides it (a project depending ontensorflow-cpuand listingtensorflowin a development group). - Imports inside functions, lazy imports, and imports on a branch of the module body are not judged: they need not run while the module loads.
- Dependencies declared outside
pyproject.toml(setup.py,setup.cfg, a requirements file a build backend reads) are not known, and neither are Hatch environments' dependencies. - A distribution a dependency installs is still reported: the report is about what the project declares, and relying on another distribution's dependencies breaks when that one changes.
- A module only a host program loads (a mypy, pytest, Sphinx, or MkDocs plugin of the package) is judged like any package module: the host installs what it needs.