conftest: install the piecewise finder before the translator is imported #2905

Merged
fiestorra merged 1 commit from upstream-pr/2905 into main 2026-12-02 00:56:29 +00:00
fiestorra commented 2026-12-01 17:00:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-01T17:00:19Z.


main is red on the CI workflow for one test, and this is why: test_support_installs_the_finder_only_where_the_tokenizer_is_slow asserts which loader the translator was imported through, and under pytest it is always the wrong one.

AssertionError: <class '_frozen_importlib_external.SourceFileLoader'>
                is not <class 'support.PiecewiseLoader'>
  tests\test_piecewise_parse.py:961

tests/semantic/support.py does the right thing — it installs ToolsFinder just before PathFinder and then imports rdna2spirv through it — but tests/conftest.py imports rdna2spirv first, for its own fixtures:

sys.path.insert(0, os.path.join(ROOT, "tools"))
import mc_command  # noqa: E402
import rdna2spirv  # noqa: E402

A finder can only take an import it is in sys.meta_path for when the module is first asked for. By the time support runs, rdna2spirv is in sys.modules with PathFinder's loader, and support's own import rdna2spirv is a no-op.

That is also why the same test is green run the other way: python tests/test_piecewise_parse.py has no conftest, so support imports first and the finder takes it. Same code, two answers, decided by which runner started it.

The change: conftest puts tests/semantic on the path and imports support before rdna2spirv. support imports rdna2spirv itself, so the line after it stays and becomes the import that already happened — no fixture changes, no reloads, and nothing that depends on the test's own order.

Measured on main 85ec1d33b (CPython 3.14 here, where needed() is true):

python -m pytest tests/test_piecewise_parse.py    1 failed, 25 passed  ->  26 passed
python tests/test_piecewise_parse.py              OK, before and after
the five files ci.yml runs for this area + test_cpu_ref
                                                  81 passed, 34 skipped

What this does not touch: the finder, the loader, piecewise_parse, or which Python versions need any of it. It only makes the conftest stop taking the import out from under it.

AI-assisted: yes — DeepSeek v4.1 (flash), which ran the test both ways to find out why it disagreed with itself.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5590456726 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2905). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-01T17:00:19Z. --- **`main` is red on the `CI` workflow for one test, and this is why: `test_support_installs_the_finder_only_where_the_tokenizer_is_slow` asserts which loader the translator was imported through, and under pytest it is always the wrong one.** ``` AssertionError: <class '_frozen_importlib_external.SourceFileLoader'> is not <class 'support.PiecewiseLoader'> tests\test_piecewise_parse.py:961 ``` **`tests/semantic/support.py` does the right thing** — **it installs `ToolsFinder` just before `PathFinder` and then imports `rdna2spirv` through it** — **but `tests/conftest.py` imports `rdna2spirv` first, for its own fixtures:** ```python sys.path.insert(0, os.path.join(ROOT, "tools")) import mc_command # noqa: E402 import rdna2spirv # noqa: E402 ``` **A finder can only take an import it is in `sys.meta_path` for when the module is first asked for.** **By the time `support` runs, `rdna2spirv` is in `sys.modules` with `PathFinder`'s loader, and `support`'s own `import rdna2spirv` is a no-op.** **That is also why the same test is green run the other way**: `python tests/test_piecewise_parse.py` has no conftest, so `support` imports first and the finder takes it. **Same code, two answers, decided by which runner started it.** **The change**: `conftest` puts `tests/semantic` on the path and imports `support` before `rdna2spirv`. **`support` imports `rdna2spirv` itself**, so the line after it stays and becomes the import that already happened — no fixture changes, no reloads, and nothing that depends on the test's own order. **Measured on `main` `85ec1d33b` (CPython 3.14 here, where `needed()` is true):** ``` python -m pytest tests/test_piecewise_parse.py 1 failed, 25 passed -> 26 passed python tests/test_piecewise_parse.py OK, before and after the five files ci.yml runs for this area + test_cpu_ref 81 passed, 34 skipped ``` **What this does not touch**: **the finder, the loader, `piecewise_parse`, or which Python versions need any of it.** **It only makes the conftest stop taking the import out from under it.** **AI-assisted:** yes — DeepSeek v4.1 (flash), which ran the test both ways to find out why it disagreed with itself. Written with an AI assistant.
merzleckeier commented 2026-12-01 17:43:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T17:43:48Z.


Independent pytest import-order check at 74da6c8a4 + d2a564b66.

I ran python -m pytest -p no:cacheprovider -q tests/test_piecewise_parse.py tests/test_cpu_ref.py on both revisions. With CPython 3.14.7, main had 64 passes and failed the loader assertion because it used SourceFileLoader. Main plus this PR passed all 65 tests. Each run also passed 1,870 subtests. With CPython 3.14.10, both revisions passed all 65 tests and 1,870 subtests. Running python tests/test_piecewise_parse.py directly passed all 26 tests in each of the four runs.

MEASURED with synthetic inputs. AI-assisted (Codex).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7319758342 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2905#issuecomment-7319758342). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-01T17:43:48Z. --- Independent pytest import-order check at `74da6c8a4` + `d2a564b66`. I ran `python -m pytest -p no:cacheprovider -q tests/test_piecewise_parse.py tests/test_cpu_ref.py` on both revisions. With CPython 3.14.7, main had 64 passes and failed the loader assertion because it used `SourceFileLoader`. Main plus this PR passed all 65 tests. Each run also passed 1,870 subtests. With CPython 3.14.10, both revisions passed all 65 tests and 1,870 subtests. Running `python tests/test_piecewise_parse.py` directly passed all 26 tests in each of the four runs. MEASURED with synthetic inputs. AI-assisted (Codex).
KelboLive commented 2026-12-01 17:48:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T17:48:04Z.

Review: COMMENTED


Confirmed against current origin/main (0d4cbd589): pytest tests/test_piecewise_parse.py is 1 failed / 25 passed there and 26/26 here, while python -I tests/test_piecewise_parse.py is green on both. The diagnosis is right — tests/semantic/support.py:58-60 can only intercept the first import, and tests/conftest.py:30 used to win it, leaving SourceFileLoader. Mutation-checked: swapping import support back after import rdna2spirv turns the test red with the original assertion, so the ordering line is what carries the fix.

One thing worth saying in the comment: this installs ToolsFinder and sets sys.dont_write_bytecode = True (via support.py:55) for the whole pytest session, so every later tools/*.py import in unrelated files (test_wave_diff, test_mc_command, …) is compiled through PiecewiseLoader, not just the translator. I spot-checked code-object equality for ttpf_mips, mc_command, shader_disasm and ttpf_assets, and a 13-file tools-importing batch is green (196 passed, 12 skipped), so it is fine — but stating the wider scope in the comment would save the next reader the question.

Minor: the version range would read better as 3.13.15 than "3.12 to 3.14.7" if that is the actual boundary you measured, and the conftest now pays the support + spirv_mini_interp import on every run (test_wave_diff.py went 2.84 s → 3.44 s).

Ordering is not fragile: it passes alone, after test_wave_diff collects first, from tests/ as cwd, and with -p no:cacheprovider. Merge is clean, and the four commits main gained since the base touch only Rust and d3d12/docs/switches.md.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6136782750 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2905#pullrequestreview-6136782750). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T17:48:04Z. **Review:** COMMENTED --- Confirmed against current `origin/main` (`0d4cbd589`): `pytest tests/test_piecewise_parse.py` is **1 failed / 25 passed** there and **26/26** here, while `python -I tests/test_piecewise_parse.py` is green on both. The diagnosis is right — `tests/semantic/support.py:58-60` can only intercept the *first* import, and `tests/conftest.py:30` used to win it, leaving `SourceFileLoader`. Mutation-checked: swapping `import support` back after `import rdna2spirv` turns the test red with the original assertion, so the ordering line is what carries the fix. One thing worth saying in the comment: this installs `ToolsFinder` **and** sets `sys.dont_write_bytecode = True` (via `support.py:55`) for the whole pytest session, so every later `tools/*.py` import in unrelated files (`test_wave_diff`, `test_mc_command`, …) is compiled through `PiecewiseLoader`, not just the translator. I spot-checked code-object equality for `ttpf_mips`, `mc_command`, `shader_disasm` and `ttpf_assets`, and a 13-file tools-importing batch is green (196 passed, 12 skipped), so it is fine — but stating the wider scope in the comment would save the next reader the question. Minor: the version range would read better as `3.13.15` than "3.12 to 3.14.7" if that is the actual boundary you measured, and the conftest now pays the `support` + `spirv_mini_interp` import on every run (`test_wave_diff.py` went 2.84 s → 3.44 s). Ordering is not fragile: it passes alone, after `test_wave_diff` collects first, from `tests/` as cwd, and with `-p no:cacheprovider`. Merge is clean, and the four commits main gained since the base touch only Rust and `d3d12/docs/switches.md`.
fiestorra commented 2026-12-01 18:11:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-01T18:11:59Z.


Both notes taken.

The sys.dont_write_bytecode = True. You are right that it belongs in the description: importing support at conftest time does two things, not one - it puts ToolsFinder in sys.meta_path and it sets sys.dont_write_bytecode = True (support.py:55), and the second is a process-wide effect that now happens before any test runs rather than at the first translator import. It is the same setting the semantic tests already ran under, and it stops pytest from dropping __pycache__ into tree under test; I have added it to the body rather than leaving it for someone to discover from a stale .pyc directory.

The version range. Fair - the thing that makes it read oddly is that two ranges are involved (3.12 up to before 3.13.16, and 3.14 up to before 3.14.8), so neither 3.13.15 nor 3.14.7 alone is the boundary. The body now says exactly that instead of a shorthand that reads like one range.

And the ordering evidence is useful (alone, after test_wave_diff collects first, from tests/ as cwd, and with -p no:cacheprovider): that is the shape I could not check from here, because the failure only appears under pytest's conftest.

Measured here (CPython 3.14, where needed() is true): pytest tests/test_piecewise_parse.py 26 passed; the same file as python tests/test_piecewise_parse.py OK; the five files ci.yml runs for this area plus test_cpu_ref, 81 passed.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7320559545 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2905#issuecomment-7320559545). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-01T18:11:59Z. --- Both notes taken. **The `sys.dont_write_bytecode = True`.** You are right that it belongs in the description: importing `support` at conftest time does two things, not one - it puts `ToolsFinder` in `sys.meta_path` and it sets `sys.dont_write_bytecode = True` (`support.py:55`), and the second is a process-wide effect that now happens before any test runs rather than at the first translator import. It is the same setting the semantic tests already ran under, and it stops pytest from dropping `__pycache__` into `tree under test`; I have added it to the body rather than leaving it for someone to discover from a stale `.pyc` directory. **The version range.** Fair - the thing that makes it read oddly is that two ranges are involved (3.12 up to before 3.13.16, and 3.14 up to before 3.14.8), so neither `3.13.15` nor `3.14.7` alone is the boundary. The body now says exactly that instead of a shorthand that reads like one range. **And the ordering evidence is useful** (alone, after `test_wave_diff` collects first, from `tests/` as cwd, and with `-p no:cacheprovider`): that is the shape I could not check from here, because the failure only appears under pytest's conftest. Measured here (CPython 3.14, where `needed()` is true): `pytest tests/test_piecewise_parse.py` 26 passed; the same file as `python tests/test_piecewise_parse.py` OK; the five files `ci.yml` runs for this area plus `test_cpu_ref`, 81 passed. Written with an AI assistant.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2905
No description provided.