Skip to content

Commit 4a10a8b

Browse files
Avoid importing submodules sharing names with standard library modules (#2702) (#2703)
(cherry picked from commit 9f8faeb) Co-authored-by: Jacob Walls <[email protected]>
1 parent 96e0327 commit 4a10a8b

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
@@ -13,7 +13,9 @@ What's New in astroid 3.3.10?
1313
=============================
1414
Release date: TBA
1515

16+
* Avoid importing submodules sharing names with standard library modules.
1617

18+
Closes #2684
1719

1820
What's New in astroid 3.3.9?
1921
============================

astroid/interpreter/_import/spec.py

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

tests/test_modutils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,18 @@ def test_find_setuptools_pep660_editable_install():
597597
with unittest.mock.patch.object(sys, "meta_path", new=[_EditableFinder]):
598598
assert spec.find_spec(["example"])
599599
assert spec.find_spec(["example", "subpackage"])
600+
601+
602+
def test_no_import_done_for_submodule_sharing_std_lib_name() -> None:
603+
sys.path.insert(0, resources.find("data"))
604+
try:
605+
with pytest.raises(ImportError):
606+
spec._find_spec_with_path(
607+
[resources.find("data")],
608+
"trace",
609+
("divide_by_zero", "trace"),
610+
("divide_by_zero",),
611+
resources.find("data/divide_by_zero"),
612+
)
613+
finally:
614+
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)