Skip to content

Commit

Permalink
Refuse star imports in stub loader
Browse files Browse the repository at this point in the history
Block these types of imports until scientific-python#94 is resolved.
  • Loading branch information
stefanv committed Mar 15, 2024
1 parent e750ba6 commit 45bb462
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lazy_loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,13 @@ def visit_ImportFrom(self, node: ast.ImportFrom):
)
if node.module:
attrs: list = self._submod_attrs.setdefault(node.module, [])
attrs.extend(alias.name for alias in node.names)
aliases = [alias.name for alias in node.names]
if "*" in aliases:
raise ValueError(
"lazy stub loader does not support star import "
f"`from {node.module} import *`"
)
attrs.extend(aliases)
else:
self._submodules.update(alias.name for alias in node.names)

Expand Down
5 changes: 5 additions & 0 deletions lazy_loader/tests/test_lazy_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def test_stub_loading_errors(tmp_path):
with pytest.raises(ValueError, match="Cannot load imports from non-existent stub"):
lazy.attach_stub("name", "not a file")

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


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

0 comments on commit 45bb462

Please sign in to comment.