Skip to content

fix(python): resolve imports against a test package's basedir - #205

Merged
adityankale190895 merged 1 commit into
DiffCI:mainfrom
FredApps:fix/python-package-basedir
Nov 18, 2026
Merged

adityankale190895 merged 1 commit into
DiffCI:mainfrom
FredApps:fix/python-package-basedir

Conversation

@FredApps

Copy link
Copy Markdown
Contributor

Fixes #204

pytest's default prepend import mode puts a test's basedir on sys.path. That is the first directory above the test's package. resolvePythonGraph now resolves imports against that directory. Before, it only used the file's own directory when that directory had no __init__.py. Now services/api/tests/test_app.py (in package tests) resolves import app to services/api/app.py, as pytest does.

A package that reaches the repository root adds no extra root. A file outside any package behaves as before.

Validation:

  • tests/repo/python-package-basedir.test.ts fails on main and passes here.
  • npm run typecheck, npm test (1169 pass), npm run audit:boundary.

Unsupported: --import-mode=append and importlib are not read. They put fewer directories on sys.path, so the extra edges only over-select.

pytest's default prepend import mode puts the first directory above a test's package on sys.path. resolvePythonGraph only added a file's own directory when it had no __init__.py, so a test in services/api/tests/ (a package) that imports services/api/app.py as `app` got no edge and no blocker, and a change to app.py selected nothing.

Signed-off-by: FredApps <[email protected]>

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cross-review of 4957923.

Checked:

  • The fix matches #204. basedirOf walks up to the nearest ancestor without __init__.py, which is where pytest's prepend mode puts the basedir. It adds nothing when the package reaches the repository root, and it does not add a root that is already present.
  • The new test fails with src/ reverted to main (1 of 2) and passes with the fix (2 of 2).
  • tests/repo/python-package-basedir.test.ts is listed in release-files.json. The commit is DCO-signed. The branch is level with main.
  • CI: every job passes. The hosted "DiffCI change impact" check is still showing in progress. That check never blocks a merge.

Fixed: nothing.

Still open (nit, not blocking): the rewritten module comment at the top of python.ts now runs onto one line of about 180 characters. I left it alone so it doesn't conflict with #207, which edits the next lines.

FredApps added a commit to FredApps/core that referenced this pull request Nov 18, 2026
…ffCI#205, DiffCI#209 and DiffCI#257

The manifest is an unordered set, and the PRs listed above add their entries at the same line. Moving this entry one line away lets them merge in either order without a conflict.

Signed-off-by: FredApps <[email protected]>
FredApps added a commit to FredApps/core that referenced this pull request Nov 18, 2026
…ffCI#205, DiffCI#207 and DiffCI#257

The manifest is an unordered set, and the PRs listed above add their entries at the same line. Moving this entry one line away lets them merge in either order without a conflict.

Signed-off-by: FredApps <[email protected]>
FredApps added a commit to FredApps/core that referenced this pull request Nov 18, 2026
…ffCI#205, DiffCI#207 and DiffCI#209

The manifest is an unordered set, and the PRs listed above add their entries at the same line. Moving this entry one line away lets them merge in either order without a conflict.

Signed-off-by: FredApps <[email protected]>
@FredApps
FredApps marked this pull request as ready for review November 18, 2026 00:56
@adityankale190895
adityankale190895 merged commit e7b4a65 into DiffCI:main Nov 18, 2026
12 of 13 checks passed
adityankale190895 pushed a commit that referenced this pull request Nov 18, 2026
* fix(python): follow pytest plugins that addopts loads with -p

readPytestSettings never read addopts, so a first-party plugin loaded with `addopts = -p myplugin` had no importer in the graph and a change to it selected no tests. Every test now depends on each -p plugin that resolves in the repository; an unresolved first-party plugin falls back. A triple-quoted pyproject.toml value, which the reader would take as an empty string, now marks the key unreadable.

Signed-off-by: FredApps <[email protected]>

* fix(python): read multi-line pyproject.toml strings instead of falling back

A triple-quoted addopts (or other key) in pyproject.toml now has its
contents read, so a repository with a multi-line addopts keeps selective
runs. Only an unterminated multi-line string still makes the analysis
fall back.

Signed-off-by: FredApps <[email protected]>

* chore: drop a stray node_modules link from the previous commit

Signed-off-by: FredApps <[email protected]>

* chore: move the release-files.json entry so it merges cleanly with #205, #209 and #257

The manifest is an unordered set, and the PRs listed above add their entries at the same line. Moving this entry one line away lets them merge in either order without a conflict.

Signed-off-by: FredApps <[email protected]>

---------

Signed-off-by: FredApps <[email protected]>
adityankale190895 pushed a commit that referenced this pull request Nov 18, 2026
* fix(python): expand wildcards in pytest testpaths

pytest expands each testpaths entry with glob.iglob(entry, recursive=True). isPytestFile compared entries as literal directory prefixes, so with testpaths = ["tests", "pkgs/*/tests"] the tests under pkgs/*/tests were not test files, and a change they cover selected nothing.

Signed-off-by: FredApps <[email protected]>

* chore: move the release-files.json entry so it merges cleanly with #205, #207 and #257

The manifest is an unordered set, and the PRs listed above add their entries at the same line. Moving this entry one line away lets them merge in either order without a conflict.

Signed-off-by: FredApps <[email protected]>

---------

Signed-off-by: FredApps <[email protected]>
adityankale190895 pushed a commit that referenced this pull request Nov 18, 2026
…rs (#257)

* fix(impact): treat modelled Python files as code when resolving readers

resolveByReference passed isSourceFilePath as isCodeFile, which does not include .py. A pytest test that reads a changed data file was therefore followed as a script; nothing names a test file, so the change selected no tests and did not fall back. Python files the adapter modelled now count as code, so the reading test is selected, and a helper that reads the file selects the tests that import it.

Signed-off-by: FredApps <[email protected]>

* chore: move the release-files.json entry so it merges cleanly with #205, #207 and #209

The manifest is an unordered set, and the PRs listed above add their entries at the same line. Moving this entry one line away lets them merge in either order without a conflict.

Signed-off-by: FredApps <[email protected]>

* ci: re-run checks after the fuzz-timing flake (#300, fixed by #302)

Signed-off-by: FredApps <[email protected]>

---------

Signed-off-by: FredApps <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants