Repository navigation
Restore numpy in sys.modules after test_no_numpy_int_type - #5807
Open
zhaoruizhe81 wants to merge 1 commit into
Open
zhaoruizhe81 wants to merge 1 commit into
zhaoruizhe81 wants to merge 1 commit into
Conversation
TestNoNumpyIntegerBaseType.test_no_numpy_int_type pops numpy from sys.modules to simulate a no-numpy environment but never restores it, so any later import numpy in the same pytest process re-executes numpy/__init__.py and can fail with a RecursionError on recent numpy 2.x releases. Save the module entry before popping and restore it in a finally block; in a true no-numpy environment the restore branch is skipped. Fixes plotly#4852
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Link to issue
Fixes #4852
Description of change
TestNoNumpyIntegerBaseType.test_no_numpy_int_typepopsnumpyfromsys.modulesto simulate a no-numpy environment, but never restores it. This PR saves the module entry before popping and restores it in afinallyblock, so the test no longer leaks global state into the rest of the pytest process.The test's intent is fully preserved: inside the popped window
get_module("numpy", should_load=False)still returnsNone(it readssys.modules.get(name, None)in_plotly_utils/optional_imports.py), so_get_int_type()still returns(int,)and the assertion still exercises the no-numpy path. In a true no-numpy environmentget_modulereturnsNone, the restore branch is skipped, and behavior is unchanged.Demo
N/A - this is a test-infrastructure change with no user-facing behavior.
Testing strategy
Verified in an isolated venv (CPython 3.13.12, numpy 2.5.3, pandas 3.0.6, pytest 9.1.1, editable install of this repo) on
main@3f8a650:pytest.main(["-q", "tests/test_optional/test_utils/test_utils.py"])(all tests pass, exit code 0) and then executingimport numpyin that same process raisesRecursionError: maximum recursion depth exceeded, because the re-import re-executesnumpy/__init__.py. A minimalimport numpy; sys.modules.pop("numpy"); import numpyreproduces the same error.python -m pytest tests/test_optional/test_utils/test_utils.py -q-> 26 passed.import numpyafterwards succeeds, andsys.modules["numpy"]is the very same module object as before the test flow (ischeck isTrue), withnp.integerintact.python -m pytest tests/test_optional/ -q --ignore=tests/test_optional/test_kaleido/test_kaleido.py --ignore=tests/test_optional/test_px/test_imshow.py->3 failed, 1241 passed, 26 skipped, 4 xfailed, identical before and after the change. The two ignored modules require optional deps not installed locally (pdfrw,xarray); the 3 failures are pre-existing environment-related failures intests/test_optional/test_offline/test_offline.py, present in the unmodified baseline as well, so there is no regression from this change.Additional information (optional)
This is the root cause of the "odd recursion error" in #4622, where the workaround was to move the affected test above this one. Popping
numpyfromsys.moduleswithout restoring it makes the file order-fragile: any test added aftertest_no_numpy_int_typethat importsnumpywill break.If maintainers prefer, an equivalent fix is
mock.patch.dict(sys.modules, {"numpy": None}), the pattern already used intests/test_io/test_renderers.py- happy to switch.Guidelines