diff options
author | Dan Schult <dschult@colgate.edu> | 2022-07-27 09:05:58 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-27 15:05:58 +0200 |
commit | 6e1557900da3c2cc8455449e7e76b07f7c36b32a (patch) | |
tree | 34e42004c1a8b74dc2f444b13e783ba758164019 /numpy/tests/test_lazyloading.py | |
parent | 45bc13e6d922690eea43b9d807d476e0f243f836 (diff) | |
download | numpy-6e1557900da3c2cc8455449e7e76b07f7c36b32a.tar.gz |
ENH: allow importlib.LazyLoader to work with numpy and add test of this (#22045)
Diffstat (limited to 'numpy/tests/test_lazyloading.py')
-rw-r--r-- | numpy/tests/test_lazyloading.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/numpy/tests/test_lazyloading.py b/numpy/tests/test_lazyloading.py new file mode 100644 index 000000000..f31a4eab7 --- /dev/null +++ b/numpy/tests/test_lazyloading.py @@ -0,0 +1,38 @@ +import sys +import importlib +from importlib.util import LazyLoader, find_spec, module_from_spec +import pytest + + +# Warning raised by _reload_guard() in numpy/__init__.py +@pytest.mark.filterwarnings("ignore:The NumPy module was reloaded") +def test_lazy_load(): + # gh-22045. lazyload doesn't import submodule names into the namespace + # muck with sys.modules to test the importing system + old_numpy = sys.modules.pop("numpy") + + numpy_modules = {} + for mod_name, mod in list(sys.modules.items()): + if mod_name[:6] == "numpy.": + numpy_modules[mod_name] = mod + sys.modules.pop(mod_name) + + try: + # create lazy load of numpy as np + spec = find_spec("numpy") + module = module_from_spec(spec) + sys.modules["numpy"] = module + loader = LazyLoader(spec.loader) + loader.exec_module(module) + np = module + + # test a subpackage import + from numpy.lib import recfunctions + + # test triggering the import of the package + np.ndarray + + finally: + if old_numpy: + sys.modules["numpy"] = old_numpy + sys.modules.update(numpy_modules) |