Skip to content

Commit b388aaf

Browse files
committed
🚨 ScalarType stubtest error fix
1 parent f8dae9b commit b388aaf

2 files changed

Lines changed: 37 additions & 107 deletions

File tree

‎src/numpy-stubs/@test/static/accept/numerictypes.pyi‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from typing import Literal, assert_type
1+
from typing import Any, Literal, assert_type
22

33
import numpy as np
44

55
assert_type(np.ScalarType[0], type[int])
66
assert_type(np.ScalarType[3], type[bool])
77
assert_type(np.ScalarType[8], type[np.csingle])
8-
assert_type(np.ScalarType[10], type[np.clongdouble])
8+
assert_type(np.ScalarType[10], type[np.clongdouble | Any])
99
assert_type(np.bool_(object()), np.bool)
1010

1111
assert_type(np.typecodes["Character"], Literal["c"])

‎src/numpy-stubs/_core/numerictypes.pyi‎

Lines changed: 35 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from builtins import bool as py_bool
32
from typing import Any, Final, Literal as L, TypeAlias, TypedDict, type_check_only
43

@@ -155,110 +154,41 @@ class _TypeCodes(TypedDict):
155154
typecodes: Final[_TypeCodes] = ...
156155

157156
# pyright: reportRedeclaration=false
158-
# NOTE: The orders of float64/longdouble, int32/int64, and timedelta64/datetime64 are platform dependent.
159-
# NOTE: We assume 64-bit platforms here.
160-
if sys.platform == "darwin":
161-
_ScalarTypeTuple: TypeAlias = tuple[
162-
type[int],
163-
type[float],
164-
type[complex],
165-
type[py_bool],
166-
type[bytes],
167-
type[str],
168-
type[memoryview[Any]],
169-
type[Any], # TODO: bool_?
170-
type[complex64],
171-
type[complex128],
172-
type[clongdouble],
173-
type[float16],
174-
type[float32],
175-
type[longdouble],
176-
type[float64],
177-
type[int8],
178-
type[int16],
179-
type[int32],
180-
type[int64],
181-
type[Any], # TODO: longlong?
182-
type[datetime64[Any]],
183-
type[timedelta64[Any]],
184-
type[object_],
185-
type[bytes_],
186-
type[str_],
187-
type[uint8],
188-
type[uint16],
189-
type[uint32],
190-
type[uint64],
191-
type[Any], # TODO: ulonglong?
192-
type[void],
193-
]
194-
elif sys.platform == "win32":
195-
_ScalarTypeTuple: TypeAlias = tuple[
196-
type[int],
197-
type[float],
198-
type[complex],
199-
type[py_bool],
200-
type[bytes],
201-
type[str],
202-
type[memoryview[Any]],
203-
type[Any], # TODO: bool_?
204-
type[complex64],
205-
type[complex128],
206-
type[clongdouble],
207-
type[float16],
208-
type[float32],
209-
type[float64],
210-
type[longdouble],
211-
type[int8],
212-
type[int16],
213-
type[int32],
214-
type[Any], # TODO: intc?
215-
type[int64],
216-
type[datetime64[Any]],
217-
type[timedelta64[Any]],
218-
type[object_],
219-
type[bytes_],
220-
type[str_],
221-
type[uint8],
222-
type[uint16],
223-
type[Any], # TODO: uintc?
224-
type[uint32],
225-
type[uint64],
226-
type[void],
227-
]
228-
else: # linux (presumed)
229-
_ScalarTypeTuple: TypeAlias = tuple[
230-
type[int],
231-
type[float],
232-
type[complex],
233-
type[py_bool],
234-
type[bytes],
235-
type[str],
236-
type[memoryview[Any]],
237-
type[bool_[Any]],
238-
type[complex64],
239-
type[complex128],
240-
type[clongdouble],
241-
type[float16],
242-
type[float32],
243-
type[float64],
244-
type[longdouble],
245-
type[int8],
246-
type[int16],
247-
type[int32],
248-
type[int64],
249-
type[longlong],
250-
type[timedelta64[Any]],
251-
type[datetime64[Any]],
252-
type[object_],
253-
type[bytes_],
254-
type[str_],
255-
type[uint8],
256-
type[uint16],
257-
type[uint32],
258-
type[uint64],
259-
type[ulonglong],
260-
type[void],
261-
]
157+
# NOTE: The order of same-kind same-itemsize types are unstable, hence the `| Any` in some places.
158+
# See https://github.com/numpy/numpy/pull/29761 for details
159+
_ScalarTypeTuple: TypeAlias = tuple[
160+
type[int],
161+
type[float],
162+
type[complex],
163+
type[py_bool],
164+
type[bytes],
165+
type[str],
166+
type[memoryview[Any]],
167+
type[bool_[Any]],
168+
type[complex64],
169+
type[complex128 | Any],
170+
type[clongdouble | Any],
171+
type[float16],
172+
type[float32],
173+
type[float64 | Any],
174+
type[longdouble | Any],
175+
type[int8],
176+
type[int16],
177+
type[int32 | Any],
178+
type[long | Any],
179+
type[int64],
180+
type[datetime64[Any] | Any],
181+
type[timedelta64[Any] | Any],
182+
type[object_],
183+
type[bytes_],
184+
type[str_],
185+
type[uint8],
186+
type[uint16],
187+
type[uint32 | Any],
188+
type[ulong | Any],
189+
type[uint64],
190+
type[void],
191+
]
262192

263193
ScalarType: Final[_ScalarTypeTuple] = ...
264194
typeDict: Final = sctypeDict

0 commit comments

Comments
 (0)