Skip to content

[Backport maintenance/3.3.x] Avoid importing submodules sharing names with standard library modules #2703

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

Merged
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
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ What's New in astroid 3.3.10?
=============================
Release date: TBA

* Avoid importing submodules sharing names with standard library modules.

Closes #2684

What's New in astroid 3.3.9?
============================
Expand Down
2 changes: 1 addition & 1 deletion astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def find_module(
# module. Note that `find_spec` actually imports parent modules, so we want to make
# sure we only run this code for stuff that can be expected to be frozen. For now
# this is only stdlib.
if modname in sys.stdlib_module_names or (
if (modname in sys.stdlib_module_names and not processed) or (
processed and processed[0] in sys.stdlib_module_names
):
try:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,18 @@ def test_find_setuptools_pep660_editable_install():
with unittest.mock.patch.object(sys, "meta_path", new=[_EditableFinder]):
assert spec.find_spec(["example"])
assert spec.find_spec(["example", "subpackage"])


def test_no_import_done_for_submodule_sharing_std_lib_name() -> None:
sys.path.insert(0, resources.find("data"))
try:
with pytest.raises(ImportError):
spec._find_spec_with_path(
[resources.find("data")],
"trace",
("divide_by_zero", "trace"),
("divide_by_zero",),
resources.find("data/divide_by_zero"),
)
finally:
sys.path.pop(0)
1 change: 1 addition & 0 deletions tests/testdata/python3/data/divide_by_zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 / 0