Skip to content

Commit 33065ec

Browse files
authored
Merge pull request #664 from hippo91/bug_pylint_2721
Deep rework of the numpy brain. The original brain has been split into numerous one. Each numpy brain module corresponds to a numpyone. There are: brain_numpy_core_fromnumeric => numpy.core.fromnumeric brain_numpy_core_function_base => numpy.core.function_base brain_numpy_core_multiarray => numpy.core.multiarray brain_numpy_core_numeric => numpy.core.numeric brain_core_numerictypes => numpy.core.numerictypes brain_core_umath => numpy.core.umath brain_numpy_random_mtrand => numpy.random.mtrand Last but not least, the numpy.ndarray class has its corresponding astroid's brain (brain_numpy_ndarray). The major improvement, besides a clearer organisation, is the fact that each method of the ndarray class should now be correctly inferred. Especially those returning ndarray objects. It has been made possible by ensuring that the return type of those methods is inferred as ndarray and nothing else. To achieve this, the inference_tip function has been used. When a numpy callable may return two different kinds of objects, for example bool or ndarray, the choice to prefer ndarray has been made in the corresponding astroid brain method.
2 parents edd79cf + f818d1a commit 33065ec

20 files changed

+1874
-1217
lines changed

ChangeLog

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ What's New in astroid 2.3.0?
66
============================
77
Release Date: TBA
88

9+
* Numpy brain support is improved.
10+
11+
Numpy's fundamental type ``numpy.ndarray`` has its own brain : ``brain_numpy_ndarray`` and
12+
each numpy module that necessitates brain action has now its own numpy brain :
13+
14+
- ``numpy.core.numeric``
15+
- ``numpy.core.function_base``
16+
- ``numpy.core.multiarray``
17+
- ``numpy.core.numeric``
18+
- ``numpy.core.numerictypes``
19+
- ``numpy.core.umath``
20+
- ``numpy.random.mtrand``
21+
22+
Close PyCQA/pylint#2865
23+
Close PyCQA/pylint#2747
24+
Close PyCQA/pylint#2721
25+
Close PyCQA/pylint#2326
26+
Close PyCQA/pylint#2021
27+
928
* Add support for Python 3.8's `NamedExpr` nodes, which is part of assignment expressions.
1029

1130
Close #674

astroid/brain/brain_numpy.py

-557
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) 2018-2019 hippo91 <[email protected]>
2+
3+
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
4+
# For details: https://github.com./PyCQA/astroid/blob/master/COPYING.LESSER
5+
6+
7+
"""Astroid hooks for numpy.core.fromnumeric module."""
8+
9+
import astroid
10+
11+
12+
def numpy_core_fromnumeric_transform():
13+
return astroid.parse(
14+
"""
15+
def sum(a, axis=None, dtype=None, out=None, keepdims=None, initial=None):
16+
return numpy.ndarray([0, 0])
17+
"""
18+
)
19+
20+
21+
astroid.register_module_extender(
22+
astroid.MANAGER, "numpy.core.fromnumeric", numpy_core_fromnumeric_transform
23+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) 2018-2019 hippo91 <[email protected]>
2+
3+
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
4+
# For details: https://github.com./PyCQA/astroid/blob/master/COPYING.LESSER
5+
6+
7+
"""Astroid hooks for numpy.core.function_base module."""
8+
9+
import functools
10+
import astroid
11+
from brain_numpy_utils import looks_like_numpy_member, infer_numpy_member
12+
13+
14+
METHODS_TO_BE_INFERRED = {
15+
"linspace": """def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0):
16+
return numpy.ndarray([0, 0])""",
17+
"logspace": """def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0):
18+
return numpy.ndarray([0, 0])""",
19+
"geomspace": """def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0):
20+
return numpy.ndarray([0, 0])""",
21+
}
22+
23+
for func_name, func_src in METHODS_TO_BE_INFERRED.items():
24+
inference_function = functools.partial(infer_numpy_member, func_src)
25+
astroid.MANAGER.register_transform(
26+
astroid.Attribute,
27+
astroid.inference_tip(inference_function),
28+
functools.partial(looks_like_numpy_member, func_name),
29+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) 2018-2019 hippo91 <[email protected]>
2+
3+
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
4+
# For details: https://github.com./PyCQA/astroid/blob/master/COPYING.LESSER
5+
6+
7+
"""Astroid hooks for numpy.core.multiarray module."""
8+
9+
import functools
10+
import astroid
11+
from brain_numpy_utils import looks_like_numpy_member, infer_numpy_member
12+
13+
14+
def numpy_core_multiarray_transform():
15+
return astroid.parse(
16+
"""
17+
# different functions defined in multiarray.py
18+
def inner(a, b):
19+
return numpy.ndarray([0, 0])
20+
21+
def vdot(a, b):
22+
return numpy.ndarray([0, 0])
23+
"""
24+
)
25+
26+
27+
astroid.register_module_extender(
28+
astroid.MANAGER, "numpy.core.multiarray", numpy_core_multiarray_transform
29+
)
30+
31+
32+
METHODS_TO_BE_INFERRED = {
33+
"array": """def array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0):
34+
return numpy.ndarray([0, 0])""",
35+
"dot": """def dot(a, b, out=None):
36+
return numpy.ndarray([0, 0])""",
37+
"empty_like": """def empty_like(a, dtype=None, order='K', subok=True):
38+
return numpy.ndarray((0, 0))""",
39+
"concatenate": """def concatenate(arrays, axis=None, out=None):
40+
return numpy.ndarray((0, 0))""",
41+
"where": """def where(condition, x=None, y=None):
42+
return numpy.ndarray([0, 0])""",
43+
"empty": """def empty(shape, dtype=float, order='C'):
44+
return numpy.ndarray([0, 0])""",
45+
"zeros": """def zeros(shape, dtype=float, order='C'):
46+
return numpy.ndarray([0, 0])""",
47+
}
48+
49+
for method_name, function_src in METHODS_TO_BE_INFERRED.items():
50+
inference_function = functools.partial(infer_numpy_member, function_src)
51+
astroid.MANAGER.register_transform(
52+
astroid.Attribute,
53+
astroid.inference_tip(inference_function),
54+
functools.partial(looks_like_numpy_member, method_name),
55+
)
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2018-2019 hippo91 <[email protected]>
2+
3+
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
4+
# For details: https://github.com./PyCQA/astroid/blob/master/COPYING.LESSER
5+
6+
7+
"""Astroid hooks for numpy.core.numeric module."""
8+
9+
import functools
10+
import astroid
11+
from brain_numpy_utils import looks_like_numpy_member, infer_numpy_member
12+
13+
14+
def numpy_core_numeric_transform():
15+
return astroid.parse(
16+
"""
17+
# different functions defined in numeric.py
18+
import numpy
19+
def zeros_like(a, dtype=None, order='K', subok=True): return numpy.ndarray((0, 0))
20+
def ones_like(a, dtype=None, order='K', subok=True): return numpy.ndarray((0, 0))
21+
def full_like(a, fill_value, dtype=None, order='K', subok=True): return numpy.ndarray((0, 0))
22+
"""
23+
)
24+
25+
26+
astroid.register_module_extender(
27+
astroid.MANAGER, "numpy.core.numeric", numpy_core_numeric_transform
28+
)
29+
30+
31+
METHODS_TO_BE_INFERRED = {
32+
"ones": """def ones(shape, dtype=None, order='C'):
33+
return numpy.ndarray([0, 0])"""
34+
}
35+
36+
37+
for method_name, function_src in METHODS_TO_BE_INFERRED.items():
38+
inference_function = functools.partial(infer_numpy_member, function_src)
39+
astroid.MANAGER.register_transform(
40+
astroid.Attribute,
41+
astroid.inference_tip(inference_function),
42+
functools.partial(looks_like_numpy_member, method_name),
43+
)

0 commit comments

Comments
 (0)