Python allows importing modules from directories without __init__.py (implicit namespace packages per PEP 420). However, grimp doesn't find these modules, so import-linter (my actual use-case) silently excludes them from analysis.
This means contracts may not cover the entire codebase with no warning.
Note: Grimp already supports namespace packages for the cross-distribution case (e.g., google.cloud.logging). This issue is different: it's about directories within a regular package that lack __init__.py.
Example
mypackage/
├── __init__.py
├── core.py
├── a/ # no __init__.py
│ └── missing.py # ← Python can import this
└── b/
├── __init__.py
└── found.py
Python can import mypackage.a.missing:
>>> from mypackage.a import missing # works
But grimp doesn't see it:
>>> import grimp
>>> graph = grimp.build_graph("mypackage")
>>> "mypackage.a.missing" in graph.modules
False
Suggested fix
Add an option (e.g., include_implicit_namespace_packages = true) to scan the filesystem for .py files in directories without __init__.py and include them in the graph.
If the above is too invasive, at least warn when .py files exist on disk but weren't included in analysis.
Reproduction
mkdir -p mypackage/a mypackage/b
touch mypackage/__init__.py mypackage/core.py
touch mypackage/a/missing.py
touch mypackage/b/__init__.py mypackage/b/found.py
import grimp
graph = grimp.build_graph("mypackage")
print(sorted(m for m in graph.modules))
# ['mypackage', 'mypackage.b', 'mypackage.b.found', 'mypackage.core']
# mypackage.a.missing is missing
Environment
- import-linter: 2.9
- Python: 3.11+
This might be directly related to seddonym/import-linter#295
Python allows importing modules from directories without
__init__.py(implicit namespace packages per PEP 420). However, grimp doesn't find these modules, so import-linter (my actual use-case) silently excludes them from analysis.This means contracts may not cover the entire codebase with no warning.
Note: Grimp already supports namespace packages for the cross-distribution case (e.g.,
google.cloud.logging). This issue is different: it's about directories within a regular package that lack__init__.py.Example
Python can import
mypackage.a.missing:But grimp doesn't see it:
Suggested fix
Add an option (e.g.,
include_implicit_namespace_packages = true) to scan the filesystem for.pyfiles in directories without__init__.pyand include them in the graph.If the above is too invasive, at least warn when
.pyfiles exist on disk but weren't included in analysis.Reproduction
Environment
This might be directly related to seddonym/import-linter#295