|
| 1 | +import sys |
| 2 | +import importlib |
| 3 | +import types |
| 4 | +import pytest |
| 5 | + |
| 6 | +import lazy_loader as lazy |
| 7 | + |
| 8 | + |
| 9 | +def test_lazy_import_basics(): |
| 10 | + math = lazy.load("math") |
| 11 | + anything_not_real = lazy.load("anything_not_real") |
| 12 | + |
| 13 | + # Now test that accessing attributes does what it should |
| 14 | + assert math.sin(math.pi) == pytest.approx(0, 1e-6) |
| 15 | + # poor-mans pytest.raises for testing errors on attribute access |
| 16 | + try: |
| 17 | + anything_not_real.pi |
| 18 | + assert False # Should not get here |
| 19 | + except ModuleNotFoundError: |
| 20 | + pass |
| 21 | + assert isinstance(anything_not_real, lazy.DelayedImportErrorModule) |
| 22 | + # see if it changes for second access |
| 23 | + try: |
| 24 | + anything_not_real.pi |
| 25 | + assert False # Should not get here |
| 26 | + except ModuleNotFoundError: |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +def test_lazy_import_impact_on_sys_modules(): |
| 31 | + math = lazy.load("math") |
| 32 | + anything_not_real = lazy.load("anything_not_real") |
| 33 | + |
| 34 | + assert type(math) == types.ModuleType |
| 35 | + assert "math" in sys.modules |
| 36 | + assert type(anything_not_real) == lazy.DelayedImportErrorModule |
| 37 | + assert "anything_not_real" not in sys.modules |
| 38 | + |
| 39 | + # only do this if numpy is installed |
| 40 | + np_test = pytest.importorskip("numpy") |
| 41 | + np = lazy.load("numpy") |
| 42 | + assert type(np) == types.ModuleType |
| 43 | + assert "numpy" in sys.modules |
| 44 | + |
| 45 | + np.pi # trigger load of numpy |
| 46 | + |
| 47 | + assert type(np) == types.ModuleType |
| 48 | + assert "numpy" in sys.modules |
| 49 | + |
| 50 | + |
| 51 | +def test_lazy_import_nonbuiltins(): |
| 52 | + sp = lazy.load("scipy") |
| 53 | + np = lazy.load("numpy") |
| 54 | + if isinstance(sp, lazy.DelayedImportErrorModule): |
| 55 | + try: |
| 56 | + sp.pi |
| 57 | + assert False |
| 58 | + except ModuleNotFoundError: |
| 59 | + pass |
| 60 | + elif isinstance(np, lazy.DelayedImportErrorModule): |
| 61 | + try: |
| 62 | + np.sin(np.pi) |
| 63 | + assert False |
| 64 | + except ModuleNotFoundError: |
| 65 | + pass |
| 66 | + else: |
| 67 | + assert np.sin(sp.pi) == pytest.approx(0, 1e-6) |
| 68 | + |
| 69 | + |
| 70 | +def test_lazy_attach(): |
| 71 | + name = "mymod" |
| 72 | + submods = ["mysubmodule", "anothersubmodule"] |
| 73 | + myall = {"not_real_submod": ["some_var_or_func"]} |
| 74 | + |
| 75 | + locls = { |
| 76 | + "attach": lazy.attach, |
| 77 | + "name": name, |
| 78 | + "submods": submods, |
| 79 | + "myall": myall, |
| 80 | + } |
| 81 | + s = "__getattr__, __lazy_dir__, __all__ = attach(name, submods, myall)" |
| 82 | + |
| 83 | + exec(s, {}, locls) |
| 84 | + expected = { |
| 85 | + "attach": lazy.attach, |
| 86 | + "name": name, |
| 87 | + "submods": submods, |
| 88 | + "myall": myall, |
| 89 | + "__getattr__": None, |
| 90 | + "__lazy_dir__": None, |
| 91 | + "__all__": None, |
| 92 | + } |
| 93 | + assert locls.keys() == expected.keys() |
| 94 | + for k, v in expected.items(): |
| 95 | + if v is not None: |
| 96 | + assert locls[k] == v |
0 commit comments