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

Open Beginner friendly
#243 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, typescript
Domain
devtools, testing

Research direction

Start in src/repo/adapters/python.ts at resolvePythonGraph and the per-file fileRoots logic. Read the existing Python graph tests and add a case matching the issue’s services/api/tests package layout, where test_app.py imports sibling app. Done when changing services/api/app.py selects that test and the end-to-end analysis reports it as affected.

Written by the indexing model from the issue text.

Description

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.

Dominant language
TypeScript
Stars
0
Forks
4
Avg merge
18h 41m
Merged PRs (30d)
48

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DiffCI/core

All issues in DiffCI/core

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.