You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Include pytz in the auto-stubbed import prefixes for both delete-account tests that import routers.users.
Clear any existing modules under the stubbed prefixes before installing each test meta-path finder, so stale sys.modules entries from earlier tests cannot bypass the local _StubFinder.
Why
test_delete_account_purge_storage.py and test_delete_account_stripe_cancel.py both import routers.users, which has a broad import graph. In a lightweight Windows test environment, collection failed first on missing pytz. In broader collect-only runs, earlier tests can also leave stale database.* or utils.* entries in sys.modules; because importlib returns existing modules before consulting these tests' finders, stale modules can break the router import before the deletion assertions run.
This keeps both delete-account import sandboxes deterministic and scoped to the namespaces they already declare as stubbed.
This PR makes two targeted changes to test_delete_account_purge_storage.py to fix flaky collection on Windows minimal venvs: it adds 'pytz' to the auto-stub prefix list so importing routers.users no longer requires the optional package, and it sweeps sys.modules clean of every stubbed namespace before installing the _StubFinder, preventing stale mocks left by earlier test files from bypassing the finder.
'pytz' is appended to _STUB_PREFIXES, eliminating the ModuleNotFoundError that surfaced on Windows when the minimal venv did not include the optional timezone library.
A new module-level helper _remove_module_tree is called for every stub prefix before the _StubFinder is inserted into sys.meta_path, making the import sandbox deterministic regardless of test-collection order.
The identical sister file test_delete_account_stripe_cancel.py uses the same _StubFinder / _STUB_PREFIXES pattern to import routers.users and is not updated, leaving the same Windows pytz gap and stale-modules vulnerability unfixed there.
Confidence Score: 4/5
Safe to merge for purge_storage tests; however the same Windows pytz failure and stale-mock bypass remain unfixed in the sister file test_delete_account_stripe_cancel.py.
The two changes to test_delete_account_purge_storage.py are correct and well-scoped. The gap is that the identical import-sandbox pattern in test_delete_account_stripe_cancel.py does not receive the same pytz stub entry or the pre-install sys.modules sweep, leaving it vulnerable to the same Windows collection failure and stale-module bypass that motivated this PR.
backend/tests/unit/test_delete_account_stripe_cancel.py — identical stub pattern without the pytz entry and without the _remove_module_tree sweep.
Adds pytz to the stub prefix list and introduces a pre-install sys.modules sweep to clear stale mocks left by earlier test files; logic is correct for this file, but the identical sister file test_delete_account_stripe_cancel.py does not receive the same fixes.
Comments Outside Diff (1)
backend/tests/unit/test_delete_account_purge_storage.py, line 38-53 (link)
Sister file test_delete_account_stripe_cancel.py has the same unfixed gaps
test_delete_account_stripe_cancel.py is a near-identical copy of this file: it uses the same _StubFinder / _STUB_PREFIXES pattern, it imports routers.users, and that module's import chain pulls in pytz. On a minimal Windows venv where pytz is absent, running stripe_cancel in isolation hits the same collection failure that this PR fixes here. That file also lacks the _remove_module_tree sweep, so any stale database.* or utils.* mock left by test_byok_security.py can bypass its _StubFinder for the same reason described in the PR.
The two fixes applied here ('pytz' in _STUB_PREFIXES and the pre-install sys.modules clear) should be mirrored in test_delete_account_stripe_cancel.py.
The Greptile P1 summary was based on the earlier commit that only updated test_delete_account_purge_storage.py.
Addressed in the current head 534ad233: the same pytz stub entry and _remove_module_tree(...) pre-install cleanup are now mirrored in test_delete_account_stripe_cancel.py as well. I rechecked the PR diff and both delete-account test files carry the same import-sandbox fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pytzin the auto-stubbed import prefixes for both delete-account tests that importrouters.users.sys.modulesentries from earlier tests cannot bypass the local_StubFinder.Why
test_delete_account_purge_storage.pyandtest_delete_account_stripe_cancel.pyboth importrouters.users, which has a broad import graph. In a lightweight Windows test environment, collection failed first on missingpytz. In broader collect-only runs, earlier tests can also leave staledatabase.*orutils.*entries insys.modules; because importlib returns existing modules before consulting these tests' finders, stale modules can break the router import before the deletion assertions run.This keeps both delete-account import sandboxes deterministic and scoped to the namespaces they already declare as stubbed.
Testing
python -m pytest tests\unit\test_delete_account_purge_storage.py -q-> 5 passedpython -m pytest tests\unit\test_delete_account_stripe_cancel.py -q-> 3 passedpython -m pytest tests\unit\test_delete_account_purge_storage.py tests\unit\test_delete_account_stripe_cancel.py -q-> 8 passedpython -m pytest tests\unit\test_byok_security.py tests\unit\test_delete_account_purge_storage.py tests\unit\test_delete_account_stripe_cancel.py --collect-only -q -x-> 89 tests collectedpython -m black --line-length 120 --skip-string-normalization tests\unit\test_delete_account_purge_storage.py tests\unit\test_delete_account_stripe_cancel.py --checkpython -m py_compile tests\unit\test_delete_account_purge_storage.py tests\unit\test_delete_account_stripe_cancel.pygit diff --check origin/main...HEAD