Debian Codegraph @

tests/brain/numpy/test_random_mtrand.py

raw
1 # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2 # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3 # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
4
5 from __future__ import annotations
6
7 import unittest
8 from typing import ClassVar
9
10 try:
11 import numpy # pylint: disable=unused-import
12
13 HAS_NUMPY = True
14 except ImportError:
15 HAS_NUMPY = False
16
17 from astroid import builder, nodes
18
19
20 @unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
21 class NumpyBrainRandomMtrandTest(unittest.TestCase):
22 """Test of all the functions of numpy.random.mtrand module."""
23
24 # Map between functions names and arguments names and default values
25 all_mtrand: ClassVar[dict[str, tuple]] = {
26 "beta": (["a", "b", "size"], [None]),
27 "binomial": (["n", "p", "size"], [None]),
28 "bytes": (["length"], []),
29 "chisquare": (["df", "size"], [None]),
30 "choice": (["a", "size", "replace", "p"], [None, True, None]),
31 "dirichlet": (["alpha", "size"], [None]),
32 "exponential": (["scale", "size"], [1.0, None]),
33 "f": (["dfnum", "dfden", "size"], [None]),
34 "gamma": (["shape", "scale", "size"], [1.0, None]),
35 "geometric": (["p", "size"], [None]),
36 "get_state": ([], []),
37 "gumbel": (["loc", "scale", "size"], [0.0, 1.0, None]),
38 "hypergeometric": (["ngood", "nbad", "nsample", "size"], [None]),
39 "laplace": (["loc", "scale", "size"], [0.0, 1.0, None]),
40 "logistic": (["loc", "scale", "size"], [0.0, 1.0, None]),
41 "lognormal": (["mean", "sigma", "size"], [0.0, 1.0, None]),
42 "logseries": (["p", "size"], [None]),
43 "multinomial": (["n", "pvals", "size"], [None]),
44 "multivariate_normal": (["mean", "cov", "size"], [None]),
45 "negative_binomial": (["n", "p", "size"], [None]),
46 "noncentral_chisquare": (["df", "nonc", "size"], [None]),
47 "noncentral_f": (["dfnum", "dfden", "nonc", "size"], [None]),
48 "normal": (["loc", "scale", "size"], [0.0, 1.0, None]),
49 "pareto": (["a", "size"], [None]),
50 "permutation": (["x"], []),
51 "poisson": (["lam", "size"], [1.0, None]),
52 "power": (["a", "size"], [None]),
53 "rand": (["args"], []),
54 "randint": (["low", "high", "size", "dtype"], [None, None, "l"]),
55 "randn": (["args"], []),
56 "random": (["size"], [None]),
57 "random_integers": (["low", "high", "size"], [None, None]),
58 "random_sample": (["size"], [None]),
59 "rayleigh": (["scale", "size"], [1.0, None]),
60 "seed": (["seed"], [None]),
61 "set_state": (["state"], []),
62 "shuffle": (["x"], []),
63 "standard_cauchy": (["size"], [None]),
64 "standard_exponential": (["size"], [None]),
65 "standard_gamma": (["shape", "size"], [None]),
66 "standard_normal": (["size"], [None]),
67 "standard_t": (["df", "size"], [None]),
68 "triangular": (["left", "mode", "right", "size"], [None]),
69 "uniform": (["low", "high", "size"], [0.0, 1.0, None]),
70 "vonmises": (["mu", "kappa", "size"], [None]),
71 "wald": (["mean", "scale", "size"], [None]),
72 "weibull": (["a", "size"], [None]),
73 "zipf": (["a", "size"], [None]),
74 }
75
76 def _inferred_numpy_attribute(self, func_name):
77 node = builder.extract_node(f"""
78 import numpy.random.mtrand as tested_module
79 func = tested_module.{func_name:s}
80 func""")
81 return next(node.infer())
82
83 def test_numpy_random_mtrand_functions(self):
84 """Test that all functions have FunctionDef type."""
85 for func in self.all_mtrand:
86 with self.subTest(func=func):
87 inferred = self._inferred_numpy_attribute(func)
88 self.assertIsInstance(inferred, nodes.FunctionDef)
89
90 def test_numpy_random_mtrand_functions_signature(self):
91 """Test the arguments names and default values."""
92 for (
93 func,
94 (exact_arg_names, exact_kwargs_default_values),
95 ) in self.all_mtrand.items():
96 with self.subTest(func=func):
97 inferred = self._inferred_numpy_attribute(func)
98 self.assertEqual(inferred.argnames(), exact_arg_names)
99 default_args_values = [
100 default.value for default in inferred.args.defaults
101 ]
102 self.assertEqual(default_args_values, exact_kwargs_default_values)