Skip to content

Commit feaa011

Browse files
Merge branch 'master' into fix-classdef-keywords
2 parents 2b2ad87 + 45aac90 commit feaa011

File tree

3 files changed

+115
-77
lines changed

3 files changed

+115
-77
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Release Date: TBA
1313

1414
* Iterate over ``Keywords`` when using ``ClassDef.get_children``
1515

16+
* Detects `import numpy` as a valid `numpy` import.
17+
18+
Closes PyCQA/pylint#3974
19+
1620

1721
What's New in astroid 2.5.1?
1822
============================

astroid/brain/brain_numpy_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def _is_a_numpy_module(node: astroid.node_classes.Name) -> bool:
3333
x for x in node.lookup(module_nickname)[1] if isinstance(x, astroid.Import)
3434
]
3535
for target in potential_import_target:
36-
if ("numpy", module_nickname) in target.names:
36+
if ("numpy", module_nickname) in target.names or (
37+
"numpy",
38+
None,
39+
) in target.names:
3740
return True
3841
return False
3942

tests/unittest_brain_numpy_core_multiarray.py

Lines changed: 107 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class BrainNumpyCoreMultiarrayTest(unittest.TestCase):
3636
("is_busday", "['2011-07-01', '2011-07-02', '2011-07-18']"),
3737
("lexsort", "(('toto', 'tutu'), ('riri', 'fifi'))"),
3838
("packbits", "np.array([1, 2])"),
39-
("ravel_multi_index", "np.array([[1, 2], [2, 1]])", "(3, 4)"),
4039
("unpackbits", "np.array([[1], [2], [3]], dtype=np.uint8)"),
4140
("vdot", "[1, 2]", "[1, 2]"),
4241
("where", "[True, False]", "[1, 2]", "[2, 1]"),
@@ -77,105 +76,137 @@ def _inferred_numpy_func_call(self, func_name, *func_args):
7776
)
7877
return node.infer()
7978

79+
def _inferred_numpy_no_alias_func_call(self, func_name, *func_args):
80+
node = builder.extract_node(
81+
"""
82+
import numpy
83+
func = numpy.{:s}
84+
func({:s})
85+
""".format(
86+
func_name, ",".join(func_args)
87+
)
88+
)
89+
return node.infer()
90+
8091
def test_numpy_function_calls_inferred_as_ndarray(self):
8192
"""
8293
Test that calls to numpy functions are inferred as numpy.ndarray
8394
"""
84-
for func_ in self.numpy_functions_returning_array:
85-
with self.subTest(typ=func_):
86-
inferred_values = list(self._inferred_numpy_func_call(*func_))
87-
self.assertTrue(
88-
len(inferred_values) == 1,
89-
msg="Too much inferred values ({}) for {:s}".format(
90-
inferred_values, func_[0]
91-
),
92-
)
93-
self.assertTrue(
94-
inferred_values[-1].pytype() == ".ndarray",
95-
msg="Illicit type for {:s} ({})".format(
96-
func_[0], inferred_values[-1].pytype()
97-
),
98-
)
95+
for infer_wrapper in (
96+
self._inferred_numpy_func_call,
97+
self._inferred_numpy_no_alias_func_call,
98+
):
99+
for func_ in self.numpy_functions_returning_array:
100+
with self.subTest(typ=func_):
101+
inferred_values = list(infer_wrapper(*func_))
102+
self.assertTrue(
103+
len(inferred_values) == 1,
104+
msg="Too much inferred values ({}) for {:s}".format(
105+
inferred_values, func_[0]
106+
),
107+
)
108+
self.assertTrue(
109+
inferred_values[-1].pytype() == ".ndarray",
110+
msg="Illicit type for {:s} ({})".format(
111+
func_[0], inferred_values[-1].pytype()
112+
),
113+
)
99114

100115
def test_numpy_function_calls_inferred_as_bool(self):
101116
"""
102117
Test that calls to numpy functions are inferred as bool
103118
"""
104-
for func_ in self.numpy_functions_returning_bool:
105-
with self.subTest(typ=func_):
106-
inferred_values = list(self._inferred_numpy_func_call(*func_))
107-
self.assertTrue(
108-
len(inferred_values) == 1,
109-
msg="Too much inferred values ({}) for {:s}".format(
110-
inferred_values, func_[0]
111-
),
112-
)
113-
self.assertTrue(
114-
inferred_values[-1].pytype() == "builtins.bool",
115-
msg="Illicit type for {:s} ({})".format(
116-
func_[0], inferred_values[-1].pytype()
117-
),
118-
)
119+
for infer_wrapper in (
120+
self._inferred_numpy_func_call,
121+
self._inferred_numpy_no_alias_func_call,
122+
):
123+
for func_ in self.numpy_functions_returning_bool:
124+
with self.subTest(typ=func_):
125+
inferred_values = list(infer_wrapper(*func_))
126+
self.assertTrue(
127+
len(inferred_values) == 1,
128+
msg="Too much inferred values ({}) for {:s}".format(
129+
inferred_values, func_[0]
130+
),
131+
)
132+
self.assertTrue(
133+
inferred_values[-1].pytype() == "builtins.bool",
134+
msg="Illicit type for {:s} ({})".format(
135+
func_[0], inferred_values[-1].pytype()
136+
),
137+
)
119138

120139
def test_numpy_function_calls_inferred_as_dtype(self):
121140
"""
122141
Test that calls to numpy functions are inferred as numpy.dtype
123142
"""
124-
for func_ in self.numpy_functions_returning_dtype:
125-
with self.subTest(typ=func_):
126-
inferred_values = list(self._inferred_numpy_func_call(*func_))
127-
self.assertTrue(
128-
len(inferred_values) == 1,
129-
msg="Too much inferred values ({}) for {:s}".format(
130-
inferred_values, func_[0]
131-
),
132-
)
133-
self.assertTrue(
134-
inferred_values[-1].pytype() == "numpy.dtype",
135-
msg="Illicit type for {:s} ({})".format(
136-
func_[0], inferred_values[-1].pytype()
137-
),
138-
)
143+
for infer_wrapper in (
144+
self._inferred_numpy_func_call,
145+
self._inferred_numpy_no_alias_func_call,
146+
):
147+
for func_ in self.numpy_functions_returning_dtype:
148+
with self.subTest(typ=func_):
149+
inferred_values = list(infer_wrapper(*func_))
150+
self.assertTrue(
151+
len(inferred_values) == 1,
152+
msg="Too much inferred values ({}) for {:s}".format(
153+
inferred_values, func_[0]
154+
),
155+
)
156+
self.assertTrue(
157+
inferred_values[-1].pytype() == "numpy.dtype",
158+
msg="Illicit type for {:s} ({})".format(
159+
func_[0], inferred_values[-1].pytype()
160+
),
161+
)
139162

140163
def test_numpy_function_calls_inferred_as_none(self):
141164
"""
142165
Test that calls to numpy functions are inferred as None
143166
"""
144-
for func_ in self.numpy_functions_returning_none:
145-
with self.subTest(typ=func_):
146-
inferred_values = list(self._inferred_numpy_func_call(*func_))
147-
self.assertTrue(
148-
len(inferred_values) == 1,
149-
msg="Too much inferred values ({}) for {:s}".format(
150-
inferred_values, func_[0]
151-
),
152-
)
153-
self.assertTrue(
154-
inferred_values[-1].pytype() == "builtins.NoneType",
155-
msg="Illicit type for {:s} ({})".format(
156-
func_[0], inferred_values[-1].pytype()
157-
),
158-
)
167+
for infer_wrapper in (
168+
self._inferred_numpy_func_call,
169+
self._inferred_numpy_no_alias_func_call,
170+
):
171+
for func_ in self.numpy_functions_returning_none:
172+
with self.subTest(typ=func_):
173+
inferred_values = list(infer_wrapper(*func_))
174+
self.assertTrue(
175+
len(inferred_values) == 1,
176+
msg="Too much inferred values ({}) for {:s}".format(
177+
inferred_values, func_[0]
178+
),
179+
)
180+
self.assertTrue(
181+
inferred_values[-1].pytype() == "builtins.NoneType",
182+
msg="Illicit type for {:s} ({})".format(
183+
func_[0], inferred_values[-1].pytype()
184+
),
185+
)
159186

160187
def test_numpy_function_calls_inferred_as_tuple(self):
161188
"""
162189
Test that calls to numpy functions are inferred as tuple
163190
"""
164-
for func_ in self.numpy_functions_returning_tuple:
165-
with self.subTest(typ=func_):
166-
inferred_values = list(self._inferred_numpy_func_call(*func_))
167-
self.assertTrue(
168-
len(inferred_values) == 1,
169-
msg="Too much inferred values ({}) for {:s}".format(
170-
inferred_values, func_[0]
171-
),
172-
)
173-
self.assertTrue(
174-
inferred_values[-1].pytype() == "builtins.tuple",
175-
msg="Illicit type for {:s} ({})".format(
176-
func_[0], inferred_values[-1].pytype()
177-
),
178-
)
191+
for infer_wrapper in (
192+
self._inferred_numpy_func_call,
193+
self._inferred_numpy_no_alias_func_call,
194+
):
195+
for func_ in self.numpy_functions_returning_tuple:
196+
with self.subTest(typ=func_):
197+
inferred_values = list(infer_wrapper(*func_))
198+
self.assertTrue(
199+
len(inferred_values) == 1,
200+
msg="Too much inferred values ({}) for {:s}".format(
201+
inferred_values, func_[0]
202+
),
203+
)
204+
self.assertTrue(
205+
inferred_values[-1].pytype() == "builtins.tuple",
206+
msg="Illicit type for {:s} ({})".format(
207+
func_[0], inferred_values[-1].pytype()
208+
),
209+
)
179210

180211

181212
if __name__ == "__main__":

0 commit comments

Comments
 (0)