Skip to content

Run pre-commit hooks #3

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

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 6 additions & 10 deletions lazy_loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"""
import importlib
import importlib.util
import types
import os
import sys
import types


def attach(package_name, submodules=None, submod_attrs=None):
Expand Down Expand Up @@ -66,19 +66,17 @@ def attach(package_name, submodules=None, submod_attrs=None):

def __getattr__(name):
if name in submodules:
return importlib.import_module(f'{package_name}.{name}')
return importlib.import_module(f"{package_name}.{name}")
elif name in attr_to_modules:
submod = importlib.import_module(
f'{package_name}.{attr_to_modules[name]}'
)
submod = importlib.import_module(f"{package_name}.{attr_to_modules[name]}")
return getattr(submod, name)
else:
raise AttributeError(f'No {package_name} attribute {name}')
raise AttributeError(f"No {package_name} attribute {name}")

def __dir__():
return __all__

if os.environ.get('EAGER_IMPORT', ''):
if os.environ.get("EAGER_IMPORT", ""):
for attr in set(attr_to_modules.keys()) | submodules:
__getattr__(attr)

Expand Down Expand Up @@ -171,6 +169,4 @@ def __getattribute__(self, attr):
if attr in ["__class__"]:
return super().__getattribute__("__class__")

raise ModuleNotFoundError(
f"No module named '{spec.name}'"
)
raise ModuleNotFoundError(f"No module named '{spec.name}'")
12 changes: 6 additions & 6 deletions tests/test_lazy_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import importlib
import types

import pytest

import lazy_loader as lazy
Expand Down Expand Up @@ -31,20 +31,20 @@ def test_lazy_import_impact_on_sys_modules():
math = lazy.load("math")
anything_not_real = lazy.load("anything_not_real")

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

# only do this if numpy is installed
np_test = pytest.importorskip("numpy")
pytest.importorskip("numpy")
np = lazy.load("numpy")
assert type(np) == types.ModuleType
assert isinstance(np, types.ModuleType)
assert "numpy" in sys.modules

np.pi # trigger load of numpy

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


Expand Down