Skip to content
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 @@ -37,7 +37,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 @@ -174,7 +174,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 @@ -628,3 +628,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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have preferred to find a public interface to test, but after almost an hour of fiddling I threw in the towel.

[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
Loading