Skip to content

Commit cb5a467

Browse files
authored
Refuse star imports in stub loader (#101)
Block these types of imports until #94 is resolved.
1 parent e750ba6 commit cb5a467

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lazy_loader/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,13 @@ def visit_ImportFrom(self, node: ast.ImportFrom):
296296
)
297297
if node.module:
298298
attrs: list = self._submod_attrs.setdefault(node.module, [])
299-
attrs.extend(alias.name for alias in node.names)
299+
aliases = [alias.name for alias in node.names]
300+
if "*" in aliases:
301+
raise ValueError(
302+
"lazy stub loader does not support star import "
303+
f"`from {node.module} import *`"
304+
)
305+
attrs.extend(aliases)
300306
else:
301307
self._submodules.update(alias.name for alias in node.names)
302308

lazy_loader/tests/test_lazy_loader.py

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ def test_stub_loading_errors(tmp_path):
153153
with pytest.raises(ValueError, match="Cannot load imports from non-existent stub"):
154154
lazy.attach_stub("name", "not a file")
155155

156+
stub2 = tmp_path / "stub2.pyi"
157+
stub2.write_text("from .mod import *\n")
158+
with pytest.raises(ValueError, match=".*does not support star import"):
159+
lazy.attach_stub("name", str(stub2))
160+
156161

157162
def test_require_kwarg():
158163
have_importlib_metadata = importlib.util.find_spec("importlib.metadata") is not None

0 commit comments

Comments
 (0)