Debian Codegraph @

tests/brain/numpy/test_core_einsumfunc.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 pytest
8
9 from astroid import builder, nodes
10
11 try:
12 import numpy # pylint: disable=unused-import
13
14 HAS_NUMPY = True
15 except ImportError:
16 HAS_NUMPY = False
17
18
19 def _inferred_numpy_func_call(func_name: str, *func_args: str) -> nodes.FunctionDef:
20 node = builder.extract_node(f"""
21 import numpy as np
22 func = np.{func_name:s}
23 func({','.join(func_args):s})
24 """)
25 return node.infer()
26
27
28 @pytest.mark.skipif(not HAS_NUMPY, reason="This test requires the numpy library.")
29 def test_numpy_function_calls_inferred_as_ndarray() -> None:
30 """Test that calls to numpy functions are inferred as numpy.ndarray."""
31 method = "einsum"
32 inferred_values = list(
33 _inferred_numpy_func_call(method, "ii, np.arange(25).reshape(5, 5)")
34 )
35
36 assert len(inferred_values) == 1, f"Too much inferred value for {method:s}"
37 assert (
38 inferred_values[-1].pytype() == ".ndarray"
39 ), f"Illicit type for {method:s} ({inferred_values[-1].pytype()})"
40
41
42 @pytest.mark.skipif(not HAS_NUMPY, reason="This test requires the numpy library.")
43 def test_function_parameters() -> None:
44 instance = builder.extract_node("""
45 import numpy
46 numpy.einsum #@
47 """)
48 actual_args = instance.inferred()[0].args
49
50 assert actual_args.vararg == "operands"
51 assert [arg.name for arg in actual_args.kwonlyargs] == ["out", "optimize"]
52 assert actual_args.kwarg == "kwargs"