Skip to content

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

Merged
Andretuta merged 1 commit into
ProjectLeonida:mainfrom
Boost9898:fix/calibrated-texture-replace-cleanup
Nov 27, 2026
Merged

Andretuta merged 1 commit into
ProjectLeonida:mainfrom
Boost9898:fix/calibrated-texture-replace-cleanup

Conversation

@Boost9898

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #1608 (comment)

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)

…d_texture holds

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 ProjectLeonida#570 fails. Patch ct.replace_retry directly, which is the same module
with or without numpy.
@EdenBendheim

Copy link
Copy Markdown
Contributor

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.

@Mby159

Mby159 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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.

@wavehs

wavehs commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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).

@Andretuta

Copy link
Copy Markdown
Collaborator

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
Andretuta merged commit c513de8 into ProjectLeonida:main Nov 27, 2026
4 of 8 checks passed
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.

5 participants