Skip to content

Commit c2d2057

Browse files
Prevent special case for virtualenv's patching of distutils from catching other submodules (#1544)
1 parent 9aabf76 commit c2d2057

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Release date: TBA
1818
* Rename ``ModuleSpec`` -> ``module_type`` constructor parameter to match attribute
1919
name and improve typing. Use ``type`` instead.
2020

21+
* Fix a bug where in attempting to handle the patching of ``distutils`` by ``virtualenv``,
22+
library submodules called ``distutils`` (e.g. ``numpy.distutils``) were included also.
23+
24+
Refs PyCQA/pylint#6497
25+
2126

2227
What's New in astroid 2.11.5?
2328
=============================

astroid/interpreter/_import/spec.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from pathlib import Path
1515
from typing import List, NamedTuple, Optional, Sequence, Tuple
1616

17+
from astroid.modutils import EXT_LIB_DIRS
18+
1719
from . import util
1820

1921

@@ -150,7 +152,10 @@ def contribute_to_path(self, spec, processed):
150152
for p in sys.path
151153
if os.path.isdir(os.path.join(p, *processed))
152154
]
153-
elif spec.name == "distutils":
155+
elif spec.name == "distutils" and not any(
156+
spec.location.lower().startswith(ext_lib_dir.lower())
157+
for ext_lib_dir in EXT_LIB_DIRS
158+
):
154159
# virtualenv below 20.0 patches distutils in an unexpected way
155160
# so we just find the location of distutils that will be
156161
# imported to avoid spurious import-error messages

tests/unittest_regrtest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ def test_numpy_crash(self):
8181
inferred = callfunc.inferred()
8282
self.assertEqual(len(inferred), 1)
8383

84+
@unittest.skipUnless(HAS_NUMPY, "Needs numpy")
85+
def test_numpy_distutils(self):
86+
"""Special handling of virtualenv's patching of distutils shouldn't interfere
87+
with numpy.distutils.
88+
89+
PY312_PLUS -- This test will likely become unnecessary when Python 3.12 is
90+
numpy's minimum version. (numpy.distutils will be removed then.)
91+
"""
92+
node = extract_node(
93+
"""
94+
from numpy.distutils.misc_util import is_sequence
95+
is_sequence("ABC") #@
96+
"""
97+
)
98+
inferred = node.inferred()
99+
self.assertIsInstance(inferred[0], nodes.Const)
100+
84101
def test_nameconstant(self) -> None:
85102
# used to fail for Python 3.4
86103
builder = AstroidBuilder()

0 commit comments

Comments
 (0)