Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement of numpy brain #486

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions astroid/brain/brain_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
27 changes: 27 additions & 0 deletions astroid/tests/unittest_brain_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()