From d973f3bd9305934eb8e46d73c0d596e6b16a3968 Mon Sep 17 00:00:00 2001 From: zhaoruizhe Date: Sat, 10 Oct 2026 22:09:25 +0800 Subject: [PATCH] Restore numpy in sys.modules after test_no_numpy_int_type 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 #4852 --- CHANGELOG.md | 1 + tests/test_optional/test_utils/test_utils.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c92c3ea6d64..a5fe864cc10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased ### Fixed +- Fix the test suite leaving `numpy` removed from `sys.modules` after `test_no_numpy_int_type` runs, which broke any later test importing `numpy` in the same pytest process [[#4852](https://github.com/plotly/plotly.py/issues/4852)] - Fix `mpl_to_plotly` tick marker mirroring, axis positioning, and trace visibility for twinned and overlaid subplots (such as `twinx` and `twiny`) [[#5310](https://github.com/plotly/plotly.py/pull/5310)], with thanks to @robertoffmoura for the contribution! - Fix `mpl_to_plotly` losing matplotlib axis line colors by exporting the color of each axis's displayed spine to the plotly x and y axis `linecolor` [[#5311](https://github.com/plotly/plotly.py/pull/5311)], with thanks to @robertoffmoura for the contribution! - Fix concurrent first access to lazily initialized graph object properties, which could raise `ValueError("Invalid value")` [[#5691](https://github.com/plotly/plotly.py/pull/5691)], with thanks to @hb1915 for the contribution! diff --git a/tests/test_optional/test_utils/test_utils.py b/tests/test_optional/test_utils/test_utils.py index d3f2a4be160..b7091c3356d 100644 --- a/tests/test_optional/test_utils/test_utils.py +++ b/tests/test_optional/test_utils/test_utils.py @@ -491,10 +491,15 @@ def test_no_numpy_int_type(self): from _plotly_utils.optional_imports import get_module np = get_module("numpy", should_load=False) + saved_numpy = None if np: - sys.modules.pop("numpy") + saved_numpy = sys.modules.pop("numpy") - int_type_tuple = _get_int_type() - expected_tuple = (int,) + try: + int_type_tuple = _get_int_type() + expected_tuple = (int,) - self.assertEqual(int_type_tuple, expected_tuple) + self.assertEqual(int_type_tuple, expected_tuple) + finally: + if saved_numpy is not None: + sys.modules["numpy"] = saved_numpy