tests: one pytest session can hold both test_address_guard modules #2769

Closed
leonida/mandy
fiestorra wants to merge 0 commits from pytest-shared-basenames into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

fiestorra commented 2026-11-30 17:29:04 +00:00

pytest tests/ does not run on main: it stops during collection and executes nothing. Two test modules share a basename, and pytest's default import mode keys a module by that basename.

$ python3 -m pytest tests/ -q
ERROR tests/test_address_guard.py
  import file mismatch:
  imported module 'test_address_guard' has this __file__ attribute:
    .../tests/semantic/test_address_guard.py
  which is not the same as the test file we want to collect:
    .../tests/test_address_guard.py
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
18955 tests collected, 1 error in 16.09s

Naming both files explicitly fails the same way, so it is not only the whole-directory invocation:

$ python3 -m pytest tests/semantic/test_address_guard.py tests/test_address_guard.py -q
   (no summary line at all - the session dies in collection)

The change is one file, no code touched:

[pytest]
addopts = --import-mode=importlib

Measured, before and after, on main be7690805:

before   pytest tests/ -q --collect-only      18955 collected, 1 error
after    pytest tests/ -q --collect-only      18962 collected, 0 errors
before   pytest <both address_guard files>    collection error, nothing runs
after    pytest <both address_guard files>    19 passed, 21 subtests passed

Why the CI never saw it: .github/workflows/ci.yml runs pytest one file at a time (pytest_files+=("$t")), so the two modules are never in the same session. The failure only reaches a person running the suite the obvious way.

That the repo's own harness still works, with this file in place:

python3 -I scripts/run_semantic_tests.py tests/semantic
549 files, 6686 tests, 0 files failed in 425 s with 16 jobs      (exit 0)

And an ordinary module that imports the shared conftest.py still passes: pytest tests/test_report_paths.py -> 26 passed, 23 subtests passed.

What I did not do: rename the module. Two modules with the same basename are legal; what was wrong is the import mode. importlib also removes the class of failure for the next pair that collides, and the conftest's sys.path insertion is unaffected (the semantic run above exercises it).

Not tested: the whole suite to completion (it takes over half an hour and hits network-ish tests); collection is where the defect lived, and that is measured.

AI-assisted: yes - DeepSeek v4.1 (flash) as the coding agent, reviewed by a separate reviewer agent. Where: Windows 11, Python 3.13.14; no GPU path involved.

Written with an AI assistant.

**`pytest tests/` does not run on `main`: it stops during collection and executes nothing. Two test modules share a basename, and pytest's default import mode keys a module by that basename.** ``` $ python3 -m pytest tests/ -q ERROR tests/test_address_guard.py import file mismatch: imported module 'test_address_guard' has this __file__ attribute: .../tests/semantic/test_address_guard.py which is not the same as the test file we want to collect: .../tests/test_address_guard.py !!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!! 18955 tests collected, 1 error in 16.09s ``` **Naming both files explicitly fails the same way**, so it is not only the whole-directory invocation: ``` $ python3 -m pytest tests/semantic/test_address_guard.py tests/test_address_guard.py -q (no summary line at all - the session dies in collection) ``` **The change is one file, no code touched:** ```ini [pytest] addopts = --import-mode=importlib ``` **Measured, before and after, on `main` `be7690805`:** ``` before pytest tests/ -q --collect-only 18955 collected, 1 error after pytest tests/ -q --collect-only 18962 collected, 0 errors before pytest <both address_guard files> collection error, nothing runs after pytest <both address_guard files> 19 passed, 21 subtests passed ``` **Why the CI never saw it:** `.github/workflows/ci.yml` runs pytest **one file at a time** (`pytest_files+=("$t")`), so the two modules are never in the same session. **The failure only reaches a person running the suite the obvious way.** **That the repo's own harness still works, with this file in place:** ``` python3 -I scripts/run_semantic_tests.py tests/semantic 549 files, 6686 tests, 0 files failed in 425 s with 16 jobs (exit 0) ``` **And an ordinary module that imports the shared `conftest.py` still passes:** `pytest tests/test_report_paths.py` -> **26 passed, 23 subtests passed**. **What I did not do:** rename the module. **Two modules with the same basename are legal**; what was wrong is the import mode. **`importlib` also removes the class of failure for the next pair that collides**, and **the conftest's `sys.path` insertion is unaffected** (the semantic run above exercises it). **Not tested:** the whole suite to completion (it takes over half an hour and hits network-ish tests); **collection is where the defect lived**, and that is measured. **AI-assisted:** yes - DeepSeek v4.1 (flash) as the coding agent, reviewed by a separate reviewer agent. **Where:** Windows 11, Python 3.13.14; no GPU path involved. Written with an AI assistant.
fiestorra commented 2026-11-30 17:31:46 +00:00

Closing this: the CI caught a real regression in my own change, and the defect I set out to fix is smaller than the one I would have introduced.

What the CI said, on linux-python-and-viewer:

Translator tests: failure
  ImportError while importing test module '.../tests/test_cube_sample.py'
  ModuleNotFoundError: No module named 'listing'

Reproduced here, minimal:

python -m pytest tests/test_cube_sample.py -q                     -> 18 passed
python -m pytest tests/test_cube_sample.py -q --import-mode=importlib -> 1 error

Why: tests/test_cube_sample.py:14 does from listing import translate_listing, and tests/listing.py is its sibling. prepend mode puts the test file's own directory on sys.path; importlib does not. One module named listing is more fragile than two modules named test_address_guard.

And what I got wrong about the size of the original problem: I wrote that the CI never saw the collision because it runs pytest per file. It does not - ci.yml:194 is python -m pytest "${pytest_files[@]}" -q, one session, many files. What actually keeps the collision away is line 193: find tests -path 'tests/semantic' -prune - the CI deliberately never collects tests/semantic/, so the two test_address_guard modules are never in the same session there.

So the collision only reaches someone running pytest tests/ over the whole tree, including tests/semantic/, which is nobody's CI and not the documented way either: the repository's own commands are python -I tests/test_x.py and scripts/run_semantic_tests.py, and both work today.

What would fix it without the regression, for whoever wants it:

  • add sys.path.insert(0, os.path.dirname(__file__)) to tests/test_cube_sample.py (and to any sibling that imports a helper the same way), then --import-mode=importlib is safe; or
  • rename one of the two test_address_guard.py files, which needs no import-mode change at all but touches the file list and any inventory that counts it.

I am not opening either as a pull request now: the first touches files unrelated to the collision, and the second renames someone's test. The measurement is here so the choice can be made by whoever owns the tree.

The lesson, which is the reason this comment exists: pytest tests/ collecting and passing is not the CI's contract. The CI's contract is the predicate in ci.yml - a pruned file list - and I had assumed the simpler shape and measured against it. A fix validated on the wrong invocation is a fix validated on nothing.

Written with an AI assistant.

**Closing this: the CI caught a real regression in my own change, and the defect I set out to fix is smaller than the one I would have introduced.** **What the CI said, on `linux-python-and-viewer`:** ``` Translator tests: failure ImportError while importing test module '.../tests/test_cube_sample.py' ModuleNotFoundError: No module named 'listing' ``` **Reproduced here, minimal:** ``` python -m pytest tests/test_cube_sample.py -q -> 18 passed python -m pytest tests/test_cube_sample.py -q --import-mode=importlib -> 1 error ``` **Why:** `tests/test_cube_sample.py:14` does `from listing import translate_listing`, and `tests/listing.py` is its sibling. **`prepend` mode puts the test file's own directory on `sys.path`; `importlib` does not.** **One module named `listing` is more fragile than two modules named `test_address_guard`.** **And what I got wrong about the size of the original problem:** I wrote that the CI never saw the collision because it runs pytest per file. **It does not** - `ci.yml:194` is `python -m pytest "${pytest_files[@]}" -q`, **one session, many files.** **What actually keeps the collision away is line 193: `find tests -path 'tests/semantic' -prune`** - **the CI deliberately never collects `tests/semantic/`, so the two `test_address_guard` modules are never in the same session there.** **So the collision only reaches someone running `pytest tests/` over the whole tree, including `tests/semantic/`**, which is nobody's CI and not the documented way either: the repository's own commands are `python -I tests/test_x.py` and `scripts/run_semantic_tests.py`, **and both work today.** **What would fix it without the regression, for whoever wants it:** - add `sys.path.insert(0, os.path.dirname(__file__))` to `tests/test_cube_sample.py` (and to any sibling that imports a helper the same way), **then** `--import-mode=importlib` is safe; or - rename one of the two `test_address_guard.py` files, **which needs no import-mode change at all** but touches the file list and any inventory that counts it. **I am not opening either as a pull request now:** **the first touches files unrelated to the collision, and the second renames someone's test.** **The measurement is here so the choice can be made by whoever owns the tree.** **The lesson, which is the reason this comment exists:** **`pytest tests/` collecting and passing is not the CI's contract.** **The CI's contract is the predicate in `ci.yml` - a pruned file list - and I had assumed the simpler shape and measured against it.** **A fix validated on the wrong invocation is a fix validated on nothing.** Written with an AI assistant.
OreoFR commented 2026-11-30 18:51:39 +00:00

Verified by OreoFR on macOS 27.2.1, Apple M5, Python 3.12.16, pytest, main ad25611b5 merged with head 983132ee6.

  • MEASURED, red (main): pytest tests/test_address_guard.py tests/semantic/test_address_guard.py → 1 error (the import file mismatch); pytest --collect-only tests → 18998 collected, 1 error.
  • MEASURED, green (main + this PR): the two files together → 19 passed, 21 subtests passed; pytest --collect-only tests → 19005 collected, 0 errors (the 7 extra are the module that could not be collected before).
  • READ IN CODE: 559 test files import a sibling helper module (e.g. spirv_wave_interp) at module level. Under importlib mode pytest no longer puts the test folders on sys.path itself, so those imports rely on each file's own path setup. Collection imports every module, so 0 collection errors with the PR means all of them still resolve.
  • In progress: the full pytest tests on both sides (it takes hours here). I will add any failure that differs between the two.

AI-assisted: Claude Code

Verified by OreoFR on macOS 27.2.1, Apple M5, Python 3.12.16, pytest, `main` `ad25611b5` merged with head `983132ee6`. - MEASURED, red (`main`): `pytest tests/test_address_guard.py tests/semantic/test_address_guard.py` → **1 error** (the import file mismatch); `pytest --collect-only tests` → 18998 collected, **1 error**. - MEASURED, green (`main` + this PR): the two files together → **19 passed, 21 subtests passed**; `pytest --collect-only tests` → **19005 collected, 0 errors** (the 7 extra are the module that could not be collected before). - READ IN CODE: 559 test files import a sibling helper module (e.g. `spirv_wave_interp`) at module level. Under `importlib` mode pytest no longer puts the test folders on `sys.path` itself, so those imports rely on each file's own path setup. Collection imports every module, so 0 collection errors with the PR means all of them still resolve. - In progress: the full `pytest tests` on both sides (it takes hours here). I will add any failure that differs between the two. AI-assisted: Claude Code
OreoFR commented 2026-11-30 19:01:56 +00:00

Follow-up: I stopped the two full pytest tests runs at about 32 % (both sides) to free the machine for another check, so I have no full-suite comparison to add. What stands from the comment above: main fails collection with 1 error, this PR collects all 19005 tests with 0 errors, and the two test_address_guard modules pass together.

AI-assisted: Claude Code

Follow-up: I stopped the two full `pytest tests` runs at about 32 % (both sides) to free the machine for another check, so I have no full-suite comparison to add. What stands from the comment above: `main` fails collection with 1 error, this PR collects all 19005 tests with 0 errors, and the two `test_address_guard` modules pass together. AI-assisted: Claude Code
Sign in to join this conversation.
No description provided.