brain_numpy_ndarray.py 8.9 KB

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