test_calibrated_texture_extra: patch ct.replace_retry so the #570 test works without numpy #2101

Open
github-bot wants to merge 1 commit from github-bot/gh-2101 into main AGit
Owner

@⁠Boost9898 on GitHub · 2026-11-25 20:44 UTC · original
Boost9898:fix/calibrated-texture-replace-cleanup → main · +1 −1 · 1 files · 1 commits · open

Claimed in the coordination issue as: #1608

What and why

#570 added test_export_file_replacement_failure_cleans_temporary_file to tests/test_calibrated_texture_extra.py. It fails on main db1a22d29 wherever numpy isn't installed (OSError not raised).

Without numpy, _load_calibrated_texture() runs calibrated_texture.py inside mock.patch.dict(sys.modules, {"numpy": fake_np}). On exit, patch.dict restores sys.modules, which also removes replace_retry, imported during that load. The test patches mock.patch("replace_retry.replace", ...), which imports and patches a fresh copy of replace_retry. ct.replace_retry is still the old one, so export_file replaces the file normally and nothing raises. Checked: with the fake numpy, ct.replace_retry is sys.modules["replace_retry"] is False.

The test now patches mock.patch.object(ct.replace_retry, "replace", ...), which is the module export_file calls, with or without numpy. tools/calibrated_texture.py is unchanged.

Tested

Apple M4, macOS 26.5.1, Python 3.14.8 and 3.12.10 (neither has numpy, so the stdlib shim path is used), CPU only, no game files.

  • tests/test_calibrated_texture_extra.py: the test fails on main db1a22d29. With this change, all 30 tests OK on both versions.
  • Negative control: with the tmp.unlink(...) cleanup in export_file removed, the test fails again, so it still checks the cleanup.
  • python -m compileall, git diff --check and scripts/check_no_game_data.py on the changed file: clean.

GPUs tested: n/a (no GPU tests involved)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator rule changes)
**@⁠Boost9898** on GitHub · 2026-11-25 20:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2101) `Boost9898:fix/calibrated-texture-replace-cleanup` → `main` · +1 −1 · 1 files · 1 commits · open Claimed in the coordination issue as: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ### What and why #570 added `test_export_file_replacement_failure_cleans_temporary_file` to `tests/test_calibrated_texture_extra.py`. It fails on `main` `db1a22d29` wherever numpy isn't installed (`OSError not raised`). Without numpy, `_load_calibrated_texture()` runs `calibrated_texture.py` inside `mock.patch.dict(sys.modules, {"numpy": fake_np})`. On exit, `patch.dict` restores `sys.modules`, which also removes `replace_retry`, imported during that load. The test patches `mock.patch("replace_retry.replace", ...)`, which imports and patches a fresh copy of `replace_retry`. `ct.replace_retry` is still the old one, so `export_file` replaces the file normally and nothing raises. Checked: with the fake numpy, `ct.replace_retry is sys.modules["replace_retry"]` is `False`. The test now patches `mock.patch.object(ct.replace_retry, "replace", ...)`, which is the module `export_file` calls, with or without numpy. `tools/calibrated_texture.py` is unchanged. ### Tested Apple M4, macOS 26.5.1, Python 3.14.8 and 3.12.10 (neither has numpy, so the stdlib shim path is used), CPU only, no game files. - `tests/test_calibrated_texture_extra.py`: the test fails on `main` `db1a22d29`. With this change, all 30 tests OK on both versions. - Negative control: with the `tmp.unlink(...)` cleanup in `export_file` removed, the test fails again, so it still checks the cleanup. - `python -m compileall`, `git diff --check` and `scripts/check_no_game_data.py` on the changed file: clean. GPUs tested: n/a (no GPU tests involved) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator rule changes)
Without numpy the test loads calibrated_texture inside mock.patch.dict(sys.modules),
which drops replace_retry from sys.modules again on exit. mock.patch("replace_retry.replace")
then imports and patches a second copy, export_file never sees the failure, and the
test added in #570 fails. Patch ct.replace_retry directly, which is the same module
with or without numpy.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:17:55 +00:00
Author
Owner

@⁠EdenBendheim on GitHub · 2026-11-26 05:17 UTC · original

Note

🤖 gpt-6.2-sol responding on behalf of EdenBendheim

Independent verification of head 4cfb025a080ca58927701721ac3042f476a5440c applied to current main a73a57e13 (macOS 27.3, Apple M1 Max, CPU-only synthetic fixtures, no numpy/game data).

  • Unmodified main: test_calibrated_texture_extra.py 29/30; reproduced test_export_file_replacement_failure_cleans_temporary_file failing with OSError not raised.
  • PR: 30/30 on Python 3.12.14 and 3.14.3.
  • Confirmed the fallback module instance differs from the module imported by name, so the revised mock targets the actual dependency.
  • Clean simulated merge, no-game-data and diff checks.

This also resolves one of the two pre-existing failures in the full standalone test run for #2193. No defects found.

**@⁠EdenBendheim** on GitHub · 2026-11-26 05:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2101#issuecomment-7183349239) > [!NOTE] > 🤖 **gpt-6.2-sol responding on behalf of EdenBendheim** Independent verification of head `4cfb025a080ca58927701721ac3042f476a5440c` applied to current main `a73a57e13` (macOS 27.3, Apple M1 Max, CPU-only synthetic fixtures, no numpy/game data). - Unmodified main: `test_calibrated_texture_extra.py` 29/30; reproduced `test_export_file_replacement_failure_cleans_temporary_file` failing with `OSError not raised`. - PR: 30/30 on Python 3.12.14 and 3.14.3. - Confirmed the fallback module instance differs from the module imported by name, so the revised mock targets the actual dependency. - Clean simulated merge, no-game-data and diff checks. This also resolves one of the two pre-existing failures in the full standalone test run for #2193. No defects found.
Author
Owner

@⁠Mby159 on GitHub · 2026-11-26 08:04 UTC · original

Note

🤖 AI-assisted (MiniMax Code, MiniMax-M3.1-Flash) — CPU-only verification run on a local Windows machine. These suites mock ct.native(), use synthetic fixtures only (no game data), and need no GPU or other external tooling.

Independent Windows verification of head 4cfb025a080ca58927701721ac3042f476a5440c applied to current main a73a57e1339be072baf5522351c8ec7f1482b56b.

Environment: Windows 11 (10.0.29712 SP0), AMD64, CPython 3.13.17, no NumPy (import numpy → ModuleNotFoundError).

Why this leg matters: the check job in .github/workflows/ci.yml runs on windows-latest and executes each tests/test_*.py suite as python -I "$t"; that leg has no NumPy, and this file is included in that loop, so it is exactly the configuration in which the file can go red. There is no completed CI run to cite for this head — its CI run has been sitting queued since 2026-11-25T20:44Z without starting (only no-game-data has completed, successfully) — so everything below was measured locally in the exact configuration that step uses (python -I, no NumPy).

Measured, same command in both cases (python -I tests/test_calibrated_texture_extra.py):

tree result
unmodified main a73a57e1339b Ran 30 tests → FAILED (failures=1)
with this PR's file Ran 30 tests → OK

The single main failure is test_export_file_replacement_failure_cleans_temporary_file → AssertionError: OSError not raised.

Why it fails, confirmed by direct probe on this tree:

  1. The module imports calibrated_texture inside mock.patch.dict(sys.modules, {"numpy": fake_np}); that import pulls in replace_retry, so replace_retry enters sys.modules inside the patch.dict block.
  2. Leaving the block restores sys.modules to its pre-block content, which removes replace_retry again — while calibrated_texture.replace_retry keeps the original module object.
  3. mock.patch("replace_retry.replace", ...) resolves its string target by importing replace_retry once more, yielding a different module object (ct.replace_retry is sys.modules["replace_retry"] → False), so the patch lands on an object ct.export_file never calls.
  4. The simulated OSError therefore never fires, and assertRaisesRegex fails with OSError not raised.

mock.patch.object(ct.replace_retry, "replace", ...) patches the object the code actually holds, so the failure path is genuinely exercised and the temporary-file cleanup assertion becomes meaningful.

I had derived this same one-line fix on my own tree before finding this PR, so this is a confirmation rather than a new proposal — the change matches what I measured and is correct as written. No defects found in it.

Caveat: the local interpreter is CPython 3.13.17 while the check leg pins 3.12, so that version axis is not covered here. The mechanism above is unittest.mock module-identity behaviour and does not depend on the interpreter version.

**@⁠Mby159** on GitHub · 2026-11-26 08:04 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2101#issuecomment-7186150257) > [!NOTE] > 🤖 **AI-assisted (MiniMax Code, MiniMax-M3.1-Flash)** — CPU-only verification run on a local Windows machine. These suites mock `ct.native()`, use synthetic fixtures only (no game data), and need no GPU or other external tooling. Independent **Windows** verification of head `4cfb025a080ca58927701721ac3042f476a5440c` applied to current main `a73a57e1339be072baf5522351c8ec7f1482b56b`. Environment: Windows 11 (10.0.29712 SP0), AMD64, CPython 3.13.17, **no NumPy** (`import numpy` → `ModuleNotFoundError`). Why this leg matters: the `check` job in `.github/workflows/ci.yml` runs on `windows-latest` and executes each `tests/test_*.py` suite as `python -I "$t"`; that leg has no NumPy, and this file is included in that loop, so it is exactly the configuration in which the file can go red. There is **no completed CI run to cite** for this head — its CI run has been sitting `queued` since 2026-11-25T20:44Z without starting (only `no-game-data` has completed, successfully) — so everything below was measured locally in the exact configuration that step uses (`python -I`, no NumPy). Measured, same command in both cases (`python -I tests/test_calibrated_texture_extra.py`): | tree | result | | --- | --- | | unmodified main `a73a57e1339b` | `Ran 30 tests` → `FAILED (failures=1)` | | with this PR's file | `Ran 30 tests` → `OK` | The single main failure is `test_export_file_replacement_failure_cleans_temporary_file` → `AssertionError: OSError not raised`. Why it fails, confirmed by direct probe on this tree: 1. The module imports `calibrated_texture` inside `mock.patch.dict(sys.modules, {"numpy": fake_np})`; that import pulls in `replace_retry`, so `replace_retry` enters `sys.modules` **inside** the patch.dict block. 2. Leaving the block restores `sys.modules` to its pre-block content, which **removes** `replace_retry` again — while `calibrated_texture.replace_retry` keeps the original module object. 3. `mock.patch("replace_retry.replace", ...)` resolves its string target by importing `replace_retry` once more, yielding a *different* module object (`ct.replace_retry is sys.modules["replace_retry"]` → `False`), so the patch lands on an object `ct.export_file` never calls. 4. The simulated `OSError` therefore never fires, and `assertRaisesRegex` fails with `OSError not raised`. `mock.patch.object(ct.replace_retry, "replace", ...)` patches the object the code actually holds, so the failure path is genuinely exercised and the temporary-file cleanup assertion becomes meaningful. I had derived this same one-line fix on my own tree before finding this PR, so this is a confirmation rather than a new proposal — the change matches what I measured and is correct as written. No defects found in it. Caveat: the local interpreter is CPython 3.13.17 while the `check` leg pins 3.12, so that version axis is not covered here. The mechanism above is `unittest.mock` module-identity behaviour and does not depend on the interpreter version.
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 18:09 UTC · original

Verified independently on main bd6ba97ab (macOS 27.2.1 arm64, Python 3.15.2, numpy absent, so the stdlib shim path runs), with the PR diff applied:

Before (unmodified main):

FAIL: test_export_file_replacement_failure_cleans_temporary_file
  File ".../tests/test_calibrated_texture_extra.py", line 2237
    with self.assertRaisesRegex(OSError, "simulated I/O write failure"):
AssertionError: OSError not raised
Ran 1 test ... FAILED

After: Ran 30 tests ... OK.

Cause confirmed: without the patch, ct.replace_retry is sys.modules["replace_retry"] is False — mock.patch.dict(sys.modules, {"numpy": fake_np}) in _load_calibrated_texture() removes the sibling modules imported during exec_module, so patching by module name hits a second copy.

Negative control: with tmp.unlink(missing_ok=True) removed from export_file, the fixed test fails again (FAILED), so it still checks the temp-file cleanup.

This matters for CI, not only locally: the check (Windows) and macos-python jobs run the stdlib loop before their pip install -r tests/requirements.txt, so numpy is absent there (stdlib-tests-linux installs numpy first and is unaffected). The change is test-only.

AI-assisted (verification run only).

**@⁠wavehs** on GitHub · 2026-11-26 18:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2101#issuecomment-7200127753) Verified independently on `main` `bd6ba97ab` (macOS 27.2.1 arm64, Python 3.15.2, numpy absent, so the stdlib shim path runs), with the PR diff applied: **Before** (unmodified main): ``` FAIL: test_export_file_replacement_failure_cleans_temporary_file File ".../tests/test_calibrated_texture_extra.py", line 2237 with self.assertRaisesRegex(OSError, "simulated I/O write failure"): AssertionError: OSError not raised Ran 1 test ... FAILED ``` **After**: `Ran 30 tests ... OK`. **Cause confirmed**: without the patch, `ct.replace_retry is sys.modules["replace_retry"]` is `False` — `mock.patch.dict(sys.modules, {"numpy": fake_np})` in `_load_calibrated_texture()` removes the sibling modules imported during `exec_module`, so patching by module name hits a second copy. **Negative control**: with `tmp.unlink(missing_ok=True)` removed from `export_file`, the fixed test fails again (`FAILED`), so it still checks the temp-file cleanup. This matters for CI, not only locally: the `check` (Windows) and `macos-python` jobs run the stdlib loop before their `pip install -r tests/requirements.txt`, so numpy is absent there (`stdlib-tests-linux` installs numpy first and is unaffected). The change is test-only. AI-assisted (verification run only).
Author
Owner

@⁠Andretuta on GitHub · 2026-11-27 06:24 UTC · original

I reproduced the failure on native Windows with CPython 3.13.14 and no NumPy. On main at d1dff7949, the replacement test failed with OSError not raised. With 4cfb025a0 merged in, all 30 tests in test_calibrated_texture_extra.py passed. The suite passed again in the two-worker runner from #2193 alongside the other Windows fixes.

AI-assisted testing.

**@⁠Andretuta** on GitHub · 2026-11-27 06:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2101#issuecomment-7211501790) I reproduced the failure on native Windows with CPython 3.13.14 and no NumPy. On `main` at `d1dff7949`, the replacement test failed with `OSError not raised`. With `4cfb025a0` merged in, all 30 tests in `test_calibrated_texture_extra.py` passed. The suite passed again in the two-worker runner from #2193 alongside the other Windows fixes. AI-assisted testing.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2101/head:github-bot/gh-2101'
git switch 'github-bot/gh-2101'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2101'
git switch 'github-bot/gh-2101'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2101'
git switch 'github-bot/gh-2101'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2101'
git switch 'main'
git merge --squash 'github-bot/gh-2101'
git switch 'main'
git merge --ff-only 'github-bot/gh-2101'
git switch 'main'
git merge 'github-bot/gh-2101'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.