Skip to content

lazy_import as a context manager #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update sys.modules test
  • Loading branch information
maurosilber committed Jul 13, 2022
commit beeb504ee05102134ca3c860b8a372674f152e77
19 changes: 13 additions & 6 deletions tests/test_lazy_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,25 @@ def test_import_error():
import anything_not_real # noqa: F401


def test_lazy_import_impact_on_sys_modules():
math = lazy.load("math")
anything_not_real = lazy.load("anything_not_real")
def test_builtin_is_in_sys_modules():
with lazy_import():
import math

assert isinstance(math, types.ModuleType)
assert "math" in sys.modules

math.pi # trigger load of math

assert isinstance(math, types.ModuleType)
assert "math" in sys.modules
assert isinstance(anything_not_real, lazy.DelayedImportErrorModule)
assert "anything_not_real" not in sys.modules


def test_non_builtin_is_in_sys_modules():
# only do this if numpy is installed
pytest.importorskip("numpy")
np = lazy.load("numpy")
with lazy_import():
import numpy as np

assert isinstance(np, types.ModuleType)
assert "numpy" in sys.modules

Expand Down