File tree Expand file tree Collapse file tree 4 files changed +34
-2
lines changed
astroid/interpreter/_import Expand file tree Collapse file tree 4 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,11 @@ Release date: TBA
1515 Closes #1780
1616 Refs #2140
1717
18+ * Prefer standard library modules over same-named modules on sys.path. For example
19+ ``import copy`` now finds ``copy`` instead of ``copy.py``. Solves ``no-member`` issues.
20+
21+ Closes pylint-dev/pylint#6535
22+
1823* Reduce file system access in ``ast_from_file()``.
1924
2025* Reduce time to ``import astroid`` by delaying ``astroid_bootstrapping()`` until
Original file line number Diff line number Diff line change 2020from typing import Any , Literal , NamedTuple , Protocol
2121
2222from astroid .const import PY310_PLUS
23- from astroid .modutils import EXT_LIB_DIRS
23+ from astroid .modutils import EXT_LIB_DIRS , STD_LIB_DIRS
2424
2525from . import util
2626
@@ -157,6 +157,19 @@ def find_module(
157157 location = getattr (spec .loader_state , "filename" , None ),
158158 type = ModuleType .PY_FROZEN ,
159159 )
160+ if (
161+ spec
162+ and isinstance (spec .loader , importlib .machinery .SourceFileLoader )
163+ and any (spec .origin .startswith (std_lib ) for std_lib in STD_LIB_DIRS )
164+ and not spec .origin .endswith ("__init__.py" )
165+ ):
166+ # Return standard library modules before local modules
167+ # https://github.com/pylint-dev/pylint/issues/6535
168+ return ModuleSpec (
169+ name = modname ,
170+ location = spec .origin ,
171+ type = ModuleType .PY_SOURCE ,
172+ )
160173 except ValueError :
161174 pass
162175 submodule_path = sys .path
Original file line number Diff line number Diff line change 2020
2121import astroid
2222from astroid import modutils
23- from astroid .const import PY310_PLUS
23+ from astroid .const import PY310_PLUS , WIN32
2424from astroid .interpreter ._import import spec
2525
2626from . import resources
@@ -268,6 +268,19 @@ def test_std_lib(self) -> None:
268268 os .path .realpath (os .path .__file__ .replace (".pyc" , ".py" )),
269269 )
270270
271+ def test_std_lib_found_before_same_named_package_on_path (self ) -> None :
272+ realpath = str (resources .RESOURCE_PATH )
273+ if WIN32 :
274+ # Escape backslashes.
275+ realpath = realpath .replace ("\\ " , "\\ \\ " )
276+ sys .path .insert (0 , realpath )
277+ self .addCleanup (sys .path .pop , 0 )
278+
279+ file = modutils .file_from_modpath (["copy" ])
280+
281+ self .assertNotIn ("test" , file ) # tests/testdata/python3/data/copy.py
282+ self .assertTrue (any (stdlib in file for stdlib in modutils .STD_LIB_DIRS ))
283+
271284 def test_builtin (self ) -> None :
272285 self .assertIsNone (modutils .file_from_modpath (["sys" ]))
273286
Original file line number Diff line number Diff line change 1+ """fake copy module (unlike email, we need one without __init__.py)"""
You can’t perform that action at this time.
0 commit comments