Skip to content

Commit aeacced

Browse files
authored
🏷️ fix stubtest errors in numpy.dtype and numpy.dtypes.* (#229)
1 parent a679ab2 commit aeacced

File tree

4 files changed

+87
-99
lines changed

4 files changed

+87
-99
lines changed

.mypyignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ numpy\.polynomial\._polytypes
33
numpy\._typing.*
44
numpy\.typing\.mypy_plugin
55

6+
# internal testing code
67
numpy\.conftest
78
numpy(\.\w+)?\.tests.*
9+
numpy.random._generator.__test__
10+
11+
# workaround for the `dtype.type` generic class-attribute that's `None` unless instantiated
12+
numpy(\..+)?\.dtype\.type
813

914
# system-dependent extended precision types
1015
numpy(\..+)?\.float(96|128)
1116
numpy(\..+)?\.complex(192|256)
1217

1318
# these are always either missing float96/complex192 or float128/complex256
14-
numpy.__all__
19+
numpy\.__all__
1520
numpy\._core\.__all__
1621
numpy\._core\.numeric\.__all__
1722
numpy\._core\.numerictypes\.__all__

.mypyignore-todo

Lines changed: 25 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# TODO: https://github.com/numpy/numtype/issues/136
1+
# https://github.com/numpy/numtype/issues/136
22
numpy(\..+)?\.(un)?signedinteger.__hash__
33
numpy(\..+)?\.(un)?signedinteger.__index__
44
numpy(\..+)?\.(un)?signedinteger.bit_count
@@ -8,8 +8,7 @@ numpy(\..+)?\.floating.as_integer_ratio
88
numpy(\..+)?\.complexfloating.__hash__
99
numpy(\..+)?\.complexfloating.__complex__
1010

11-
# ufuncs
12-
# TODO: https://github.com/numpy/numtype/issues/88
11+
# https://github.com/numpy/numtype/issues/88
1312
numpy(\._core(\.(_multiarray_umath|multiarray|numeric|umath))?|\.matlib)?\.f?abs(olute)?
1413
numpy(\._core(\.(_multiarray_umath|multiarray|numeric|umath))?|\.matlib)?\.ceil
1514
numpy(\._core(\.(_multiarray_umath|multiarray|numeric|umath))?|\.matlib)?\.trunc
@@ -64,25 +63,23 @@ numpy(\._core(\.(_multiarray_umath|multiarray|numeric|umath))?|\.matlib)?\.cbrt
6463
numpy(\._core(\.(_multiarray_umath|multiarray|numeric|umath))?|\.matlib)?\.vec(dot|mat)
6564

6665
numpy(\._core(\.arrayprint|\.numeric)?|\.matlib)?\.array2string
67-
numpy(\._core(\.einsumfunc)?)?\.einsum_path
66+
numpy(\._core(\.einsumfunc)?|\.matlib)?\.einsum_path
6867
numpy(\._core(\.memmap)?|\.matlib)?\.memmap\.__new__
69-
numpy(\._core(\.multiarray|\.numeric)?)?\.dtype
70-
numpy(\._core(\.multiarray|\.numeric)?)?\.dtype.type
7168

72-
numpy(\.lib\._arraysetops_impl)?\.union1d
73-
numpy(\.lib\._arraysetops_impl)?\.unique_(all|counts|inverse|values)
74-
numpy(\.lib\._function_base_impl)?\.average
75-
numpy(\.lib\._function_base_impl)?\.corrcoef
76-
numpy(\.lib\._function_base_impl)?\.median
77-
numpy(\.lib\._npyio_impl)?\.genfromtxt
78-
numpy(\.lib\._npyio_impl)?\.save
69+
numpy(\.lib\._arraysetops_impl|\.matlib)?\.union1d
70+
numpy(\.lib\._arraysetops_impl|\.matlib)?\.unique_(all|counts|inverse|values)
71+
numpy(\.lib\._function_base_impl|\.matlib)?\.average
72+
numpy(\.lib\._function_base_impl|\.matlib)?\.corrcoef
73+
numpy(\.lib\._function_base_impl|\.matlib)?\.median
74+
numpy(\.lib\._npyio_impl|\.matlib)?\.genfromtxt
75+
numpy(\.lib\._npyio_impl|\.matlib)?\.save
7976
numpy(\.lib\._nanfunctions_impl)?\.nan(median|percentile)
8077
numpy(\.lib\._twodim_base_impl)?\.tri(l|u)
8178

82-
numpy.matrix.__new__
83-
numpy.ndindex.ndincr
84-
numpy.ndenumerate.iter
85-
numpy.poly1d.integ
79+
numpy(\.matrixlib(\.defmatrix)?|\.matlib)?\.matrix\.__new__
80+
numpy(\.lib\._polynomial_impl|\.matlib)?\.poly1d\.integ
81+
numpy(\.lib\._index_tricks_impl|\.matlib)?\.ndindex\.ndincr
82+
numpy(\.lib\._index_tricks_impl|\.matlib)?\.ndenumerate\.iter
8683

8784
numpy._core.cversions
8885
numpy._core.printoptions
@@ -119,10 +116,10 @@ numpy(\._core\.defchararray|(\._core)?\.strings|\.char)\.isupper
119116
numpy(\._core\.defchararray|(\._core)?\.strings|\.char)\.startswith
120117
numpy(\._core\.defchararray|(\._core)?\.strings|\.char)\.str_len
121118

122-
numpy._core.records.array
123-
numpy._core(.records)?.recarray.__getattr__
124-
numpy._core(.records)?.recarray.__new__
125-
numpy._core(.records)?.record.__name__
119+
numpy(\._core(\.records)?|\.matlib|\.rec)?\.recarray\.__getattr__
120+
numpy(\._core(\.records)?|\.matlib|\.rec)?\.recarray\.__new__
121+
numpy(\._core(\.records)?|\.matlib|\.rec)?\.record\.__name__
122+
numpy(\._core\.records|\.rec)\.array
126123

127124
numpy(\._core\.defchararray|\.char)\.chararray\.__new__
128125
numpy(\._core\.defchararray|\.char)\.chararray\.argsort
@@ -158,19 +155,6 @@ numpy.ctypeslib._ctypeslib
158155

159156
numpy.distutils
160157

161-
numpy.dtypes.BoolDType
162-
numpy.dtypes.U?Int(8|16|32|64)?DType
163-
numpy.dtypes.U?Long(Long)?DType
164-
numpy.dtypes.Float(16|32|64)DType
165-
numpy.dtypes.Complex(64|128)DType
166-
numpy.dtypes.C?LongDoubleDType
167-
numpy.dtypes.ObjectDType
168-
numpy.dtypes.(Bytes|Str|Void)DType
169-
numpy.dtypes.(DateTime|TimeDelta)64DType
170-
numpy.dtypes.StringDType
171-
numpy.dtypes.StringDType.coerce
172-
numpy.dtypes.StringDType.na_object
173-
174158
numpy.f2py.__main__
175159
numpy.f2py.__version__
176160
numpy.f2py.(aux|c)funcs
@@ -185,16 +169,11 @@ numpy.f2py.((cb|common|f90mod|use)_)?rules
185169

186170
numpy.fft.helper
187171

188-
numpy.lib.Arrayterator.__array__
189-
numpy.lib._array_utils_impl.normalize_axis_tuple
190-
numpy.lib._arrayterator_impl.Arrayterator.__array__
172+
numpy.lib(._arrayterator_impl)?.Arrayterator.__array__
173+
numpy.lib.(_array_utils_impl|array_utils).normalize_axis_tuple
174+
numpy.lib.(_npyio_impl|npyio).NpzFile.get
191175
numpy.lib._index_tricks_impl.AxisConcatenator.makemat
192-
numpy.lib._index_tricks_impl.ndenumerate.iter
193-
numpy.lib._index_tricks_impl.ndindex.ndincr
194176
numpy.lib._iotools.NameValidator.defaultdeletechars
195-
numpy.lib.(_npyio_impl|npyio).NpzFile.get
196-
numpy.lib._polynomial_impl.poly1d.integ
197-
numpy.lib.array_utils.normalize_axis_tuple
198177
numpy.lib.format.open_memmap
199178
numpy.lib.format.read_array(_header_(1|2)_0)?
200179
numpy.lib.mixins.NDArrayOperatorsMixin.__array_ufunc__
@@ -234,40 +213,18 @@ numpy.ma.core.mask_rowcols
234213
numpy.ma.extras.MAxisConcatenator.concatenate
235214
numpy.ma.mrecords.fromtextfile
236215

237-
numpy.matlib.average
238-
numpy.matlib.cdouble
239-
numpy.matlib.corrcoef
240-
numpy.matlib.dtype
241-
numpy.matlib.dtype.type
242-
numpy.matlib.einsum_path
243216
numpy.matlib.f2py
244-
numpy.matlib.genfromtxt
217+
numpy.matlib.typing
218+
numpy.matlib.cdouble
245219
numpy.matlib.hanning
246220
numpy.matlib.in1d
247-
numpy.matlib.matrix.__new__
248-
numpy.matlib.(nan)?median
221+
numpy.matlib.nanmedian
249222
numpy.matlib.nanpercentile
250-
numpy.matlib.ndenumerate.iter
251-
numpy.matlib.ndindex.ndincr
252-
numpy.matlib.poly1d.integ
253-
numpy.matlib.recarray.__getattr__
254-
numpy.matlib.recarray.__new__
255-
numpy.matlib.record.__name__
256-
numpy.matlib.save
257223
numpy.matlib.split
258224
numpy.matlib.stack
259-
numpy.matlib.tril
260-
numpy.matlib.triu
261-
numpy.matlib.typing
262-
numpy.matlib.union1d
263-
numpy.matlib.unique_all
264-
numpy.matlib.unique_counts
265-
numpy.matlib.unique_inverse
266-
numpy.matlib.unique_values
225+
numpy.matlib.tri(l|u)
267226

268227
numpy.matrixlib.defmatrix.mat
269-
numpy.matrixlib.defmatrix.matrix.__new__
270-
numpy.matrixlib.matrix.__new__
271228

272229
numpy.polynomial.Chebyshev.basis_name
273230
numpy.polynomial.Hermite.basis_name
@@ -444,15 +401,9 @@ numpy.polynomial.polynomial.polyvander
444401
numpy.polynomial.polynomial.polyvander2d
445402
numpy.polynomial.polynomial.polyvander3d
446403

447-
numpy.random._generator.__test__
448404
numpy\.random(\.bit_generator)?\.BitGenerator\.capsule
449405
numpy\.random\.((_mt19937\.)?MT19937|(_pcg64\.)?PCG64(DXSM)?|(_philox\.)?Philox|(_sfc64\.)?SFC64|(bit_generator\.)?(Seedless)?SeedSequence)\.__(reduce|setstate)_cython__
450406

451-
numpy.rec.array
452-
numpy(.rec)?.recarray.__getattr__
453-
numpy(.rec)?.recarray.__new__
454-
numpy(.rec)?.record.__name__
455-
456407
numpy\.testing(\._private.utils)?\.assert_array_equal
457408
numpy\.testing(\._private.utils)?\.assert_array_almost_equal
458409
numpy\.testing(\._private.utils)?\.assert_array_compare

src/numpy-stubs/__init__.pyi

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,21 @@ test: Final[PytestTester] = ...
991991
###
992992
# Public API
993993

994+
@type_check_only
995+
class _DTypeMeta(type):
996+
@property
997+
def type(cls, /) -> type[generic] | None: ...
998+
@property
999+
def _abstract(cls, /) -> bool: ...
1000+
@property
1001+
def _is_numeric(cls, /) -> bool: ...
1002+
@property
1003+
def _parametric(cls, /) -> bool: ...
1004+
@property
1005+
def _legacy(cls, /) -> bool: ...
1006+
9941007
@final
995-
class dtype(Generic[_ScalarT_co]):
1008+
class dtype(Generic[_ScalarT_co], metaclass=_DTypeMeta):
9961009
names: tuple[str, ...] | None
9971010

9981011
@property
@@ -1515,11 +1528,11 @@ class dtype(Generic[_ScalarT_co]):
15151528
#
15161529
def newbyteorder(self, new_order: _ByteOrder = ..., /) -> Self: ...
15171530

1518-
# avoid shadowing `type` and `str`
1519-
@property
1520-
def type(self) -> type[_ScalarT_co]: ...
1531+
# place these at the bottom to avoid shadowing the `type` and `str` builtins
15211532
@property
15221533
def str(self) -> LiteralString: ...
1534+
@property
1535+
def type(self) -> type[_ScalarT_co]: ...
15231536

15241537
@type_check_only
15251538
class _ArrayOrScalarCommon:

src/numpy-stubs/dtypes.pyi

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
from types import MemberDescriptorType
12
from typing import (
23
Any,
3-
Final,
4+
ClassVar,
45
Generic,
56
Literal as L,
67
NoReturn,
78
TypeAlias,
89
final,
910
type_check_only,
1011
)
11-
from typing_extensions import LiteralString, Self, TypeVar
12+
from typing_extensions import LiteralString, Self, TypeVar, override
1213

1314
import numpy as np
1415

@@ -50,15 +51,15 @@ __all__ = [
5051

5152
# Helper base classes (typing-only)
5253

53-
_SCT_co = TypeVar("_SCT_co", bound=np.generic, covariant=True)
54+
_ScalarT_co = TypeVar("_ScalarT_co", bound=np.generic, covariant=True)
5455

5556
@type_check_only
56-
class _SimpleDType(np.dtype[_SCT_co], Generic[_SCT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
57+
class _SimpleDType(np.dtype[_ScalarT_co], Generic[_ScalarT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
5758
names: None # pyright: ignore[reportIncompatibleVariableOverride]
5859
def __new__(cls, /) -> Self: ...
5960
def __getitem__(self, key: Any, /) -> NoReturn: ...
6061
@property
61-
def base(self) -> np.dtype[_SCT_co]: ...
62+
def base(self) -> np.dtype[_ScalarT_co]: ...
6263
@property
6364
def fields(self) -> None: ...
6465
@property
@@ -73,7 +74,7 @@ class _SimpleDType(np.dtype[_SCT_co], Generic[_SCT_co]): # type: ignore[misc]
7374
def subdtype(self) -> None: ...
7475

7576
@type_check_only
76-
class _LiteralDType(_SimpleDType[_SCT_co], Generic[_SCT_co]): # type: ignore[misc]
77+
class _LiteralDType(_SimpleDType[_ScalarT_co], Generic[_ScalarT_co]): # type: ignore[misc]
7778
@property
7879
def flags(self) -> L[0]: ...
7980
@property
@@ -233,11 +234,11 @@ class UInt64DType( # type: ignore[misc]
233234
@property
234235
def str(self) -> L["<u8", ">u8"]: ...
235236

236-
# Standard C-named version/alias:
237-
ByteDType: Final = Int8DType
238-
UByteDType: Final = UInt8DType
239-
ShortDType: Final = Int16DType
240-
UShortDType: Final = UInt16DType
237+
# NOTE: Don't make these `Final`: it will break stubtest
238+
ByteDType = Int8DType
239+
UByteDType = UInt8DType
240+
ShortDType = Int16DType
241+
UShortDType = UInt16DType
241242

242243
@final
243244
class IntDType( # type: ignore[misc]
@@ -353,13 +354,13 @@ class Float64DType( # type: ignore[misc]
353354
class LongDoubleDType( # type: ignore[misc]
354355
_TypeCodes[L["f"], L["g"], L[13]],
355356
_NativeOrder,
356-
_NBit[L[8, 12, 16], L[8, 12, 16]],
357+
_NBit[L[12, 16], L[12, 16]],
357358
_LiteralDType[np.longdouble],
358359
):
359360
@property
360-
def name(self) -> L["float64", "float96", "float128"]: ...
361+
def name(self) -> L["float96", "float128"]: ...
361362
@property
362-
def str(self) -> L["<f8", ">f8", "<f12", ">f12", "<f16", ">f16"]: ...
363+
def str(self) -> L["<f12", ">f12", "<f16", ">f16"]: ...
363364

364365
# Complex:
365366

@@ -391,13 +392,13 @@ class Complex128DType( # type: ignore[misc]
391392
class CLongDoubleDType( # type: ignore[misc]
392393
_TypeCodes[L["c"], L["G"], L[16]],
393394
_NativeOrder,
394-
_NBit[L[8, 12, 16], L[16, 24, 32]],
395+
_NBit[L[12, 16], L[24, 32]],
395396
_LiteralDType[np.clongdouble],
396397
):
397398
@property
398-
def name(self) -> L["complex128", "complex192", "complex256"]: ...
399+
def name(self) -> L["complex192", "complex256"]: ...
399400
@property
400-
def str(self) -> L["<c16", ">c16", "<c24", ">c24", "<c32", ">c32"]: ...
401+
def str(self) -> L["<c24", ">c24", "<c32", ">c32"]: ...
401402

402403
# Python objects:
403404

@@ -617,27 +618,45 @@ class StringDType( # type: ignore[misc]
617618
# https://github.com/numpy/numpy/pull/28196
618619
np.dtype[str], # type: ignore[type-var] # pyright: ignore[reportGeneralTypeIssues,reportInvalidTypeArguments]
619620
):
621+
@property
622+
def coerce(self) -> L[True]: ...
623+
na_object: ClassVar[MemberDescriptorType] # does not get instantiated
624+
625+
#
626+
@override
620627
def __new__(cls, /) -> StringDType: ...
628+
@override
621629
def __getitem__(self, key: Any, /) -> NoReturn: ...
622630
@property
631+
@override
623632
def base(self) -> StringDType: ...
624633
@property
634+
@override
625635
def fields(self) -> None: ...
626636
@property
637+
@override
627638
def hasobject(self) -> L[True]: ...
628639
@property
640+
@override
629641
def isalignedstruct(self) -> L[False]: ...
630642
@property
643+
@override
631644
def isnative(self) -> L[True]: ...
632645
@property
646+
@override
633647
def name(self) -> L["StringDType64", "StringDType128"]: ...
634648
@property
649+
@override
635650
def ndim(self) -> L[0]: ...
636651
@property
652+
@override
637653
def shape(self) -> tuple[()]: ...
638654
@property
639-
def str(self) -> L["|T8", "|T16"]: ...
640-
@property
655+
@override
641656
def subdtype(self) -> None: ...
642657
@property
643-
def type(self) -> type[str]: ... # type: ignore[valid-type]
658+
@override
659+
def type(self) -> type[str]: ...
660+
@property
661+
@override
662+
def str(self) -> L["|T8", "|T16"]: ...

0 commit comments

Comments
 (0)