dtypes.pyi 15 KB

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