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"""Different utilities for the numpy brains."""
6
7from __future__ import annotations
8
9from astroid.builder import extract_node
10from astroid.context import InferenceContext
11from astroid.nodes.node_classes import Attribute, Import, Name
12
13# Class subscript is available in numpy starting with version 1.20.0
14NUMPY_VERSION_TYPE_HINTS_SUPPORT = ("1", "20", "0")
15
16
17def numpy_supports_type_hints() -> bool:
18 """Returns True if numpy supports type hints."""
19 np_ver = _get_numpy_version()
20 return np_ver and np_ver > NUMPY_VERSION_TYPE_HINTS_SUPPORT
21
22
23def _get_numpy_version() -> tuple[str, str, str]:
24 """
25 Return the numpy version number if numpy can be imported.
26
27 Otherwise returns ('0', '0', '0')
28 """
29 try:
30 import numpy # pylint: disable=import-outside-toplevel
31
32 return tuple(numpy.version.version.split("."))
33 except (ImportError, AttributeError):
34 return ("0", "0", "0")
35
36
37def infer_numpy_name(
38 sources: dict[str, str], node: Name, context: InferenceContext | None = None
39):
40 extracted_node = extract_node(sources[node.name])
41 return extracted_node.infer(context=context)
42
43
44def infer_numpy_attribute(
45 sources: dict[str, str], node: Attribute, context: InferenceContext | None = None
46):
47 extracted_node = extract_node(sources[node.attrname])
48 return extracted_node.infer(context=context)
49
50
51def _is_a_numpy_module(node: Name) -> bool:
52 """
53 Returns True if the node is a representation of a numpy module.
54
55 For example in :
56 import numpy as np
57 x = np.linspace(1, 2)
58 The node <Name.np> is a representation of the numpy module.
59
60 :param node: node to test
61 :return: True if the node is a representation of the numpy module.
62 """
63 module_nickname = node.name
64 potential_import_target = [
65 x for x in node.lookup(module_nickname)[1] if isinstance(x, Import)
66 ]
67 return any(
68 ("numpy", module_nickname) in target.names or ("numpy", None) in target.names
69 for target in potential_import_target
70 )
71
72
73def member_name_looks_like_numpy_member(
74 member_names: frozenset[str], node: Name
75) -> bool:
76 """
77 Returns True if the Name node's name matches a member name from numpy
78 """
79 return node.name in member_names and node.root().name.startswith("numpy")
80
81
82def attribute_name_looks_like_numpy_member(
83 member_names: frozenset[str], node: Attribute
84) -> bool:
85 """
86 Returns True if the Attribute node's name matches a member name from numpy
87 """
88 return (
89 node.attrname in member_names
90 and isinstance(node.expr, Name)
91 and _is_a_numpy_module(node.expr)
92 )