Skip to content

Commit

Permalink
Prevent special case for virtualenv's patching of distutils from …
Browse files Browse the repository at this point in the history
…catching other submodules (#1544)
  • Loading branch information
jacobtylerwalls authored and Pierre-Sassoulas committed May 9, 2022
1 parent 5f30afe commit dbcd1bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ChangeLog
Expand Up @@ -7,6 +7,11 @@ What's New in astroid 2.12.0?
Release date: TBA


* Fix a bug where in attempting to handle the patching of ``distutils`` by ``virtualenv``,
library submodules called ``distutils`` (e.g. ``numpy.distutils``) were included also.

Refs PyCQA/pylint#6497


What's New in astroid 2.11.5?
=============================
Expand Down
7 changes: 6 additions & 1 deletion astroid/interpreter/_import/spec.py
Expand Up @@ -13,6 +13,8 @@
from functools import lru_cache
from pathlib import Path

from astroid.modutils import EXT_LIB_DIRS

from . import util

ModuleType = enum.Enum(
Expand Down Expand Up @@ -149,7 +151,10 @@ def contribute_to_path(self, spec, processed):
for p in sys.path
if os.path.isdir(os.path.join(p, *processed))
]
elif spec.name == "distutils":
elif spec.name == "distutils" and not any(
spec.location.lower().startswith(ext_lib_dir.lower())
for ext_lib_dir in EXT_LIB_DIRS
):
# virtualenv below 20.0 patches distutils in an unexpected way
# so we just find the location of distutils that will be
# imported to avoid spurious import-error messages
Expand Down
17 changes: 17 additions & 0 deletions tests/unittest_regrtest.py
Expand Up @@ -81,6 +81,23 @@ def test_numpy_crash(self):
inferred = callfunc.inferred()
self.assertEqual(len(inferred), 1)

@unittest.skipUnless(HAS_NUMPY, "Needs numpy")
def test_numpy_distutils(self):
"""Special handling of virtualenv's patching of distutils shouldn't interfere
with numpy.distutils.
PY312_PLUS -- This test will likely become unnecessary when Python 3.12 is
numpy's minimum version. (numpy.distutils will be removed then.)
"""
node = extract_node(
"""
from numpy.distutils.misc_util import is_sequence
is_sequence("ABC") #@
"""
)
inferred = node.inferred()
self.assertIsInstance(inferred[0], nodes.Const)

def test_nameconstant(self) -> None:
# used to fail for Python 3.4
builder = AstroidBuilder()
Expand Down

0 comments on commit dbcd1bc

Please sign in to comment.