Skip to content

Python: a test inside a package misses imports that resolve from its basedir #204

Closed

Description

@FredApps

resolvePythonGraph (src/repo/adapters/python.ts, the fileRoots line in the per-file loop) adds a file's own directory to the import roots only when that directory has no __init__.py. pytest's default prepend import mode puts the file's basedir on sys.path: the first directory above the file's package. A test inside a package below the repository root can import modules beside that package, and DiffCI misses the edge.

pyproject.toml                    [tool.pytest.ini_options]
services/api/app.py               def f(): return 1
services/api/tests/__init__.py
services/api/tests/test_app.py    import app

python -m pytest services/api/tests/test_app.py passes (pytest 9.2).

Expected: a change to services/api/app.py selects services/api/tests/test_app.py.

Actual: no edge and no blocker (app is not first-party when seen from the root), so the change is selective with no tests:

resolvePythonGraph(
  ["services/api/app.py", "services/api/tests/__init__.py", "services/api/tests/test_app.py"],
  { "services/api/app.py": { imports: [] }, "services/api/tests/__init__.py": { imports: [] },
    "services/api/tests/test_app.py": { imports: [["app", 0, []]] } },
  { pythonFiles: ["test_*.py"], testpaths: [], pythonpath: [] },
  new Set(["services/api/tests/test_app.py"]),
); // { edges: [], blockers: [] }

End to end, ImpactAnalyzer returns fallbackRequired: false and no affected tests.

Fix: resolve against the basedir: the file's directory when it is not a package, otherwise the nearest ancestor without __init__.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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