diff --git a/ChangeLog b/ChangeLog index 354b711f92..1e95df1554 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ Change log for the astroid package (used to be astng) ===================================================== + * Enhancement of brain_numpy by adding different types from + numpy.core.numerictypes + 2018-01-23 -- 1.6.1 * Fix a crash when __annotations__ access a parent's __init__ that does not have arguments @@ -1384,6 +1387,3 @@ Change log for the astroid package (used to be astng) longer be maintained (this explains that this package is starting with the 0.13 version number, since the fork occurs with the version released in logilab-common 0.12). - - - diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py index 51df17b70a..257e995fba 100644 --- a/astroid/brain/brain_numpy.py +++ b/astroid/brain/brain_numpy.py @@ -146,6 +146,39 @@ def subtract(x1, x2, {opt_args:s}): pass def true_divide(x1, x2, {opt_args:s}): pass '''.format(opt_args=ufunc_optional_keyword_arguments)) + +def numpy_core_numerictypes_transform(): + return astroid.parse(''' + # different types defined in numerictypes.py + uint16 = type('uint16') + uint32 = type('uint32') + uint64 = type('uint64') + int128 = type('int128') + uint128 = type('uint128') + float16 = type('float16') + float32 = type('float32') + float64 = type('float64') + float80 = type('float80') + float96 = type('float96') + float128 = type('float128') + float256 = type('float256') + complex32 = type('complex32') + complex64 = type('complex64') + complex128 = type('complex128') + complex160 = type('complex160') + complex192 = type('complex192') + complex256 = type('complex256') + complex512 = type('complex512') + timedelta64 = type('timedelta64') + datetime64 = type('datetime64') + unicode_ = type('unicode_') + string_ = type('string_') + object_ = type('object_') + ''') + + astroid.register_module_extender(astroid.MANAGER, 'numpy.core.umath', numpy_core_umath_transform) astroid.register_module_extender(astroid.MANAGER, 'numpy.random.mtrand', numpy_random_mtrand_transform) +astroid.register_module_extender(astroid.MANAGER, 'numpy.core.numerictypes', + numpy_core_numerictypes_transform) diff --git a/astroid/context.py b/astroid/context.py index 627bae5dfd..00b0287ba3 100644 --- a/astroid/context.py +++ b/astroid/context.py @@ -42,7 +42,6 @@ def cache_generator(self, key, generator): yield result self.inferred[key] = tuple(results) - return @contextlib.contextmanager def restore_path(self): diff --git a/astroid/node_classes.py b/astroid/node_classes.py index dcff369232..3c7418f324 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -636,7 +636,7 @@ def nodes_of_class(self, klass, skip_klass=None): for matching in child_node.nodes_of_class(klass, skip_klass): yield matching - def _infer_name(self, frame, name): + def _infer_name(self, frame, name): #pylint: disable=useless-return # overridden for ImportFrom, Import, Global, TryExcept and Arguments return None diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index be4483fe7d..74db78b849 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -536,7 +536,7 @@ def statement(self): """ return self - def previous_sibling(self): + def previous_sibling(self): #pylint: disable=useless-return """The previous sibling statement. :returns: The previous sibling statement node. @@ -544,7 +544,7 @@ def previous_sibling(self): """ return - def next_sibling(self): + def next_sibling(self): #pylint: disable=useless-return """The next sibling statement node. :returns: The next sibling statement node. diff --git a/astroid/tests/unittest_brain_numpy.py b/astroid/tests/unittest_brain_numpy.py index d6508b5bde..d90ed2edd7 100644 --- a/astroid/tests/unittest_brain_numpy.py +++ b/astroid/tests/unittest_brain_numpy.py @@ -238,5 +238,32 @@ def test_numpy_random_mtrand_functions_signature(self): self.assertEqual(default_args_values, exact_kwargs_default_values) +@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.") +class NumpyBrainCoreNumericTypesTest(SubTestWrapper): + """ + Test of all the missing types defined in numerictypes module. + """ + all_types = ['uint16', 'uint32', 'uint64', 'int128', 'uint128', + 'float16', 'float32', 'float64', 'float80', 'float96', + 'float128', 'float256', 'complex32', 'complex64', 'complex128', + 'complex160', 'complex192', 'complex256', 'complex512', + 'timedelta64', 'datetime64', 'unicode_', 'string_', 'object_'] + + def _inferred_numpy_attribute(self, attrib): + node = builder.extract_node(""" + import numpy.core.numerictypes as tested_module + missing_type = tested_module.{:s}""".format(attrib)) + return next(node.value.infer()) + + def test_numpy_core_types(self): + """ + Test that all defined types have ClassDef type. + """ + for typ in self.all_types: + with self.subTest(typ=typ): + inferred = self._inferred_numpy_attribute(typ) + self.assertIsInstance(inferred, nodes.ClassDef) + + if __name__ == '__main__': unittest.main() diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 70eeb98fa5..238853f78a 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -4273,7 +4273,7 @@ def test_duplicated_keyword_arguments(self): self.assertIn('f', site.duplicated_keywords) -@unittest.expectedFailure +@unittest.skip class ObjectDunderNewTest(unittest.TestCase): def test_object_dunder_new_is_inferred_if_decorator(self):