Skip to content

Commit 6f5dd58

Browse files
authored
Merge pull request numpy#27510 from jorenham/typing/type_check_only
TYP: Mark stub-only classes as `@type_check_only`
2 parents c195609 + 1b25463 commit 6f5dd58

23 files changed

+140
-54
lines changed

numpy/__init__.pyi

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import datetime as dt
88
import enum
99
from abc import abstractmethod
1010
from types import EllipsisType, TracebackType, MappingProxyType, GenericAlias
11-
from contextlib import contextmanager
1211
from decimal import Decimal
1312
from fractions import Fraction
1413
from uuid import UUID
@@ -25,7 +24,6 @@ from numpy._typing import (
2524
_SupportsArray,
2625
_NestedSequence,
2726
_FiniteNestedSequence,
28-
_SupportsArray,
2927
_ArrayLikeBool_co,
3028
_ArrayLikeUInt_co,
3129
_ArrayLikeInt_co,
@@ -207,6 +205,7 @@ from typing import (
207205
final,
208206
ClassVar,
209207
TypeAlias,
208+
type_check_only,
210209
)
211210

212211
# NOTE: `typing_extensions` is always available in `.pyi` stubs or when
@@ -744,6 +743,7 @@ _AnyStr_contra = TypeVar("_AnyStr_contra", LiteralString, builtins.str, bytes, c
744743

745744
# Protocol for representing file-like-objects accepted
746745
# by `ndarray.tofile` and `fromfile`
746+
@type_check_only
747747
class _IOProtocol(Protocol):
748748
def flush(self) -> object: ...
749749
def fileno(self) -> int: ...
@@ -752,6 +752,7 @@ class _IOProtocol(Protocol):
752752

753753
# NOTE: `seek`, `write` and `flush` are technically only required
754754
# for `readwrite`/`write` modes
755+
@type_check_only
755756
class _MemMapIOProtocol(Protocol):
756757
def flush(self) -> object: ...
757758
def fileno(self) -> SupportsIndex: ...
@@ -761,19 +762,14 @@ class _MemMapIOProtocol(Protocol):
761762
@property
762763
def read(self) -> object: ...
763764

765+
@type_check_only
764766
class _SupportsWrite(Protocol[_AnyStr_contra]):
765767
def write(self, s: _AnyStr_contra, /) -> object: ...
766768

767-
def __dir__() -> Sequence[str]: ...
768-
769769
__version__: LiteralString
770770
__array_api_version__: LiteralString
771771
test: PytestTester
772772

773-
# TODO: Move placeholders to their respective module once
774-
# their annotations are properly implemented
775-
#
776-
# Placeholders for classes
777773

778774
def show_config() -> None: ...
779775

@@ -1390,6 +1386,7 @@ _SortKind: TypeAlias = L[
13901386
]
13911387
_SortSide: TypeAlias = L["left", "right"]
13921388

1389+
@type_check_only
13931390
class _ArrayOrScalarCommon:
13941391
@property
13951392
def T(self) -> Self: ...
@@ -1812,13 +1809,16 @@ else:
18121809

18131810
_ArrayAPIVersion: TypeAlias = L["2021.12", "2022.12", "2023.12"]
18141811

1812+
@type_check_only
18151813
class _SupportsItem(Protocol[_T_co]):
18161814
def item(self, args: Any, /) -> _T_co: ...
18171815

1816+
@type_check_only
18181817
class _SupportsReal(Protocol[_T_co]):
18191818
@property
18201819
def real(self) -> _T_co: ...
18211820

1821+
@type_check_only
18221822
class _SupportsImag(Protocol[_T_co]):
18231823
@property
18241824
def imag(self) -> _T_co: ...
@@ -3376,11 +3376,12 @@ class bool(generic):
33763376
bool_: TypeAlias = bool
33773377

33783378
_StringType = TypeVar("_StringType", bound=str | bytes)
3379-
_ShapeType = TypeVar("_ShapeType", bound=Any)
3379+
_ShapeType = TypeVar("_ShapeType", bound=_Shape)
33803380
_ObjectType = TypeVar("_ObjectType", bound=object)
33813381

33823382
# A sequence-like interface like `collections.abc.Sequence`, but without the
33833383
# irrelevant methods.
3384+
@type_check_only
33843385
class _SimpleSequence(Protocol):
33853386
def __len__(self, /) -> int: ...
33863387
def __getitem__(self, index: int, /) -> Any: ...
@@ -3421,6 +3422,7 @@ class object_(generic):
34213422

34223423
# The `datetime64` constructors requires an object with the three attributes below,
34233424
# and thus supports datetime duck typing
3425+
@type_check_only
34243426
class _DatetimeScalar(Protocol):
34253427
@property
34263428
def day(self) -> int: ...
@@ -4727,6 +4729,7 @@ class matrix(ndarray[_Shape2DType_co, _DType_co]):
47274729
def getH(self) -> matrix[_Shape2D, _DType_co]: ...
47284730

47294731

4732+
@type_check_only
47304733
class _SupportsDLPack(Protocol[_T_contra]):
47314734
def __dlpack__(self, *, stream: None | _T_contra = ...) -> _PyCapsule: ...
47324735

numpy/_array_api_info.pyi

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from typing import (
66
TypeVar,
77
final,
88
overload,
9+
type_check_only,
910
)
1011
from typing_extensions import Never
1112

@@ -63,38 +64,44 @@ _Permute3: TypeAlias = (
6364
| tuple[_T3, _T1, _T2] | tuple[_T3, _T2, _T1]
6465
)
6566

67+
@type_check_only
6668
class _DTypesBool(TypedDict):
6769
bool: np.dtype[np.bool]
6870

71+
@type_check_only
6972
class _DTypesInt(TypedDict):
7073
int8: np.dtype[np.int8]
7174
int16: np.dtype[np.int16]
7275
int32: np.dtype[np.int32]
7376
int64: np.dtype[np.int64]
7477

78+
@type_check_only
7579
class _DTypesUInt(TypedDict):
7680
uint8: np.dtype[np.uint8]
7781
uint16: np.dtype[np.uint16]
7882
uint32: np.dtype[np.uint32]
7983
uint64: np.dtype[np.uint64]
8084

81-
class _DTypesInteger(_DTypesInt, _DTypesUInt):
82-
...
85+
@type_check_only
86+
class _DTypesInteger(_DTypesInt, _DTypesUInt): ...
8387

88+
@type_check_only
8489
class _DTypesFloat(TypedDict):
8590
float32: np.dtype[np.float32]
8691
float64: np.dtype[np.float64]
8792

93+
@type_check_only
8894
class _DTypesComplex(TypedDict):
8995
complex64: np.dtype[np.complex64]
9096
complex128: np.dtype[np.complex128]
9197

92-
class _DTypesNumber(_DTypesInteger, _DTypesFloat, _DTypesComplex):
93-
...
98+
@type_check_only
99+
class _DTypesNumber(_DTypesInteger, _DTypesFloat, _DTypesComplex): ...
94100

95-
class _DTypes(_DTypesBool, _DTypesNumber):
96-
...
101+
@type_check_only
102+
class _DTypes(_DTypesBool, _DTypesNumber): ...
97103

104+
@type_check_only
98105
class _DTypesUnion(TypedDict, total=False):
99106
bool: np.dtype[np.bool]
100107
int8: np.dtype[np.int8]
@@ -112,7 +119,6 @@ class _DTypesUnion(TypedDict, total=False):
112119

113120
_EmptyDict: TypeAlias = dict[Never, Never]
114121

115-
116122
@final
117123
class __array_namespace_info__:
118124
__module__: ClassVar[Literal['numpy']]

numpy/_core/_type_aliases.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Collection
2-
from typing import Any, Final, Literal as L, TypeAlias, TypedDict
2+
from typing import Any, Final, Literal as L, TypeAlias, TypedDict, type_check_only
33

44
import numpy as np
55

@@ -16,6 +16,7 @@ __all__ = (
1616
sctypeDict: Final[dict[str, type[np.generic]]]
1717
allTypes: Final[dict[str, type[np.generic]]]
1818

19+
@type_check_only
1920
class _CNamesDict(TypedDict):
2021
BOOL: np.dtype[np.bool]
2122
HALF: np.dtype[np.half]
@@ -58,7 +59,7 @@ _AbstractTypeName: TypeAlias = L[
5859
]
5960
_abstract_type_names: Final[set[_AbstractTypeName]]
6061

61-
62+
@type_check_only
6263
class _AliasesType(TypedDict):
6364
double: L["float64"]
6465
cdouble: L["complex128"]
@@ -71,6 +72,7 @@ class _AliasesType(TypedDict):
7172

7273
_aliases: Final[_AliasesType]
7374

75+
@type_check_only
7476
class _ExtraAliasesType(TypedDict):
7577
float: L["float64"]
7678
complex: L["complex128"]
@@ -83,6 +85,7 @@ class _ExtraAliasesType(TypedDict):
8385

8486
_extra_aliases: Final[_ExtraAliasesType]
8587

88+
@type_check_only
8689
class _SCTypes(TypedDict):
8790
int: Collection[type[np.signedinteger[Any]]]
8891
uint: Collection[type[np.unsignedinteger[Any]]]

numpy/_core/_ufunc_config.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
from collections.abc import Callable
2-
from typing import Any, Literal, TypeAlias, TypedDict
2+
from typing import Any, Literal, TypeAlias, TypedDict, type_check_only
33

44
from numpy import _SupportsWrite
55

66
_ErrKind: TypeAlias = Literal["ignore", "warn", "raise", "call", "print", "log"]
77
_ErrFunc: TypeAlias = Callable[[str, int], Any]
88

9+
@type_check_only
910
class _ErrDict(TypedDict):
1011
divide: _ErrKind
1112
over: _ErrKind
1213
under: _ErrKind
1314
invalid: _ErrKind
1415

16+
@type_check_only
1517
class _ErrDictOptional(TypedDict, total=False):
1618
all: None | _ErrKind
1719
divide: None | _ErrKind

numpy/_core/arrayprint.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable
2-
from typing import Any, Literal, TypeAlias, TypedDict, SupportsIndex
2+
from typing import Any, Literal, TypeAlias, TypedDict, SupportsIndex, type_check_only
33

44
# Using a private class is by no means ideal, but it is simply a consequence
55
# of a `contextlib.context` returning an instance of aforementioned class
@@ -20,6 +20,7 @@ from numpy._typing import NDArray, _CharLike_co, _FloatLike_co
2020

2121
_FloatMode: TypeAlias = Literal["fixed", "unique", "maxprec", "maxprec_equal"]
2222

23+
@type_check_only
2324
class _FormatDict(TypedDict, total=False):
2425
bool: Callable[[np.bool], str]
2526
int: Callable[[integer[Any]], str]
@@ -38,6 +39,7 @@ class _FormatDict(TypedDict, total=False):
3839
complex_kind: Callable[[complexfloating[Any, Any]], str]
3940
str_kind: Callable[[_CharLike_co], str]
4041

42+
@type_check_only
4143
class _FormatOptions(TypedDict):
4244
precision: int
4345
threshold: int

numpy/_core/multiarray.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ _RollKind: TypeAlias = L[ # `raise` is deliberately excluded
136136
"modifiedpreceding",
137137
]
138138

139+
@type_check_only
139140
class _SupportsLenAndGetItem(Protocol[_T_contra, _T_co]):
140141
def __len__(self) -> int: ...
141142
def __getitem__(self, key: _T_contra, /) -> _T_co: ...
142143

144+
@type_check_only
143145
class _SupportsArray(Protocol[_ArrayType_co]):
144146
def __array__(self, /) -> _ArrayType_co: ...
145147

numpy/_core/numerictypes.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from typing import (
33
Any,
44
TypeVar,
55
TypedDict,
6+
type_check_only,
67
)
78

89
import numpy as np
@@ -43,6 +44,7 @@ from numpy._typing import DTypeLike
4344
_T = TypeVar("_T")
4445
_SCT = TypeVar("_SCT", bound=generic)
4546

47+
@type_check_only
4648
class _TypeCodes(TypedDict):
4749
Character: L['c']
4850
Integer: L['bhilqnp']

numpy/_core/records.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ from typing import (
88
overload,
99
Protocol,
1010
SupportsIndex,
11-
Literal
11+
Literal,
12+
type_check_only
1213
)
1314

1415
from numpy import (
@@ -38,6 +39,7 @@ _SCT = TypeVar("_SCT", bound=generic)
3839

3940
_RecArray: TypeAlias = recarray[Any, dtype[_SCT]]
4041

42+
@type_check_only
4143
class _SupportsReadInto(Protocol):
4244
def seek(self, offset: int, whence: int, /) -> object: ...
4345
def tell(self, /) -> int: ...

0 commit comments

Comments
 (0)