Skip to content

Commit 9f8faeb

Browse files
Avoid importing submodules sharing names with standard library modules (#2702)
1 parent 291c3a5 commit 9f8faeb

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ What's New in astroid 3.3.10?
3737
=============================
3838
Release date: TBA
3939

40+
* Avoid importing submodules sharing names with standard library modules.
4041

42+
Closes #2684
4143

4244

4345
What's New in astroid 3.3.9?

astroid/interpreter/_import/spec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def find_module(
174174
# module. Note that `find_spec` actually imports parent modules, so we want to make
175175
# sure we only run this code for stuff that can be expected to be frozen. For now
176176
# this is only stdlib.
177-
if modname in sys.stdlib_module_names or (
177+
if (modname in sys.stdlib_module_names and not processed) or (
178178
processed and processed[0] in sys.stdlib_module_names
179179
):
180180
try:

tests/test_modutils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,18 @@ def test_find_setuptools_pep660_editable_install():
628628
with unittest.mock.patch.object(sys, "meta_path", new=[_EditableFinder]):
629629
assert spec.find_spec(["example"])
630630
assert spec.find_spec(["example", "subpackage"])
631+
632+
633+
def test_no_import_done_for_submodule_sharing_std_lib_name() -> None:
634+
sys.path.insert(0, resources.find("data"))
635+
try:
636+
with pytest.raises(ImportError):
637+
spec._find_spec_with_path(
638+
[resources.find("data")],
639+
"trace",
640+
("divide_by_zero", "trace"),
641+
("divide_by_zero",),
642+
resources.find("data/divide_by_zero"),
643+
)
644+
finally:
645+
sys.path.pop(0)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1 / 0

0 commit comments

Comments
 (0)