1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
# ruff: noqa: ANN401
from typing import (
Any,
Generic,
LiteralString,
Never,
NoReturn,
Self,
TypeAlias,
final,
overload,
type_check_only,
)
from typing import Literal as L
from typing_extensions import TypeVar
import numpy as np
__all__ = [ # noqa: RUF022
'BoolDType',
'Int8DType',
'ByteDType',
'UInt8DType',
'UByteDType',
'Int16DType',
'ShortDType',
'UInt16DType',
'UShortDType',
'Int32DType',
'IntDType',
'UInt32DType',
'UIntDType',
'Int64DType',
'LongDType',
'UInt64DType',
'ULongDType',
'LongLongDType',
'ULongLongDType',
'Float16DType',
'Float32DType',
'Float64DType',
'LongDoubleDType',
'Complex64DType',
'Complex128DType',
'CLongDoubleDType',
'ObjectDType',
'BytesDType',
'StrDType',
'VoidDType',
'DateTime64DType',
'TimeDelta64DType',
'StringDType',
]
# Helper base classes (typing-only)
_ScalarT_co = TypeVar("_ScalarT_co", bound=np.generic, covariant=True)
@type_check_only
class _SimpleDType(np.dtype[_ScalarT_co], Generic[_ScalarT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
names: None # pyright: ignore[reportIncompatibleVariableOverride]
def __new__(cls, /) -> Self: ...
def __getitem__(self, key: Any, /) -> NoReturn: ...
@property
def base(self) -> np.dtype[_ScalarT_co]: ...
@property
def fields(self) -> None: ...
@property
def isalignedstruct(self) -> L[False]: ...
@property
def isnative(self) -> L[True]: ...
@property
def ndim(self) -> L[0]: ...
@property
def shape(self) -> tuple[()]: ...
@property
def subdtype(self) -> None: ...
@type_check_only
class _LiteralDType(_SimpleDType[_ScalarT_co], Generic[_ScalarT_co]): # type: ignore[misc]
@property
def flags(self) -> L[0]: ...
@property
def hasobject(self) -> L[False]: ...
# Helper mixins (typing-only):
_KindT_co = TypeVar("_KindT_co", bound=LiteralString, covariant=True)
_CharT_co = TypeVar("_CharT_co", bound=LiteralString, covariant=True)
_NumT_co = TypeVar("_NumT_co", bound=int, covariant=True)
@type_check_only
class _TypeCodes(Generic[_KindT_co, _CharT_co, _NumT_co]):
@final
@property
def kind(self) -> _KindT_co: ...
@final
@property
def char(self) -> _CharT_co: ...
@final
@property
def num(self) -> _NumT_co: ...
@type_check_only
class _NoOrder:
@final
@property
def byteorder(self) -> L["|"]: ...
@type_check_only
class _NativeOrder:
@final
@property
def byteorder(self) -> L["="]: ...
_DataSize_co = TypeVar("_DataSize_co", bound=int, covariant=True)
_ItemSize_co = TypeVar("_ItemSize_co", bound=int, covariant=True, default=int)
@type_check_only
class _NBit(Generic[_DataSize_co, _ItemSize_co]):
@final
@property
def alignment(self) -> _DataSize_co: ...
@final
@property
def itemsize(self) -> _ItemSize_co: ...
@type_check_only
class _8Bit(_NoOrder, _NBit[L[1], L[1]]): ...
# Boolean:
@final
class BoolDType( # type: ignore[misc]
_TypeCodes[L["b"], L["?"], L[0]],
_8Bit,
_LiteralDType[np.bool],
):
@property
def name(self) -> L["bool"]: ...
@property
def str(self) -> L["|b1"]: ...
# Sized integers:
@final
class Int8DType( # type: ignore[misc]
_TypeCodes[L["i"], L["b"], L[1]],
_8Bit,
_LiteralDType[np.int8],
):
@property
def name(self) -> L["int8"]: ...
@property
def str(self) -> L["|i1"]: ...
@final
class UInt8DType( # type: ignore[misc]
_TypeCodes[L["u"], L["B"], L[2]],
_8Bit,
_LiteralDType[np.uint8],
):
@property
def name(self) -> L["uint8"]: ...
@property
def str(self) -> L["|u1"]: ...
@final
class Int16DType( # type: ignore[misc]
_TypeCodes[L["i"], L["h"], L[3]],
_NativeOrder,
_NBit[L[2], L[2]],
_LiteralDType[np.int16],
):
@property
def name(self) -> L["int16"]: ...
@property
def str(self) -> L["<i2", ">i2"]: ...
@final
class UInt16DType( # type: ignore[misc]
_TypeCodes[L["u"], L["H"], L[4]],
_NativeOrder,
_NBit[L[2], L[2]],
_LiteralDType[np.uint16],
):
@property
def name(self) -> L["uint16"]: ...
@property
def str(self) -> L["<u2", ">u2"]: ...
@final
class Int32DType( # type: ignore[misc]
_TypeCodes[L["i"], L["i", "l"], L[5, 7]],
_NativeOrder,
_NBit[L[4], L[4]],
_LiteralDType[np.int32],
):
@property
def name(self) -> L["int32"]: ...
@property
def str(self) -> L["<i4", ">i4"]: ...
@final
class UInt32DType( # type: ignore[misc]
_TypeCodes[L["u"], L["I", "L"], L[6, 8]],
_NativeOrder,
_NBit[L[4], L[4]],
_LiteralDType[np.uint32],
):
@property
def name(self) -> L["uint32"]: ...
@property
def str(self) -> L["<u4", ">u4"]: ...
@final
class Int64DType( # type: ignore[misc]
_TypeCodes[L["i"], L["l", "q"], L[7, 9]],
_NativeOrder,
_NBit[L[8], L[8]],
_LiteralDType[np.int64],
):
@property
def name(self) -> L["int64"]: ...
@property
def str(self) -> L["<i8", ">i8"]: ...
@final
class UInt64DType( # type: ignore[misc]
_TypeCodes[L["u"], L["L", "Q"], L[8, 10]],
_NativeOrder,
_NBit[L[8], L[8]],
_LiteralDType[np.uint64],
):
@property
def name(self) -> L["uint64"]: ...
@property
def str(self) -> L["<u8", ">u8"]: ...
# Standard C-named version/alias:
# NOTE: Don't make these `Final`: it will break stubtest
ByteDType = Int8DType
UByteDType = UInt8DType
ShortDType = Int16DType
UShortDType = UInt16DType
@final
class IntDType( # type: ignore[misc]
_TypeCodes[L["i"], L["i"], L[5]],
_NativeOrder,
_NBit[L[4], L[4]],
_LiteralDType[np.intc],
):
@property
def name(self) -> L["int32"]: ...
@property
def str(self) -> L["<i4", ">i4"]: ...
@final
class UIntDType( # type: ignore[misc]
_TypeCodes[L["u"], L["I"], L[6]],
_NativeOrder,
_NBit[L[4], L[4]],
_LiteralDType[np.uintc],
):
@property
def name(self) -> L["uint32"]: ...
@property
def str(self) -> L["<u4", ">u4"]: ...
@final
class LongDType( # type: ignore[misc]
_TypeCodes[L["i"], L["l"], L[7]],
_NativeOrder,
_NBit[L[4, 8], L[4, 8]],
_LiteralDType[np.long],
):
@property
def name(self) -> L["int32", "int64"]: ...
@property
def str(self) -> L["<i4", ">i4", "<i8", ">i8"]: ...
@final
class ULongDType( # type: ignore[misc]
_TypeCodes[L["u"], L["L"], L[8]],
_NativeOrder,
_NBit[L[4, 8], L[4, 8]],
_LiteralDType[np.ulong],
):
@property
def name(self) -> L["uint32", "uint64"]: ...
@property
def str(self) -> L["<u4", ">u4", "<u8", ">u8"]: ...
@final
class LongLongDType( # type: ignore[misc]
_TypeCodes[L["i"], L["q"], L[9]],
_NativeOrder,
_NBit[L[8], L[8]],
_LiteralDType[np.longlong],
):
@property
def name(self) -> L["int64"]: ...
@property
def str(self) -> L["<i8", ">i8"]: ...
@final
class ULongLongDType( # type: ignore[misc]
_TypeCodes[L["u"], L["Q"], L[10]],
_NativeOrder,
_NBit[L[8], L[8]],
_LiteralDType[np.ulonglong],
):
@property
def name(self) -> L["uint64"]: ...
@property
def str(self) -> L["<u8", ">u8"]: ...
# Floats:
@final
class Float16DType( # type: ignore[misc]
_TypeCodes[L["f"], L["e"], L[23]],
_NativeOrder,
_NBit[L[2], L[2]],
_LiteralDType[np.float16],
):
@property
def name(self) -> L["float16"]: ...
@property
def str(self) -> L["<f2", ">f2"]: ...
@final
class Float32DType( # type: ignore[misc]
_TypeCodes[L["f"], L["f"], L[11]],
_NativeOrder,
_NBit[L[4], L[4]],
_LiteralDType[np.float32],
):
@property
def name(self) -> L["float32"]: ...
@property
def str(self) -> L["<f4", ">f4"]: ...
@final
class Float64DType( # type: ignore[misc]
_TypeCodes[L["f"], L["d"], L[12]],
_NativeOrder,
_NBit[L[8], L[8]],
_LiteralDType[np.float64],
):
@property
def name(self) -> L["float64"]: ...
@property
def str(self) -> L["<f8", ">f8"]: ...
@final
class LongDoubleDType( # type: ignore[misc]
_TypeCodes[L["f"], L["g"], L[13]],
_NativeOrder,
_NBit[L[8, 12, 16], L[8, 12, 16]],
_LiteralDType[np.longdouble],
):
@property
def name(self) -> L["float64", "float96", "float128"]: ...
@property
def str(self) -> L["<f8", ">f8", "<f12", ">f12", "<f16", ">f16"]: ...
# Complex:
@final
class Complex64DType( # type: ignore[misc]
_TypeCodes[L["c"], L["F"], L[14]],
_NativeOrder,
_NBit[L[4], L[8]],
_LiteralDType[np.complex64],
):
@property
def name(self) -> L["complex64"]: ...
@property
def str(self) -> L["<c8", ">c8"]: ...
@final
class Complex128DType( # type: ignore[misc]
_TypeCodes[L["c"], L["D"], L[15]],
_NativeOrder,
_NBit[L[8], L[16]],
_LiteralDType[np.complex128],
):
@property
def name(self) -> L["complex128"]: ...
@property
def str(self) -> L["<c16", ">c16"]: ...
@final
class CLongDoubleDType( # type: ignore[misc]
_TypeCodes[L["c"], L["G"], L[16]],
_NativeOrder,
_NBit[L[8, 12, 16], L[16, 24, 32]],
_LiteralDType[np.clongdouble],
):
@property
def name(self) -> L["complex128", "complex192", "complex256"]: ...
@property
def str(self) -> L["<c16", ">c16", "<c24", ">c24", "<c32", ">c32"]: ...
# Python objects:
@final
class ObjectDType( # type: ignore[misc]
_TypeCodes[L["O"], L["O"], L[17]],
_NoOrder,
_NBit[L[8], L[8]],
_SimpleDType[np.object_],
):
@property
def hasobject(self) -> L[True]: ...
@property
def name(self) -> L["object"]: ...
@property
def str(self) -> L["|O"]: ...
# Flexible:
@final
class BytesDType( # type: ignore[misc]
_TypeCodes[L["S"], L["S"], L[18]],
_NoOrder,
_NBit[L[1], _ItemSize_co],
_SimpleDType[np.bytes_],
Generic[_ItemSize_co],
):
def __new__(cls, size: _ItemSize_co, /) -> BytesDType[_ItemSize_co]: ...
@property
def hasobject(self) -> L[False]: ...
@property
def name(self) -> LiteralString: ...
@property
def str(self) -> LiteralString: ...
@final
class StrDType( # type: ignore[misc]
_TypeCodes[L["U"], L["U"], L[19]],
_NativeOrder,
_NBit[L[4], _ItemSize_co],
_SimpleDType[np.str_],
Generic[_ItemSize_co],
):
def __new__(cls, size: _ItemSize_co, /) -> StrDType[_ItemSize_co]: ...
@property
def hasobject(self) -> L[False]: ...
@property
def name(self) -> LiteralString: ...
@property
def str(self) -> LiteralString: ...
@final
class VoidDType( # type: ignore[misc]
_TypeCodes[L["V"], L["V"], L[20]],
_NoOrder,
_NBit[L[1], _ItemSize_co],
np.dtype[np.void], # pyright: ignore[reportGeneralTypeIssues]
Generic[_ItemSize_co],
):
# NOTE: `VoidDType(...)` raises a `TypeError` at the moment
def __new__(cls, length: _ItemSize_co, /) -> NoReturn: ...
@property
def base(self) -> Self: ...
@property
def isalignedstruct(self) -> L[False]: ...
@property
def isnative(self) -> L[True]: ...
@property
def ndim(self) -> L[0]: ...
@property
def shape(self) -> tuple[()]: ...
@property
def subdtype(self) -> None: ...
@property
def name(self) -> LiteralString: ...
@property
def str(self) -> LiteralString: ...
# Other:
_DateUnit: TypeAlias = L["Y", "M", "W", "D"]
_TimeUnit: TypeAlias = L["h", "m", "s", "ms", "us", "ns", "ps", "fs", "as"]
_DateTimeUnit: TypeAlias = _DateUnit | _TimeUnit
@final
class DateTime64DType( # type: ignore[misc]
_TypeCodes[L["M"], L["M"], L[21]],
_NativeOrder,
_NBit[L[8], L[8]],
_LiteralDType[np.datetime64],
):
# NOTE: `DateTime64DType(...)` raises a `TypeError` at the moment
# TODO: Once implemented, don't forget the`unit: L["μs"]` overload.
def __new__(cls, unit: _DateTimeUnit, /) -> NoReturn: ...
@property
def name(self) -> L[
"datetime64",
"datetime64[Y]",
"datetime64[M]",
"datetime64[W]",
"datetime64[D]",
"datetime64[h]",
"datetime64[m]",
"datetime64[s]",
"datetime64[ms]",
"datetime64[us]",
"datetime64[ns]",
"datetime64[ps]",
"datetime64[fs]",
"datetime64[as]",
]: ...
@property
def str(self) -> L[
"<M8", ">M8",
"<M8[Y]", ">M8[Y]",
"<M8[M]", ">M8[M]",
"<M8[W]", ">M8[W]",
"<M8[D]", ">M8[D]",
"<M8[h]", ">M8[h]",
"<M8[m]", ">M8[m]",
"<M8[s]", ">M8[s]",
"<M8[ms]", ">M8[ms]",
"<M8[us]", ">M8[us]",
"<M8[ns]", ">M8[ns]",
"<M8[ps]", ">M8[ps]",
"<M8[fs]", ">M8[fs]",
"<M8[as]", ">M8[as]",
]: ...
@final
class TimeDelta64DType( # type: ignore[misc]
_TypeCodes[L["m"], L["m"], L[22]],
_NativeOrder,
_NBit[L[8], L[8]],
_LiteralDType[np.timedelta64],
):
# NOTE: `TimeDelta64DType(...)` raises a `TypeError` at the moment
# TODO: Once implemented, don't forget to overload on `unit: L["μs"]`.
def __new__(cls, unit: _DateTimeUnit, /) -> NoReturn: ...
@property
def name(self) -> L[
"timedelta64",
"timedelta64[Y]",
"timedelta64[M]",
"timedelta64[W]",
"timedelta64[D]",
"timedelta64[h]",
"timedelta64[m]",
"timedelta64[s]",
"timedelta64[ms]",
"timedelta64[us]",
"timedelta64[ns]",
"timedelta64[ps]",
"timedelta64[fs]",
"timedelta64[as]",
]: ...
@property
def str(self) -> L[
"<m8", ">m8",
"<m8[Y]", ">m8[Y]",
"<m8[M]", ">m8[M]",
"<m8[W]", ">m8[W]",
"<m8[D]", ">m8[D]",
"<m8[h]", ">m8[h]",
"<m8[m]", ">m8[m]",
"<m8[s]", ">m8[s]",
"<m8[ms]", ">m8[ms]",
"<m8[us]", ">m8[us]",
"<m8[ns]", ">m8[ns]",
"<m8[ps]", ">m8[ps]",
"<m8[fs]", ">m8[fs]",
"<m8[as]", ">m8[as]",
]: ...
_NaObjectT_co = TypeVar("_NaObjectT_co", default=Never, covariant=True)
@final
class StringDType( # type: ignore[misc]
_TypeCodes[L["T"], L["T"], L[2056]],
_NativeOrder,
_NBit[L[8], L[16]],
# TODO(jorenham): change once we have a string scalar type:
# https://github.com/numpy/numpy/issues/28165
np.dtype[str], # type: ignore[type-var] # pyright: ignore[reportGeneralTypeIssues, reportInvalidTypeArguments]
Generic[_NaObjectT_co],
):
@property
def na_object(self) -> _NaObjectT_co: ...
@property
def coerce(self) -> L[True]: ...
#
@overload
def __new__(cls, /, *, coerce: bool = True) -> Self: ...
@overload
def __new__(cls, /, *, na_object: _NaObjectT_co, coerce: bool = True) -> Self: ...
#
def __getitem__(self, key: Never, /) -> NoReturn: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
@property
def fields(self) -> None: ...
@property
def base(self) -> Self: ...
@property
def ndim(self) -> L[0]: ...
@property
def shape(self) -> tuple[()]: ...
#
@property
def name(self) -> L["StringDType64", "StringDType128"]: ...
@property
def subdtype(self) -> None: ...
@property
def type(self) -> type[str]: ...
@property
def str(self) -> L["|T8", "|T16"]: ...
#
@property
def hasobject(self) -> L[True]: ...
@property
def isalignedstruct(self) -> L[False]: ...
@property
def isnative(self) -> L[True]: ...
|