Skip to content

Include modules in directories without __init__.py #298

@jstriebel

Description

@jstriebel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions