diff options
| author | hippo91 <guillaume.peillex@gmail.com> | 2019-02-17 19:31:37 +0100 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-02-17 19:31:37 +0100 |
| commit | e084ae9fc6e21baed5092ca855935408bac936a7 (patch) | |
| tree | 8df33941104b769bf797558bb5e32b72fc6c08bb | |
| parent | 0bafc47c4fbe987e8dd1002d37bc3bcd62951405 (diff) | |
| download | astroid-git-e084ae9fc6e21baed5092ca855935408bac936a7.tar.gz | |
Add support of unary operators to numpy types.
| -rw-r--r-- | ChangeLog | 3 | ||||
| -rw-r--r-- | astroid/brain/brain_numpy.py | 7 | ||||
| -rw-r--r-- | astroid/tests/unittest_brain_numpy.py | 44 |
3 files changed, 53 insertions, 1 deletions
@@ -6,6 +6,9 @@ What's New in astroid 2.2.0? ============================ Release Date: TBA +* Fix a bug concerning inference of unary operators on numpy types. + + Close PyCQA/pylint#2436 (first part) * Fix a crash with ``typing.NamedTuple`` and empty fields. Close PyCQA/pylint#2745 diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py index 7ad8c118..7868ab72 100644 --- a/astroid/brain/brain_numpy.py +++ b/astroid/brain/brain_numpy.py @@ -270,6 +270,7 @@ def numpy_core_numerictypes_transform(): self.type = None def newbyteorder(self, new_order='S'): return any + def __neg__(self): return any class ndarray(object): @@ -291,6 +292,9 @@ def numpy_core_numerictypes_transform(): self.size = None self.strides = None + def __neg__(self): return any + def __inv__(self): return any + def __invert__(self): return any def all(self): return any def any(self): return any def argmax(self): return any @@ -356,7 +360,8 @@ def numpy_core_numerictypes_transform(): class flexible(generic): pass class bool_(generic): pass - class number(generic): pass + class number(generic): + def __neg__(self): return any class datetime64(generic): pass diff --git a/astroid/tests/unittest_brain_numpy.py b/astroid/tests/unittest_brain_numpy.py index 141e5335..f0a3ab35 100644 --- a/astroid/tests/unittest_brain_numpy.py +++ b/astroid/tests/unittest_brain_numpy.py @@ -573,6 +573,50 @@ class NumpyBrainCoreNumericTypesTest(SubTestWrapper): with self.subTest(attr=attr): self.assertNotEqual(len(inferred.getattr(attr)), 0) + def test_number_types_have_unary_operators(self): + """ + Test that number types have unary operators + """ + unary_ops = ("__neg__",) + + for type_ in ( + "float64", + "float96", + "floating", + "int16", + "int32", + "int32", + "int64", + "int8", + "integer", + "number", + "signedinteger", + "uint16", + "uint32", + "uint32", + "uint64", + "uint8", + "unsignedinteger", + ): + with self.subTest(typ=type_): + inferred = self._inferred_numpy_attribute(type_) + for attr in unary_ops: + with self.subTest(attr=attr): + self.assertNotEqual(len(inferred.getattr(attr)), 0) + + def test_array_types_have_unary_operators(self): + """ + Test that array types have unary operators + """ + unary_ops = ("__neg__", "__inv__", "__invert__") + + for type_ in ("ndarray",): + with self.subTest(typ=type_): + inferred = self._inferred_numpy_attribute(type_) + for attr in unary_ops: + with self.subTest(attr=attr): + self.assertNotEqual(len(inferred.getattr(attr)), 0) + if __name__ == "__main__": unittest.main() |
