brain_numpy_ndarray.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
  4. """Astroid hooks for numpy ndarray class."""
  5. from __future__ import annotations
  6. from astroid.brain.brain_numpy_utils import numpy_supports_type_hints
  7. from astroid.builder import extract_node
  8. from astroid.context import InferenceContext
  9. from astroid.inference_tip import inference_tip
  10. from astroid.manager import AstroidManager
  11. from astroid.nodes.node_classes import Attribute
  12. def infer_numpy_ndarray(node, context: InferenceContext | None = None):
  13. ndarray = """
  14. class ndarray(object):
  15. def __init__(self, shape, dtype=float, buffer=None, offset=0,
  16. strides=None, order=None):
  17. self.T = numpy.ndarray([0, 0])
  18. self.base = None
  19. self.ctypes = None
  20. self.data = None
  21. self.dtype = None
  22. self.flags = None
  23. # Should be a numpy.flatiter instance but not available for now
  24. # Putting an array instead so that iteration and indexing are authorized
  25. self.flat = np.ndarray([0, 0])
  26. self.imag = np.ndarray([0, 0])
  27. self.itemsize = None
  28. self.nbytes = None
  29. self.ndim = None
  30. self.real = np.ndarray([0, 0])
  31. self.shape = numpy.ndarray([0, 0])
  32. self.size = None
  33. self.strides = None
  34. def __abs__(self): return numpy.ndarray([0, 0])
  35. def __add__(self, value): return numpy.ndarray([0, 0])
  36. def __and__(self, value): return numpy.ndarray([0, 0])
  37. def __array__(self, dtype=None): return numpy.ndarray([0, 0])
  38. def __array_wrap__(self, obj): return numpy.ndarray([0, 0])
  39. def __contains__(self, key): return True
  40. def __copy__(self): return numpy.ndarray([0, 0])
  41. def __deepcopy__(self, memo): return numpy.ndarray([0, 0])
  42. def __divmod__(self, value): return (numpy.ndarray([0, 0]), numpy.ndarray([0, 0]))
  43. def __eq__(self, value): return numpy.ndarray([0, 0])
  44. def __float__(self): return 0.
  45. def __floordiv__(self): return numpy.ndarray([0, 0])
  46. def __ge__(self, value): return numpy.ndarray([0, 0])
  47. def __getitem__(self, key): return uninferable
  48. def __gt__(self, value): return numpy.ndarray([0, 0])
  49. def __iadd__(self, value): return numpy.ndarray([0, 0])
  50. def __iand__(self, value): return numpy.ndarray([0, 0])
  51. def __ifloordiv__(self, value): return numpy.ndarray([0, 0])
  52. def __ilshift__(self, value): return numpy.ndarray([0, 0])
  53. def __imod__(self, value): return numpy.ndarray([0, 0])
  54. def __imul__(self, value): return numpy.ndarray([0, 0])
  55. def __int__(self): return 0
  56. def __invert__(self): return numpy.ndarray([0, 0])
  57. def __ior__(self, value): return numpy.ndarray([0, 0])
  58. def __ipow__(self, value): return numpy.ndarray([0, 0])
  59. def __irshift__(self, value): return numpy.ndarray([0, 0])
  60. def __isub__(self, value): return numpy.ndarray([0, 0])
  61. def __itruediv__(self, value): return numpy.ndarray([0, 0])
  62. def __ixor__(self, value): return numpy.ndarray([0, 0])
  63. def __le__(self, value): return numpy.ndarray([0, 0])
  64. def __len__(self): return 1
  65. def __lshift__(self, value): return numpy.ndarray([0, 0])
  66. def __lt__(self, value): return numpy.ndarray([0, 0])
  67. def __matmul__(self, value): return numpy.ndarray([0, 0])
  68. def __mod__(self, value): return numpy.ndarray([0, 0])
  69. def __mul__(self, value): return numpy.ndarray([0, 0])
  70. def __ne__(self, value): return numpy.ndarray([0, 0])
  71. def __neg__(self): return numpy.ndarray([0, 0])
  72. def __or__(self, value): return numpy.ndarray([0, 0])
  73. def __pos__(self): return numpy.ndarray([0, 0])
  74. def __pow__(self): return numpy.ndarray([0, 0])
  75. def __repr__(self): return str()
  76. def __rshift__(self): return numpy.ndarray([0, 0])
  77. def __setitem__(self, key, value): return uninferable
  78. def __str__(self): return str()
  79. def __sub__(self, value): return numpy.ndarray([0, 0])
  80. def __truediv__(self, value): return numpy.ndarray([0, 0])
  81. def __xor__(self, value): return numpy.ndarray([0, 0])
  82. def all(self, axis=None, out=None, keepdims=False): return np.ndarray([0, 0])
  83. def any(self, axis=None, out=None, keepdims=False): return np.ndarray([0, 0])
  84. def argmax(self, axis=None, out=None): return np.ndarray([0, 0])
  85. def argmin(self, axis=None, out=None): return np.ndarray([0, 0])
  86. def argpartition(self, kth, axis=-1, kind='introselect', order=None): return np.ndarray([0, 0])
  87. def argsort(self, axis=-1, kind='quicksort', order=None): return np.ndarray([0, 0])
  88. def astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True): return np.ndarray([0, 0])
  89. def byteswap(self, inplace=False): return np.ndarray([0, 0])
  90. def choose(self, choices, out=None, mode='raise'): return np.ndarray([0, 0])
  91. def clip(self, min=None, max=None, out=None): return np.ndarray([0, 0])
  92. def compress(self, condition, axis=None, out=None): return np.ndarray([0, 0])
  93. def conj(self): return np.ndarray([0, 0])
  94. def conjugate(self): return np.ndarray([0, 0])
  95. def copy(self, order='C'): return np.ndarray([0, 0])
  96. def cumprod(self, axis=None, dtype=None, out=None): return np.ndarray([0, 0])
  97. def cumsum(self, axis=None, dtype=None, out=None): return np.ndarray([0, 0])
  98. def diagonal(self, offset=0, axis1=0, axis2=1): return np.ndarray([0, 0])
  99. def dot(self, b, out=None): return np.ndarray([0, 0])
  100. def dump(self, file): return None
  101. def dumps(self): return str()
  102. def fill(self, value): return None
  103. def flatten(self, order='C'): return np.ndarray([0, 0])
  104. def getfield(self, dtype, offset=0): return np.ndarray([0, 0])
  105. def item(self, *args): return uninferable
  106. def itemset(self, *args): return None
  107. def max(self, axis=None, out=None): return np.ndarray([0, 0])
  108. def mean(self, axis=None, dtype=None, out=None, keepdims=False): return np.ndarray([0, 0])
  109. def min(self, axis=None, out=None, keepdims=False): return np.ndarray([0, 0])
  110. def newbyteorder(self, new_order='S'): return np.ndarray([0, 0])
  111. def nonzero(self): return (1,)
  112. def partition(self, kth, axis=-1, kind='introselect', order=None): return None
  113. def prod(self, axis=None, dtype=None, out=None, keepdims=False): return np.ndarray([0, 0])
  114. def ptp(self, axis=None, out=None): return np.ndarray([0, 0])
  115. def put(self, indices, values, mode='raise'): return None
  116. def ravel(self, order='C'): return np.ndarray([0, 0])
  117. def repeat(self, repeats, axis=None): return np.ndarray([0, 0])
  118. def reshape(self, shape, order='C'): return np.ndarray([0, 0])
  119. def resize(self, new_shape, refcheck=True): return None
  120. def round(self, decimals=0, out=None): return np.ndarray([0, 0])
  121. def searchsorted(self, v, side='left', sorter=None): return np.ndarray([0, 0])
  122. def setfield(self, val, dtype, offset=0): return None
  123. def setflags(self, write=None, align=None, uic=None): return None
  124. def sort(self, axis=-1, kind='quicksort', order=None): return None
  125. def squeeze(self, axis=None): return np.ndarray([0, 0])
  126. def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False): return np.ndarray([0, 0])
  127. def sum(self, axis=None, dtype=None, out=None, keepdims=False): return np.ndarray([0, 0])
  128. def swapaxes(self, axis1, axis2): return np.ndarray([0, 0])
  129. def take(self, indices, axis=None, out=None, mode='raise'): return np.ndarray([0, 0])
  130. def tobytes(self, order='C'): return b''
  131. def tofile(self, fid, sep="", format="%s"): return None
  132. def tolist(self, ): return []
  133. def tostring(self, order='C'): return b''
  134. def trace(self, offset=0, axis1=0, axis2=1, dtype=None, out=None): return np.ndarray([0, 0])
  135. def transpose(self, *axes): return np.ndarray([0, 0])
  136. def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False): return np.ndarray([0, 0])
  137. def view(self, dtype=None, type=None): return np.ndarray([0, 0])
  138. """
  139. if numpy_supports_type_hints():
  140. ndarray += """
  141. @classmethod
  142. def __class_getitem__(cls, value):
  143. return cls
  144. """
  145. node = extract_node(ndarray)
  146. return node.infer(context=context)
  147. def _looks_like_numpy_ndarray(node) -> bool:
  148. return isinstance(node, Attribute) and node.attrname == "ndarray"
  149. AstroidManager().register_transform(
  150. Attribute,
  151. inference_tip(infer_numpy_ndarray),
  152. _looks_like_numpy_ndarray,
  153. )