summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhippo91 <guillaume.peillex@gmail.com>2017-10-29 17:09:19 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2017-11-11 12:39:24 +0100
commitad6e9dd2cd843eb8bab7eee8f9779e2a1a1261e9 (patch)
tree566ad5acf146ba984cad679848f7df9445f0edb4
parentcc60d739c5e32561a55ca82a5b452fba4b79b6f6 (diff)
downloadastroid-git-ad6e9dd2cd843eb8bab7eee8f9779e2a1a1261e9.tar.gz
Introduction of numpy.random.mtrand functions signatures inside numpy brain + add of a dedicated unit test
-rw-r--r--astroid/brain/brain_numpy.py91
-rw-r--r--astroid/tests/unittest_brain_numpy.py131
2 files changed, 149 insertions, 73 deletions
diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py
index 5feb1ad0..51df17b7 100644
--- a/astroid/brain/brain_numpy.py
+++ b/astroid/brain/brain_numpy.py
@@ -9,7 +9,57 @@
import astroid
-# TODO(cpopa): drop when understanding augmented assignments
+def numpy_random_mtrand_transform():
+ return astroid.parse('''
+ def beta(a, b, size=None): pass
+ def binomial(n, p, size=None): pass
+ def bytes(length): pass
+ def chisquare(df, size=None): pass
+ def choice(a, size=None, replace=True, p=None): pass
+ def dirichlet(alpha, size=None): pass
+ def exponential(scale=1.0, size=None): pass
+ def f(dfnum, dfden, size=None): pass
+ def gamma(shape, scale=1.0, size=None): pass
+ def geometric(p, size=None): pass
+ def get_state(): pass
+ def gumbel(loc=0.0, scale=1.0, size=None): pass
+ def hypergeometric(ngood, nbad, nsample, size=None): pass
+ def laplace(loc=0.0, scale=1.0, size=None): pass
+ def logistic(loc=0.0, scale=1.0, size=None): pass
+ def lognormal(mean=0.0, sigma=1.0, size=None): pass
+ def logseries(p, size=None): pass
+ def multinomial(n, pvals, size=None): pass
+ def multivariate_normal(mean, cov, size=None): pass
+ def negative_binomial(n, p, size=None): pass
+ def noncentral_chisquare(df, nonc, size=None): pass
+ def noncentral_f(dfnum, dfden, nonc, size=None): pass
+ def normal(loc=0.0, scale=1.0, size=None): pass
+ def pareto(a, size=None): pass
+ def permutation(x): pass
+ def poisson(lam=1.0, size=None): pass
+ def power(a, size=None): pass
+ def rand(*args): pass
+ def randint(low, high=None, size=None, dtype='l'): pass
+ def randn(*args): pass
+ def random_integers(low, high=None, size=None): pass
+ def random_sample(size=None): pass
+ def rayleigh(scale=1.0, size=None): pass
+ def seed(seed=None): pass
+ def set_state(state): pass
+ def shuffle(x): pass
+ def standard_cauchy(size=None): pass
+ def standard_exponential(size=None): pass
+ def standard_gamma(shape, size=None): pass
+ def standard_normal(size=None): pass
+ def standard_t(df, size=None): pass
+ def triangular(left, mode, right, size=None): pass
+ def uniform(low=0.0, high=1.0, size=None): pass
+ def vonmises(mu, kappa, size=None): pass
+ def wald(mean, scale, size=None): pass
+ def weibull(a, size=None): pass
+ def zipf(a, size=None): pass
+ ''')
+
def numpy_core_umath_transform():
ufunc_optional_keyword_arguments = ("""out=None, where=True, casting='same_kind', order='K', """
@@ -96,41 +146,6 @@ def numpy_core_umath_transform():
def true_divide(x1, x2, {opt_args:s}): pass
'''.format(opt_args=ufunc_optional_keyword_arguments))
-def numpy_core_transform():
- return astroid.parse('''
- from numpy.core import numeric
- from numpy.core import fromnumeric
- from numpy.core import defchararray
- from numpy.core import records
- from numpy.core import function_base
- from numpy.core import machar
- from numpy.core import getlimits
- from numpy.core import shape_base
- __all__ = (['char', 'rec', 'memmap', 'chararray'] + numeric.__all__ +
- fromnumeric.__all__ +
- records.__all__ +
- function_base.__all__ +
- machar.__all__ +
- getlimits.__all__ +
- shape_base.__all__)
- ''')
-
-
-def numpy_transform():
- return astroid.parse('''
- from numpy import core
- from numpy import matrixlib as _mat
- from numpy import lib
- __all__ = ['add_newdocs',
- 'ModuleDeprecationWarning',
- 'VisibleDeprecationWarning', 'linalg', 'fft', 'random',
- 'ctypeslib', 'ma',
- '__version__', 'pkgload', 'PackageLoader',
- 'show_config'] + core.__all__ + _mat.__all__ + lib.__all__
-
- ''')
-
-
astroid.register_module_extender(astroid.MANAGER, 'numpy.core.umath', numpy_core_umath_transform)
-#astroid.register_module_extender(astroid.MANAGER, 'numpy.core', numpy_core_transform)
-#astroid.register_module_extender(astroid.MANAGER, 'numpy', numpy_transform)
+astroid.register_module_extender(astroid.MANAGER, 'numpy.random.mtrand',
+ numpy_random_mtrand_transform)
diff --git a/astroid/tests/unittest_brain_numpy.py b/astroid/tests/unittest_brain_numpy.py
index 8d758719..1c3e7589 100644
--- a/astroid/tests/unittest_brain_numpy.py
+++ b/astroid/tests/unittest_brain_numpy.py
@@ -7,7 +7,6 @@
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
-import sys
import unittest
try:
@@ -16,46 +15,34 @@ try:
except ImportError:
HAS_NUMPY = False
-import six
-
-from astroid import MANAGER
-from astroid import bases
from astroid import builder
from astroid import nodes
-from astroid import util
-from astroid import test_utils
-import astroid
+
@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
-class NumpyBrainTest(unittest.TestCase):
+class NumpyBrainCoreUmathTest(unittest.TestCase):
- no_arg_ufunc = (
- 'geterrobj',
- )
+ no_arg_ufunc = ('geterrobj',)
- one_arg_ufunc_spec = (
- 'seterrobj',
- )
+ one_arg_ufunc_spec = ('seterrobj',)
one_arg_ufunc = (
- 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctanh',
- 'cbrt', 'conj', 'conjugate', 'cosh', 'deg2rad',
- 'degrees', 'exp2', 'expm1', 'fabs', 'frexp', 'isfinite',
- 'isinf', 'log', 'log1p', 'log2', 'logical_not', 'modf',
- 'negative', 'rad2deg', 'radians', 'reciprocal', 'rint',
- 'sign', 'signbit', 'spacing', 'square', 'tan', 'tanh',
- 'trunc',
- )
-
+ 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctanh',
+ 'cbrt', 'conj', 'conjugate', 'cosh', 'deg2rad',
+ 'degrees', 'exp2', 'expm1', 'fabs', 'frexp', 'isfinite',
+ 'isinf', 'log', 'log1p', 'log2', 'logical_not', 'modf',
+ 'negative', 'rad2deg', 'radians', 'reciprocal', 'rint',
+ 'sign', 'signbit', 'spacing', 'square', 'tan', 'tanh',
+ 'trunc',)
+
two_args_ufunc = (
- 'bitwise_and', 'bitwise_or', 'bitwise_xor',
- 'copysign', 'divide', 'equal', 'float_power',
- 'floor_divide', 'fmax', 'fmin', 'fmod', 'greater',
- 'hypot', 'ldexp', 'left_shift', 'less', 'logaddexp',
- 'logaddexp2', 'logical_and', 'logical_or', 'logical_xor',
- 'maximum', 'minimum', 'nextafter', 'not_equal', 'power',
- 'remainder', 'right_shift', 'subtract', 'true_divide',
- )
+ 'bitwise_and', 'bitwise_or', 'bitwise_xor',
+ 'copysign', 'divide', 'equal', 'float_power',
+ 'floor_divide', 'fmax', 'fmin', 'fmod', 'greater',
+ 'hypot', 'ldexp', 'left_shift', 'less', 'logaddexp',
+ 'logaddexp2', 'logical_and', 'logical_or', 'logical_xor',
+ 'maximum', 'minimum', 'nextafter', 'not_equal', 'power',
+ 'remainder', 'right_shift', 'subtract', 'true_divide',)
all_ufunc = no_arg_ufunc + one_arg_ufunc_spec + one_arg_ufunc + two_args_ufunc
@@ -73,7 +60,7 @@ class NumpyBrainTest(unittest.TestCase):
with self.subTest(const=const):
inferred = self._inferred_numpy_attribute(const)
self.assertIsInstance(inferred, nodes.Const)
-
+
def test_numpy_core_umath_constants_values(self):
exact_values = {'e': 2.718281828459045,
'euler_gamma': 0.5772156649015329}
@@ -87,13 +74,13 @@ class NumpyBrainTest(unittest.TestCase):
with self.subTest(func=func):
inferred = self._inferred_numpy_attribute(func)
self.assertIsInstance(inferred, nodes.FunctionDef)
-
+
def test_numpy_core_umath_functions_no_arg(self):
for func in self.no_arg_ufunc:
with self.subTest(func=func):
inferred = self._inferred_numpy_attribute(func)
self.assertFalse(inferred.argnames())
-
+
def test_numpy_core_umath_functions_one_arg_spec(self):
exact_arg_names = ['errobj']
for func in self.one_arg_ufunc_spec:
@@ -123,5 +110,79 @@ class NumpyBrainTest(unittest.TestCase):
default_args_values = [default.value for default in inferred.args.defaults]
self.assertEqual(default_args_values, exact_kwargs_default_values)
+
+class NumpyBrainRandomMtrandTest(unittest.TestCase):
+
+ all_mtrand = {
+ 'beta': (['a', 'b', 'size'], [None]),
+ 'binomial': (['n', 'p', 'size'], [None]),
+ 'bytes': (['length',], []),
+ 'chisquare': (['df', 'size'], [None]),
+ 'choice': (['a', 'size', 'replace', 'p'], [None, True, None]),
+ 'dirichlet': (['alpha', 'size'], [None]),
+ 'exponential': (['scale', 'size'], [1.0, None]),
+ 'f': (['dfnum', 'dfden', 'size'], [None]),
+ 'gamma': (['shape', 'scale', 'size'], [1.0, None]),
+ 'geometric': (['p', 'size'], [None]),
+ 'get_state': ([], []),
+ 'gumbel': (['loc', 'scale', 'size'], [0.0, 1.0, None]),
+ 'hypergeometric': (['ngood', 'nbad', 'nsample', 'size'], [None]),
+ 'laplace': (['loc', 'scale', 'size'], [0.0, 1.0, None]),
+ 'logistic': (['loc', 'scale', 'size'], [0.0, 1.0, None]),
+ 'lognormal': (['mean', 'sigma', 'size'], [0.0, 1.0, None]),
+ 'logseries': (['p', 'size'], [None]),
+ 'multinomial': (['n', 'pvals', 'size'], [None]),
+ 'multivariate_normal': (['mean', 'cov', 'size'], [None]),
+ 'negative_binomial': (['n', 'p', 'size'], [None]),
+ 'noncentral_chisquare': (['df', 'nonc', 'size'], [None]),
+ 'noncentral_f': (['dfnum', 'dfden', 'nonc', 'size'], [None]),
+ 'normal': (['loc', 'scale', 'size'], [0.0, 1.0, None]),
+ 'pareto': (['a', 'size'], [None]),
+ 'permutation': (['x'], []),
+ 'poisson': (['lam', 'size'], [1.0, None]),
+ 'power': (['a', 'size'], [None]),
+ 'rand': (['args'], []),
+ 'randint': (['low', 'high', 'size', 'dtype'], [None, None, 'l']),
+ 'randn': (['args'], []),
+ 'random_integers': (['low', 'high', 'size'], [None, None]),
+ 'random_sample': (['size'], [None]),
+ 'rayleigh': (['scale', 'size'], [1.0, None]),
+ 'seed': (['seed'], [None]),
+ 'set_state': (['state'], []),
+ 'shuffle': (['x'], []),
+ 'standard_cauchy': (['size'], [None]),
+ 'standard_exponential': (['size'], [None]),
+ 'standard_gamma': (['shape', 'size'], [None]),
+ 'standard_normal': (['size'], [None]),
+ 'standard_t': (['df', 'size'], [None]),
+ 'triangular': (['left', 'mode', 'right', 'size'], [None]),
+ 'uniform': (['low', 'high', 'size'], [0.0, 1.0, None]),
+ 'vonmises': (['mu', 'kappa', 'size'], [None]),
+ 'wald': (['mean', 'scale', 'size'], [None]),
+ 'weibull': (['a', 'size'], [None]),
+ 'zipf': (['a', 'size'], [None])}
+
+ def _inferred_numpy_attribute(self, func_name):
+ node = builder.extract_node("""
+ import numpy.random.mtrand as tested_module
+ func = tested_module.{:s}
+ func""".format(func_name))
+ return next(node.infer())
+
+ def test_numpy_random_mtrand_functions(self):
+ for func in self.all_mtrand:
+ with self.subTest(func=func):
+ inferred = self._inferred_numpy_attribute(func)
+ self.assertIsInstance(inferred, nodes.FunctionDef)
+
+ def test_numpy_core_umath_functions_signature(self):
+ for func, (exact_arg_names, exact_kwargs_default_values) in self.all_mtrand.items():
+ with self.subTest(func=func):
+ inferred = self._inferred_numpy_attribute(func)
+ self.assertEqual(inferred.argnames(), exact_arg_names)
+ default_args_values = [default.value for default in inferred.args.defaults]
+ self.assertEqual(default_args_values, exact_kwargs_default_values)
+
+
if __name__ == '__main__':
unittest.main()