File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed
astroid/interpreter/_import Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,13 @@ Release date: TBA
19
19
20
20
Refs pylint-dev/pylint#9442
21
21
22
+ * Make ``astroid.interpreter._import.util.is_namespace`` only consider modules
23
+ using a loader set to ``NamespaceLoader`` or ``None`` as namespaces.
24
+ This fixes a problem that ``six.moves`` brain was not effective if ``six.moves``
25
+ was already imported.
26
+
27
+ Closes #1107
28
+
22
29
23
30
What's New in astroid 3.1.1?
24
31
============================
Original file line number Diff line number Diff line change 12
12
13
13
from astroid .const import IS_PYPY
14
14
15
+ if sys .version_info >= (3 , 11 ):
16
+ from importlib .machinery import NamespaceLoader
17
+ else :
18
+ from importlib ._bootstrap_external import _NamespaceLoader as NamespaceLoader
19
+
15
20
16
21
@lru_cache (maxsize = 4096 )
17
22
def is_namespace (modname : str ) -> bool :
@@ -101,4 +106,7 @@ def is_namespace(modname: str) -> bool:
101
106
found_spec is not None
102
107
and found_spec .submodule_search_locations is not None
103
108
and found_spec .origin is None
109
+ and (
110
+ found_spec .loader is None or isinstance (found_spec .loader , NamespaceLoader )
111
+ )
104
112
)
Original file line number Diff line number Diff line change @@ -29,6 +29,8 @@ def test_attribute_access(self) -> None:
29
29
six.moves.urllib_parse #@
30
30
six.moves.urllib_error #@
31
31
six.moves.urllib.request #@
32
+ from six.moves import StringIO
33
+ StringIO #@
32
34
"""
33
35
)
34
36
assert isinstance (ast_nodes , list )
@@ -64,6 +66,18 @@ def test_attribute_access(self) -> None:
64
66
self .assertIsInstance (urlretrieve , nodes .FunctionDef )
65
67
self .assertEqual (urlretrieve .qname (), "urllib.request.urlretrieve" )
66
68
69
+ StringIO = next (ast_nodes [4 ].infer ())
70
+ self .assertIsInstance (StringIO , nodes .ClassDef )
71
+ self .assertEqual (StringIO .qname (), "_io.StringIO" )
72
+ self .assertTrue (StringIO .callable ())
73
+
74
+ def test_attribute_access_with_six_moves_imported (self ) -> None :
75
+ astroid .MANAGER .clear_cache ()
76
+ astroid .MANAGER ._mod_file_cache .clear ()
77
+ import six .moves # type: ignore[import] # pylint: disable=import-outside-toplevel,unused-import,redefined-outer-name
78
+
79
+ self .test_attribute_access ()
80
+
67
81
def test_from_imports (self ) -> None :
68
82
ast_node = builder .extract_node (
69
83
"""
You can’t perform that action at this time.
0 commit comments