Skip to content
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

Refuse star imports in stub loader #101

Merged
merged 1 commit into from
Mar 22, 2024
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
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
Loading