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# TODO(hippo91) : correct the methods signature.
6
7"""Astroid hooks for numpy.core.numerictypes module."""
8from astroid import nodes
9from astroid.brain.brain_numpy_utils import numpy_supports_type_hints
10from astroid.brain.helpers import register_module_extender
11from astroid.builder import parse
12from astroid.manager import AstroidManager
13
14
15def numpy_core_numerictypes_transform() -> nodes.Module:
16 # TODO: Uniformize the generic API with the ndarray one.
17 # According to numpy doc the generic object should expose
18 # the same API than ndarray. This has been done here partially
19 # through the astype method.
20 generic_src = """
21 class generic(object):
22 def __init__(self, value):
23 self.T = np.ndarray([0, 0])
24 self.base = None
25 self.data = None
26 self.dtype = None
27 self.flags = None
28 # Should be a numpy.flatiter instance but not available for now
29 # Putting an array instead so that iteration and indexing are authorized
30 self.flat = np.ndarray([0, 0])
31 self.imag = None
32 self.itemsize = None
33 self.nbytes = None
34 self.ndim = None
35 self.real = None
36 self.size = None
37 self.strides = None
38
39 def all(self): return uninferable
40 def any(self): return uninferable
41 def argmax(self): return uninferable
42 def argmin(self): return uninferable
43 def argsort(self): return uninferable
44 def astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True): return np.ndarray([0, 0])
45 def base(self): return uninferable
46 def byteswap(self): return uninferable
47 def choose(self): return uninferable
48 def clip(self): return uninferable
49 def compress(self): return uninferable
50 def conj(self): return uninferable
51 def conjugate(self): return uninferable
52 def copy(self): return uninferable
53 def cumprod(self): return uninferable
54 def cumsum(self): return uninferable
55 def data(self): return uninferable
56 def diagonal(self): return uninferable
57 def dtype(self): return uninferable
58 def dump(self): return uninferable
59 def dumps(self): return uninferable
60 def fill(self): return uninferable
61 def flags(self): return uninferable
62 def flat(self): return uninferable
63 def flatten(self): return uninferable
64 def getfield(self): return uninferable
65 def imag(self): return uninferable
66 def item(self): return uninferable
67 def itemset(self): return uninferable
68 def itemsize(self): return uninferable
69 def max(self): return uninferable
70 def mean(self): return uninferable
71 def min(self): return uninferable
72 def nbytes(self): return uninferable
73 def ndim(self): return uninferable
74 def newbyteorder(self): return uninferable
75 def nonzero(self): return uninferable
76 def prod(self): return uninferable
77 def ptp(self): return uninferable
78 def put(self): return uninferable
79 def ravel(self): return uninferable
80 def real(self): return uninferable
81 def repeat(self): return uninferable
82 def reshape(self): return uninferable
83 def resize(self): return uninferable
84 def round(self): return uninferable
85 def searchsorted(self): return uninferable
86 def setfield(self): return uninferable
87 def setflags(self): return uninferable
88 def shape(self): return uninferable
89 def size(self): return uninferable
90 def sort(self): return uninferable
91 def squeeze(self): return uninferable
92 def std(self): return uninferable
93 def strides(self): return uninferable
94 def sum(self): return uninferable
95 def swapaxes(self): return uninferable
96 def take(self): return uninferable
97 def tobytes(self): return uninferable
98 def tofile(self): return uninferable
99 def tolist(self): return uninferable
100 def tostring(self): return uninferable
101 def trace(self): return uninferable
102 def transpose(self): return uninferable
103 def var(self): return uninferable
104 def view(self): return uninferable
105 """
106 if numpy_supports_type_hints():
107 generic_src += """
108 @classmethod
109 def __class_getitem__(cls, value):
110 return cls
111 """
112 return parse(
113 generic_src
114 + """
115 class dtype(object):
116 def __init__(self, obj, align=False, copy=False):
117 self.alignment = None
118 self.base = None
119 self.byteorder = None
120 self.char = None
121 self.descr = None
122 self.fields = None
123 self.flags = None
124 self.hasobject = None
125 self.isalignedstruct = None
126 self.isbuiltin = None
127 self.isnative = None
128 self.itemsize = None
129 self.kind = None
130 self.metadata = None
131 self.name = None
132 self.names = None
133 self.num = None
134 self.shape = None
135 self.str = None
136 self.subdtype = None
137 self.type = None
138
139 def newbyteorder(self, new_order='S'): return uninferable
140 def __neg__(self): return uninferable
141
142 class busdaycalendar(object):
143 def __init__(self, weekmask='1111100', holidays=None):
144 self.holidays = None
145 self.weekmask = None
146
147 class flexible(generic): pass
148 class bool_(generic): pass
149 class number(generic):
150 def __neg__(self): return uninferable
151 class datetime64(generic):
152 def __init__(self, nb, unit=None): pass
153
154
155 class void(flexible):
156 def __init__(self, *args, **kwargs):
157 self.base = None
158 self.dtype = None
159 self.flags = None
160 def getfield(self): return uninferable
161 def setfield(self): return uninferable
162
163
164 class character(flexible): pass
165
166
167 class integer(number):
168 def __init__(self, value):
169 self.denominator = None
170 self.numerator = None
171
172
173 class inexact(number): pass
174
175
176 class str_(str, character):
177 def maketrans(self, x, y=None, z=None): return uninferable
178
179
180 class bytes_(bytes, character):
181 def fromhex(self, string): return uninferable
182 def maketrans(self, frm, to): return uninferable
183
184
185 class signedinteger(integer): pass
186
187
188 class unsignedinteger(integer): pass
189
190
191 class complexfloating(inexact): pass
192
193
194 class floating(inexact): pass
195
196
197 class float64(floating, float):
198 def fromhex(self, string): return uninferable
199
200
201 class uint64(unsignedinteger): pass
202 class complex64(complexfloating): pass
203 class int16(signedinteger): pass
204 class float96(floating): pass
205 class int8(signedinteger): pass
206 class uint32(unsignedinteger): pass
207 class uint8(unsignedinteger): pass
208 class _typedict(dict): pass
209 class complex192(complexfloating): pass
210 class timedelta64(signedinteger):
211 def __init__(self, nb, unit=None): pass
212 class int32(signedinteger): pass
213 class uint16(unsignedinteger): pass
214 class float32(floating): pass
215 class complex128(complexfloating, complex): pass
216 class float16(floating): pass
217 class int64(signedinteger): pass
218
219 buffer_type = memoryview
220 bool8 = bool_
221 byte = int8
222 bytes0 = bytes_
223 cdouble = complex128
224 cfloat = complex128
225 clongdouble = complex192
226 clongfloat = complex192
227 complex_ = complex128
228 csingle = complex64
229 double = float64
230 float_ = float64
231 half = float16
232 int0 = int32
233 int_ = int32
234 intc = int32
235 intp = int32
236 long = int32
237 longcomplex = complex192
238 longdouble = float96
239 longfloat = float96
240 longlong = int64
241 object0 = object_
242 object_ = object_
243 short = int16
244 single = float32
245 singlecomplex = complex64
246 str0 = str_
247 string_ = bytes_
248 ubyte = uint8
249 uint = uint32
250 uint0 = uint32
251 uintc = uint32
252 uintp = uint32
253 ulonglong = uint64
254 unicode = str_
255 unicode_ = str_
256 ushort = uint16
257 void0 = void
258 """
259 )
260
261
262def register(manager: AstroidManager) -> None:
263 register_module_extender(
264 manager, "numpy.core.numerictypes", numpy_core_numerictypes_transform
265 )