diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py index 51df17b70..257e995fb 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/tests/unittest_brain_numpy.py b/astroid/tests/unittest_brain_numpy.py index d6508b5bd..d90ed2edd 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()