Skip to content

Commit 7b6c02b

Browse files
committed
fix: Remove unused guard
__getattr__() is called on missing values. Actually existing values will be returned without hitting this method.
1 parent 21e5d04 commit 7b6c02b

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

src/lazy_loader/__init__.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,13 @@ def __init__(self, frame_data, *args, message, **kwargs):
106106
super().__init__(*args, **kwargs)
107107

108108
def __getattr__(self, x):
109-
if x in ("__class__", "__file__", "__frame_data", "__message"):
110-
super().__getattr__(x)
111-
else:
112-
fd = self.__frame_data
113-
raise ModuleNotFoundError(
114-
f"{self.__message}\n\n"
115-
"This error is lazily reported, having originally occurred in\n"
116-
f" File {fd['filename']}, line {fd['lineno']}, in {fd['function']}\n\n"
117-
f"----> {''.join(fd['code_context'] or '').strip()}"
118-
)
109+
fd = self.__frame_data
110+
raise ModuleNotFoundError(
111+
f"{self.__message}\n\n"
112+
"This error is lazily reported, having originally occurred in\n"
113+
f" File {fd['filename']}, line {fd['lineno']}, in {fd['function']}\n\n"
114+
f"----> {''.join(fd['code_context'] or '').strip()}"
115+
)
119116

120117

121118
def load(fullname, *, require=None, error_on_import=False, suppress_warning=False):

tests/test_lazy_loader.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@
1010
import lazy_loader as lazy
1111

1212

13+
@pytest.fixture
14+
def clean_fake_pkg():
15+
yield
16+
sys.modules.pop("tests.fake_pkg.some_func", None)
17+
sys.modules.pop("tests.fake_pkg", None)
18+
sys.modules.pop("tests", None)
19+
20+
21+
@pytest.mark.parametrize("attempt", [1, 2])
22+
def test_cleanup_fixture(clean_fake_pkg, attempt):
23+
assert "tests.fake_pkg" not in sys.modules
24+
assert "tests.fake_pkg.some_func" not in sys.modules
25+
from tests import fake_pkg
26+
27+
assert "tests.fake_pkg" in sys.modules
28+
assert "tests.fake_pkg.some_func" not in sys.modules
29+
assert isinstance(fake_pkg.some_func, types.FunctionType)
30+
assert "tests.fake_pkg.some_func" in sys.modules
31+
32+
1333
def test_lazy_import_basics():
1434
math = lazy.load("math")
1535
anything_not_real = lazy.load("anything_not_real")
@@ -127,18 +147,24 @@ def test_lazy_attach_returns_copies():
127147
assert _all == [*expected, "modify_returned_all"]
128148

129149

130-
def test_attach_same_module_and_attr_name():
131-
from tests import fake_pkg
150+
@pytest.mark.parametrize("eager_import", [False, True])
151+
def test_attach_same_module_and_attr_name(clean_fake_pkg, eager_import):
152+
env = {}
153+
if eager_import:
154+
env["EAGER_IMPORT"] = "1"
132155

133-
# Grab attribute twice, to ensure that importing it does not
134-
# override function by module
135-
assert isinstance(fake_pkg.some_func, types.FunctionType)
136-
assert isinstance(fake_pkg.some_func, types.FunctionType)
156+
with mock.patch.dict(os.environ, env):
157+
from tests import fake_pkg
158+
159+
# Grab attribute twice, to ensure that importing it does not
160+
# override function by module
161+
assert isinstance(fake_pkg.some_func, types.FunctionType)
162+
assert isinstance(fake_pkg.some_func, types.FunctionType)
137163

138-
# Ensure imports from submodule still work
139-
from tests.fake_pkg.some_func import some_func
164+
# Ensure imports from submodule still work
165+
from tests.fake_pkg.some_func import some_func
140166

141-
assert isinstance(some_func, types.FunctionType)
167+
assert isinstance(some_func, types.FunctionType)
142168

143169

144170
FAKE_STUB = """

0 commit comments

Comments
 (0)