Skip to content

Commit 75dedbb

Browse files
authored
Merge pull request numpy#28525 from jorenham/numtype/229
TYP: fix stubtest errors in ``numpy.dtype`` and ``numpy.dtypes.*``
2 parents 4f2e62a + ee26a7a commit 75dedbb

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

numpy/__init__.pyi

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,8 +1194,21 @@ __future_scalars__: Final[set[L["bytes", "str", "object"]]] = ...
11941194
__array_api_version__: Final[L["2023.12"]] = "2023.12"
11951195
test: Final[PytestTester] = ...
11961196

1197+
@type_check_only
1198+
class _DTypeMeta(type):
1199+
@property
1200+
def type(cls, /) -> type[generic] | None: ...
1201+
@property
1202+
def _abstract(cls, /) -> bool: ...
1203+
@property
1204+
def _is_numeric(cls, /) -> bool: ...
1205+
@property
1206+
def _parametric(cls, /) -> bool: ...
1207+
@property
1208+
def _legacy(cls, /) -> bool: ...
1209+
11971210
@final
1198-
class dtype(Generic[_SCT_co]):
1211+
class dtype(Generic[_SCT_co], metaclass=_DTypeMeta):
11991212
names: None | tuple[builtins.str, ...]
12001213
def __hash__(self) -> int: ...
12011214

numpy/dtypes.pyi

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
from typing import (
2-
Any,
3-
Final,
4-
Generic,
5-
Literal as L,
6-
NoReturn,
7-
TypeAlias,
8-
final,
9-
type_check_only,
10-
)
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+
116
from typing_extensions import LiteralString, Self, TypeVar
127

138
import numpy as np
149

15-
__all__ = [
10+
__all__ = [ # noqa: RUF022
1611
'BoolDType',
1712
'Int8DType',
1813
'ByteDType',
@@ -53,7 +48,7 @@ __all__ = [
5348
_SCT_co = TypeVar("_SCT_co", bound=np.generic, covariant=True)
5449

5550
@type_check_only
56-
class _SimpleDType(Generic[_SCT_co], np.dtype[_SCT_co]): # type: ignore[misc]
51+
class _SimpleDType(np.dtype[_SCT_co], Generic[_SCT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
5752
names: None # pyright: ignore[reportIncompatibleVariableOverride]
5853
def __new__(cls, /) -> Self: ...
5954
def __getitem__(self, key: Any, /) -> NoReturn: ...
@@ -73,7 +68,7 @@ class _SimpleDType(Generic[_SCT_co], np.dtype[_SCT_co]): # type: ignore[misc]
7368
def subdtype(self) -> None: ...
7469

7570
@type_check_only
76-
class _LiteralDType(Generic[_SCT_co], _SimpleDType[_SCT_co]): # type: ignore[misc]
71+
class _LiteralDType(_SimpleDType[_SCT_co], Generic[_SCT_co]): # type: ignore[misc]
7772
@property
7873
def flags(self) -> L[0]: ...
7974
@property
@@ -234,10 +229,11 @@ class UInt64DType( # type: ignore[misc]
234229
def str(self) -> L["<u8", ">u8"]: ...
235230

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

242238
@final
243239
class IntDType( # type: ignore[misc]
@@ -419,11 +415,11 @@ class ObjectDType( # type: ignore[misc]
419415

420416
@final
421417
class BytesDType( # type: ignore[misc]
422-
Generic[_ItemSize_co],
423418
_TypeCodes[L["S"], L["S"], L[18]],
424419
_NoOrder,
425420
_NBit[L[1], _ItemSize_co],
426421
_SimpleDType[np.bytes_],
422+
Generic[_ItemSize_co],
427423
):
428424
def __new__(cls, size: _ItemSize_co, /) -> BytesDType[_ItemSize_co]: ...
429425
@property
@@ -435,11 +431,11 @@ class BytesDType( # type: ignore[misc]
435431

436432
@final
437433
class StrDType( # type: ignore[misc]
438-
Generic[_ItemSize_co],
439434
_TypeCodes[L["U"], L["U"], L[19]],
440435
_NativeOrder,
441436
_NBit[L[4], _ItemSize_co],
442437
_SimpleDType[np.str_],
438+
Generic[_ItemSize_co],
443439
):
444440
def __new__(cls, size: _ItemSize_co, /) -> StrDType[_ItemSize_co]: ...
445441
@property
@@ -451,11 +447,11 @@ class StrDType( # type: ignore[misc]
451447

452448
@final
453449
class VoidDType( # type: ignore[misc]
454-
Generic[_ItemSize_co],
455450
_TypeCodes[L["V"], L["V"], L[20]],
456451
_NoOrder,
457452
_NBit[L[1], _ItemSize_co],
458-
np.dtype[np.void],
453+
np.dtype[np.void], # pyright: ignore[reportGeneralTypeIssues]
454+
Generic[_ItemSize_co],
459455
):
460456
# NOTE: `VoidDType(...)` raises a `TypeError` at the moment
461457
def __new__(cls, length: _ItemSize_co, /) -> NoReturn: ...
@@ -578,8 +574,13 @@ class StringDType( # type: ignore[misc]
578574
_NativeOrder,
579575
_NBit[L[8], L[16]],
580576
# TODO: Replace the (invalid) `str` with the scalar type, once implemented
581-
np.dtype[str], # type: ignore[type-var]
577+
np.dtype[str], # type: ignore[type-var] # pyright: ignore[reportGeneralTypeIssues,reportInvalidTypeArguments]
582578
):
579+
@property
580+
def coerce(self) -> L[True]: ...
581+
na_object: ClassVar[MemberDescriptorType] # does not get instantiated
582+
583+
#
583584
def __new__(cls, /) -> StringDType: ...
584585
def __getitem__(self, key: Any, /) -> NoReturn: ...
585586
@property

0 commit comments

Comments
 (0)