Skip to content

Commit

Permalink
ENH: allow importlib.LazyLoader to work with numpy and add test of th…
Browse files Browse the repository at this point in the history
…is (#22045)
  • Loading branch information
dschult committed Jul 27, 2022
1 parent 45bc13e commit 6e15579
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions numpy/core/__init__.py
Expand Up @@ -75,8 +75,10 @@
from . import fromnumeric
from .fromnumeric import *
from . import defchararray as char
from . import records
from . import records as rec
from .records import record, recarray, format_parser
# Note: module name memmap is overwritten by a class with same name
from .memmap import *
from .defchararray import chararray
from . import function_base
Expand Down
18 changes: 18 additions & 0 deletions numpy/lib/__init__.py
Expand Up @@ -21,6 +21,24 @@
from . import scimath as emath

# Private submodules
# load module names. See https://github.com/networkx/networkx/issues/5838
from . import type_check
from . import index_tricks
from . import function_base
from . import nanfunctions
from . import shape_base
from . import stride_tricks
from . import twodim_base
from . import ufunclike
from . import histograms
from . import polynomial
from . import utils
from . import arraysetops
from . import npyio
from . import arrayterator
from . import arraypad
from . import _version

from .type_check import *
from .index_tricks import *
from .function_base import *
Expand Down
1 change: 1 addition & 0 deletions numpy/matrixlib/__init__.py
@@ -1,6 +1,7 @@
"""Sub-package containing the matrix class and related functions.
"""
from . import defmatrix
from .defmatrix import *

__all__ = defmatrix.__all__
Expand Down
1 change: 1 addition & 0 deletions numpy/testing/__init__.py
Expand Up @@ -7,6 +7,7 @@
"""
from unittest import TestCase

from . import _private
from ._private.utils import *
from ._private.utils import (_assert_valid_refcount, _gen_alignment_data)
from ._private import extbuild, decorators as dec
Expand Down
38 changes: 38 additions & 0 deletions 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)

0 comments on commit 6e15579

Please sign in to comment.