Skip to content

Commit dbcd1bc

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Prevent special case for virtualenv's patching of distutils from catching other submodules (#1544)
1 parent 5f30afe commit dbcd1bc

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
@@ -7,6 +7,11 @@ What's New in astroid 2.12.0?
77
Release date: TBA
88

99

10+
* Fix a bug where in attempting to handle the patching of ``distutils`` by ``virtualenv``,
11+
library submodules called ``distutils`` (e.g. ``numpy.distutils``) were included also.
12+
13+
Refs PyCQA/pylint#6497
14+
1015

1116
What's New in astroid 2.11.5?
1217
=============================

astroid/interpreter/_import/spec.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from functools import lru_cache
1414
from pathlib import Path
1515

16+
from astroid.modutils import EXT_LIB_DIRS
17+
1618
from . import util
1719

1820
ModuleType = enum.Enum(
@@ -149,7 +151,10 @@ def contribute_to_path(self, spec, processed):
149151
for p in sys.path
150152
if os.path.isdir(os.path.join(p, *processed))
151153
]
152-
elif spec.name == "distutils":
154+
elif spec.name == "distutils" and not any(
155+
spec.location.lower().startswith(ext_lib_dir.lower())
156+
for ext_lib_dir in EXT_LIB_DIRS
157+
):
153158
# virtualenv below 20.0 patches distutils in an unexpected way
154159
# so we just find the location of distutils that will be
155160
# 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)