-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Revert modifications of PR 1148. While it is probably still a good idea to prevent nodes that are dynamically imported to be inferred through an astroid's brain, the way it was done in builder.py was incorrect. The way it was done, lead to prevent the use of astroid legetimate brains even for node that was not dynamically loaded. * Adds a brain to infer the numpy.ma.masked_where function Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
- Loading branch information
1 parent
4ffdf11
commit 05445e2
Showing
4 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright (c) 2021 hippo91 <guillaume.peillex@gmail.com> | ||
|
||
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html | ||
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE | ||
"""Astroid hooks for numpy ma module""" | ||
|
||
from astroid.brain.helpers import register_module_extender | ||
from astroid.builder import parse | ||
from astroid.manager import AstroidManager | ||
|
||
|
||
def numpy_ma_transform(): | ||
""" | ||
Infer the call of the masked_where function | ||
:param node: node to infer | ||
:param context: inference context | ||
""" | ||
return parse( | ||
""" | ||
import numpy.ma | ||
def masked_where(condition, a, copy=True): | ||
return numpy.ma.masked_array(a, mask=[]) | ||
""" | ||
) | ||
|
||
|
||
register_module_extender(AstroidManager(), "numpy.ma", numpy_ma_transform) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (c) 2021 hippo91 <guillaume.peillex@gmail.com> | ||
|
||
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html | ||
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE | ||
import pytest | ||
|
||
try: | ||
import numpy # pylint: disable=unused-import | ||
|
||
HAS_NUMPY = True | ||
except ImportError: | ||
HAS_NUMPY = False | ||
|
||
from astroid import builder | ||
|
||
|
||
@pytest.mark.skipif(HAS_NUMPY is False, reason="This test requires the numpy library.") | ||
class TestBrainNumpyMa: | ||
""" | ||
Test the numpy ma brain module | ||
""" | ||
|
||
@staticmethod | ||
def test_numpy_ma_masked_where_returns_maskedarray(): | ||
""" | ||
Test that calls to numpy ma masked_where returns a MaskedArray object. | ||
The "masked_where" node is an Attribute | ||
""" | ||
src = """ | ||
import numpy as np | ||
data = np.ndarray((1,2)) | ||
np.ma.masked_where([1, 0, 0], data) | ||
""" | ||
node = builder.extract_node(src) | ||
cls_node = node.inferred()[0] | ||
assert cls_node.pytype() == "numpy.ma.core.MaskedArray" | ||
|
||
@staticmethod | ||
def test_numpy_ma_masked_where_returns_maskedarray_bis(): | ||
""" | ||
Test that calls to numpy ma masked_where returns a MaskedArray object | ||
The "masked_where" node is a Name | ||
""" | ||
src = """ | ||
from numpy.ma import masked_where | ||
data = np.ndarray((1,2)) | ||
masked_where([1, 0, 0], data) | ||
""" | ||
node = builder.extract_node(src) | ||
cls_node = node.inferred()[0] | ||
assert cls_node.pytype() == "numpy.ma.core.MaskedArray" |