Skip to content

Commit 22dcd39

Browse files
authored
Merge pull request #135 from lagru/return-copy-in-dir
Make sure that `__dir__` returns new copies of `__all__`
2 parents 513f429 + df4f89a commit 22dcd39

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lazy_loader/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __getattr__(name):
9090
raise AttributeError(f"No {package_name} attribute {name}")
9191

9292
def __dir__():
93-
return __all__
93+
return list(__all__)
9494

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

lazy_loader/tests/test_lazy_loader.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ def test_lazy_attach():
104104
assert locls[k] == v
105105

106106

107+
def test_lazy_attach_returns_copies():
108+
_get, _dir, _all = lazy.attach(
109+
__name__, ["my_submodule", "another_submodule"], {"foo": ["some_attr"]}
110+
)
111+
assert _dir() is not _dir()
112+
assert _dir() == _all
113+
assert _dir() is not _all
114+
115+
expected = ["another_submodule", "my_submodule", "some_attr"]
116+
assert _dir() == expected
117+
assert _all == expected
118+
assert _dir() is not _all
119+
120+
_dir().append("modify_returned_list")
121+
assert _dir() == expected
122+
assert _all == expected
123+
assert _dir() is not _all
124+
125+
_all.append("modify_returned_all")
126+
assert _dir() == expected
127+
assert _all == [*expected, "modify_returned_all"]
128+
129+
107130
def test_attach_same_module_and_attr_name():
108131
from lazy_loader.tests import fake_pkg
109132

0 commit comments

Comments
 (0)