File tree 4 files changed +34
-2
lines changed
astroid/interpreter/_import
4 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,11 @@ Release date: TBA
15
15
Closes #1780
16
16
Refs #2140
17
17
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
+
18
23
* Reduce file system access in ``ast_from_file()``.
19
24
20
25
* Reduce time to ``import astroid`` by delaying ``astroid_bootstrapping()`` until
Original file line number Diff line number Diff line change 20
20
from typing import Any , Literal , NamedTuple , Protocol
21
21
22
22
from astroid .const import PY310_PLUS
23
- from astroid .modutils import EXT_LIB_DIRS
23
+ from astroid .modutils import EXT_LIB_DIRS , STD_LIB_DIRS
24
24
25
25
from . import util
26
26
@@ -157,6 +157,19 @@ def find_module(
157
157
location = getattr (spec .loader_state , "filename" , None ),
158
158
type = ModuleType .PY_FROZEN ,
159
159
)
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
+ )
160
173
except ValueError :
161
174
pass
162
175
submodule_path = sys .path
Original file line number Diff line number Diff line change 20
20
21
21
import astroid
22
22
from astroid import modutils
23
- from astroid .const import PY310_PLUS
23
+ from astroid .const import PY310_PLUS , WIN32
24
24
from astroid .interpreter ._import import spec
25
25
26
26
from . import resources
@@ -268,6 +268,19 @@ def test_std_lib(self) -> None:
268
268
os .path .realpath (os .path .__file__ .replace (".pyc" , ".py" )),
269
269
)
270
270
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
+
271
284
def test_builtin (self ) -> None :
272
285
self .assertIsNone (modutils .file_from_modpath (["sys" ]))
273
286
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