Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/astroid/brain/brain_numpy_utils.py: 60%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

30 statements  

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 import nodes 

10from astroid.builder import extract_node 

11from astroid.context import InferenceContext 

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: nodes.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], 

46 node: nodes.Attribute, 

47 context: InferenceContext | None = None, 

48): 

49 extracted_node = extract_node(sources[node.attrname]) 

50 return extracted_node.infer(context=context) 

51 

52 

53def _is_a_numpy_module(node: nodes.Name) -> bool: 

54 """ 

55 Returns True if the node is a representation of a numpy module. 

56 

57 For example in : 

58 import numpy as np 

59 x = np.linspace(1, 2) 

60 The node <Name.np> is a representation of the numpy module. 

61 

62 :param node: node to test 

63 :return: True if the node is a representation of the numpy module. 

64 """ 

65 module_nickname = node.name 

66 potential_import_target = [ 

67 x for x in node.lookup(module_nickname)[1] if isinstance(x, nodes.Import) 

68 ] 

69 return any( 

70 ("numpy", module_nickname) in target.names or ("numpy", None) in target.names 

71 for target in potential_import_target 

72 ) 

73 

74 

75def member_name_looks_like_numpy_member( 

76 member_names: frozenset[str], node: nodes.Name 

77) -> bool: 

78 """ 

79 Returns True if the Name node's name matches a member name from numpy 

80 """ 

81 return node.name in member_names and node.root().name.startswith("numpy") 

82 

83 

84def attribute_name_looks_like_numpy_member( 

85 member_names: frozenset[str], node: nodes.Attribute 

86) -> bool: 

87 """ 

88 Returns True if the Attribute node's name matches a member name from numpy 

89 """ 

90 return ( 

91 node.attrname in member_names 

92 and isinstance(node.expr, nodes.Name) 

93 and _is_a_numpy_module(node.expr) 

94 )