Skip to content

Commit 20103e7

Browse files
authored
Merge pull request numpy#25039 from mtsokol/adjust-typing-for-long
MAINT: Adjust typing for readded `np.long`
2 parents 95ca101 + 96e5d1f commit 20103e7

File tree

11 files changed

+276
-268
lines changed

11 files changed

+276
-268
lines changed

numpy/__init__.pyi

+2-4
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,12 @@ from numpy._typing import (
9797
_ShortCodes,
9898
_IntCCodes,
9999
_IntPCodes,
100-
_IntCodes,
101100
_LongCodes,
102101
_LongLongCodes,
103102
_UByteCodes,
104103
_UShortCodes,
105104
_UIntCCodes,
106105
_UIntPCodes,
107-
_UIntCodes,
108106
_ULongCodes,
109107
_ULongLongCodes,
110108
_HalfCodes,
@@ -692,7 +690,7 @@ class dtype(Generic[_DTypeScalar_co]):
692690
@overload
693691
def __new__(cls, dtype: _UIntPCodes | type[ct.c_void_p] | type[ct.c_size_t], align: bool = ..., copy: bool = ..., metadata: dict[builtins.str, Any] = ...) -> dtype[uintp]: ...
694692
@overload
695-
def __new__(cls, dtype: _UIntCodes | type[ct.c_ulong], align: bool = ..., copy: bool = ..., metadata: dict[builtins.str, Any] = ...) -> dtype[uint]: ...
693+
def __new__(cls, dtype: _ULongCodes | type[ct.c_ulong], align: bool = ..., copy: bool = ..., metadata: dict[builtins.str, Any] = ...) -> dtype[ulong]: ...
696694
@overload
697695
def __new__(cls, dtype: _ULongLongCodes | type[ct.c_ulonglong], align: bool = ..., copy: bool = ..., metadata: dict[builtins.str, Any] = ...) -> dtype[ulonglong]: ...
698696

@@ -714,7 +712,7 @@ class dtype(Generic[_DTypeScalar_co]):
714712
@overload
715713
def __new__(cls, dtype: _IntPCodes | type[ct.c_ssize_t], align: bool = ..., copy: bool = ..., metadata: dict[builtins.str, Any] = ...) -> dtype[intp]: ...
716714
@overload
717-
def __new__(cls, dtype: _IntCodes | type[ct.c_long], align: bool = ..., copy: bool = ..., metadata: dict[builtins.str, Any] = ...) -> dtype[int_]: ...
715+
def __new__(cls, dtype: _LongCodes | type[ct.c_long], align: bool = ..., copy: bool = ..., metadata: dict[builtins.str, Any] = ...) -> dtype[long]: ...
718716
@overload
719717
def __new__(cls, dtype: _LongLongCodes | type[ct.c_longlong], align: bool = ..., copy: bool = ..., metadata: dict[builtins.str, Any] = ...) -> dtype[longlong]: ...
720718

numpy/_core/numerictypes.pyi

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ from numpy import (
1717
ubyte,
1818
ushort,
1919
uintc,
20-
uint,
20+
ulong,
2121
ulonglong,
2222
byte,
2323
short,
2424
intc,
25-
int_,
25+
long,
2626
longlong,
2727
half,
2828
single,
@@ -133,7 +133,7 @@ ScalarType: tuple[
133133
type[byte],
134134
type[short],
135135
type[intc],
136-
type[int_],
136+
type[long],
137137
type[longlong],
138138
type[timedelta64],
139139
type[datetime64],
@@ -143,7 +143,7 @@ ScalarType: tuple[
143143
type[ubyte],
144144
type[ushort],
145145
type[uintc],
146-
type[uint],
146+
type[ulong],
147147
type[ulonglong],
148148
type[void],
149149
]

numpy/_typing/_dtype_like.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@
3131
_ByteCodes,
3232
_ShortCodes,
3333
_IntCCodes,
34+
_LongCodes,
35+
_LongLongCodes,
3436
_IntPCodes,
3537
_IntCodes,
36-
_LongLongCodes,
3738
_UByteCodes,
3839
_UShortCodes,
3940
_UIntCCodes,
41+
_ULongCodes,
42+
_ULongLongCodes,
4043
_UIntPCodes,
4144
_UIntCodes,
42-
_ULongLongCodes,
4345
_HalfCodes,
4446
_SingleCodes,
4547
_DoubleCodes,
@@ -153,9 +155,10 @@ def dtype(self) -> _DType_co: ...
153155
_UByteCodes,
154156
_UShortCodes,
155157
_UIntCCodes,
158+
_LongCodes,
159+
_ULongLongCodes,
156160
_UIntPCodes,
157161
_UIntCodes,
158-
_ULongLongCodes,
159162
]
160163
_DTypeLikeInt = Union[
161164
type[int],
@@ -169,9 +172,10 @@ def dtype(self) -> _DType_co: ...
169172
_ByteCodes,
170173
_ShortCodes,
171174
_IntCCodes,
175+
_LongCodes,
176+
_LongLongCodes,
172177
_IntPCodes,
173178
_IntCodes,
174-
_LongLongCodes,
175179
]
176180
_DTypeLikeFloat = Union[
177181
type[float],

numpy/ctypeslib.pyi

+10-10
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ from numpy import (
2222
byte,
2323
short,
2424
intc,
25-
int_,
25+
long,
2626
longlong,
2727
ubyte,
2828
ushort,
2929
uintc,
30-
uint,
30+
ulong,
3131
ulonglong,
3232
single,
3333
double,
@@ -52,12 +52,12 @@ from numpy._typing import (
5252
_UByteCodes,
5353
_UShortCodes,
5454
_UIntCCodes,
55-
_UIntCodes,
55+
_ULongCodes,
5656
_ULongLongCodes,
5757
_ByteCodes,
5858
_ShortCodes,
5959
_IntCCodes,
60-
_IntCodes,
60+
_LongCodes,
6161
_LongLongCodes,
6262
_SingleCodes,
6363
_DoubleCodes,
@@ -156,7 +156,7 @@ def as_ctypes_type(dtype: _ShortCodes | _DTypeLike[short] | type[ctypes.c_short]
156156
@overload
157157
def as_ctypes_type(dtype: _IntCCodes | _DTypeLike[intc] | type[ctypes.c_int]) -> type[ctypes.c_int]: ...
158158
@overload
159-
def as_ctypes_type(dtype: _IntCodes | _DTypeLike[int_] | type[int | ctypes.c_long]) -> type[ctypes.c_long]: ...
159+
def as_ctypes_type(dtype: _LongCodes | _DTypeLike[long] | type[int | ctypes.c_long]) -> type[ctypes.c_long]: ...
160160
@overload
161161
def as_ctypes_type(dtype: _LongLongCodes | _DTypeLike[longlong] | type[ctypes.c_longlong]) -> type[ctypes.c_longlong]: ...
162162
@overload
@@ -166,7 +166,7 @@ def as_ctypes_type(dtype: _UShortCodes | _DTypeLike[ushort] | type[ctypes.c_usho
166166
@overload
167167
def as_ctypes_type(dtype: _UIntCCodes | _DTypeLike[uintc] | type[ctypes.c_uint]) -> type[ctypes.c_uint]: ...
168168
@overload
169-
def as_ctypes_type(dtype: _UIntCodes | _DTypeLike[uint] | type[ctypes.c_ulong]) -> type[ctypes.c_ulong]: ...
169+
def as_ctypes_type(dtype: _ULongCodes | _DTypeLike[ulong] | type[ctypes.c_ulong]) -> type[ctypes.c_ulong]: ...
170170
@overload
171171
def as_ctypes_type(dtype: _ULongLongCodes | _DTypeLike[ulonglong] | type[ctypes.c_ulonglong]) -> type[ctypes.c_ulonglong]: ...
172172
@overload
@@ -196,7 +196,7 @@ def as_ctypes(obj: short) -> ctypes.c_short: ...
196196
@overload
197197
def as_ctypes(obj: intc) -> ctypes.c_int: ...
198198
@overload
199-
def as_ctypes(obj: int_) -> ctypes.c_long: ...
199+
def as_ctypes(obj: long) -> ctypes.c_long: ...
200200
@overload
201201
def as_ctypes(obj: longlong) -> ctypes.c_longlong: ...
202202
@overload
@@ -206,7 +206,7 @@ def as_ctypes(obj: ushort) -> ctypes.c_ushort: ...
206206
@overload
207207
def as_ctypes(obj: uintc) -> ctypes.c_uint: ...
208208
@overload
209-
def as_ctypes(obj: uint) -> ctypes.c_ulong: ...
209+
def as_ctypes(obj: ulong) -> ctypes.c_ulong: ...
210210
@overload
211211
def as_ctypes(obj: ulonglong) -> ctypes.c_ulonglong: ...
212212
@overload
@@ -226,7 +226,7 @@ def as_ctypes(obj: NDArray[short]) -> ctypes.Array[ctypes.c_short]: ...
226226
@overload
227227
def as_ctypes(obj: NDArray[intc]) -> ctypes.Array[ctypes.c_int]: ...
228228
@overload
229-
def as_ctypes(obj: NDArray[int_]) -> ctypes.Array[ctypes.c_long]: ...
229+
def as_ctypes(obj: NDArray[long]) -> ctypes.Array[ctypes.c_long]: ...
230230
@overload
231231
def as_ctypes(obj: NDArray[longlong]) -> ctypes.Array[ctypes.c_longlong]: ...
232232
@overload
@@ -236,7 +236,7 @@ def as_ctypes(obj: NDArray[ushort]) -> ctypes.Array[ctypes.c_ushort]: ...
236236
@overload
237237
def as_ctypes(obj: NDArray[uintc]) -> ctypes.Array[ctypes.c_uint]: ...
238238
@overload
239-
def as_ctypes(obj: NDArray[uint]) -> ctypes.Array[ctypes.c_ulong]: ...
239+
def as_ctypes(obj: NDArray[ulong]) -> ctypes.Array[ctypes.c_ulong]: ...
240240
@overload
241241
def as_ctypes(obj: NDArray[ulonglong]) -> ctypes.Array[ctypes.c_ulonglong]: ...
242242
@overload

numpy/random/_generator.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class Generator:
217217
low: int,
218218
high: None | int = ...,
219219
size: None = ...,
220-
dtype: _DTypeLikeBool = ...,
220+
dtype: type[bool] = ...,
221221
endpoint: bool = ...,
222222
) -> bool: ...
223223
@overload
@@ -226,7 +226,7 @@ class Generator:
226226
low: int,
227227
high: None | int = ...,
228228
size: None = ...,
229-
dtype: _DTypeLikeInt | _DTypeLikeUInt = ...,
229+
dtype: type[int] = ...,
230230
endpoint: bool = ...,
231231
) -> int: ...
232232
@overload

numpy/random/_generator.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ cdef class Generator:
645645
raise TypeError('Unsupported dtype %r for integers' % _dtype)
646646

647647

648-
if size is None and dtype in (bool, int):
648+
if size is None and (dtype is bool or dtype is int):
649649
if np.array(ret).shape == ():
650650
return dtype(ret)
651651
return ret

numpy/random/mtrand.pyi

+22-21
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ from numpy import (
1111
int16,
1212
int32,
1313
int64,
14-
int_,
15-
uint,
14+
long,
15+
ulong,
1616
uint8,
1717
uint16,
1818
uint32,
@@ -107,7 +107,8 @@ class RandomState:
107107
@overload
108108
def tomaxint(self, size: None = ...) -> int: ... # type: ignore[misc]
109109
@overload
110-
def tomaxint(self, size: _ShapeLike) -> NDArray[int_]: ...
110+
# Generates long values, but stores it in a 64bit int:
111+
def tomaxint(self, size: _ShapeLike) -> NDArray[int64]: ...
111112
@overload
112113
def randint( # type: ignore[misc]
113114
self,
@@ -120,23 +121,23 @@ class RandomState:
120121
low: int,
121122
high: None | int = ...,
122123
size: None = ...,
123-
dtype: _DTypeLikeBool = ...,
124+
dtype: type[bool] = ...,
124125
) -> bool: ...
125126
@overload
126127
def randint( # type: ignore[misc]
127128
self,
128129
low: int,
129130
high: None | int = ...,
130131
size: None = ...,
131-
dtype: _DTypeLikeInt | _DTypeLikeUInt = ...,
132+
dtype: type[int] = ...,
132133
) -> int: ...
133134
@overload
134135
def randint( # type: ignore[misc]
135136
self,
136137
low: _ArrayLikeInt_co,
137138
high: None | _ArrayLikeInt_co = ...,
138139
size: None | _ShapeLike = ...,
139-
) -> NDArray[int_]: ...
140+
) -> NDArray[long]: ...
140141
@overload
141142
def randint( # type: ignore[misc]
142143
self,
@@ -215,16 +216,16 @@ class RandomState:
215216
low: _ArrayLikeInt_co,
216217
high: None | _ArrayLikeInt_co = ...,
217218
size: None | _ShapeLike = ...,
218-
dtype: dtype[int_] | type[int] | type[int_] | _IntCodes | _SupportsDType[dtype[int_]] = ...,
219-
) -> NDArray[int_]: ...
219+
dtype: dtype[long] | type[int] | type[long] | _LongCodes | _SupportsDType[dtype[long]] = ...,
220+
) -> NDArray[long]: ...
220221
@overload
221222
def randint( # type: ignore[misc]
222223
self,
223224
low: _ArrayLikeInt_co,
224225
high: None | _ArrayLikeInt_co = ...,
225226
size: None | _ShapeLike = ...,
226-
dtype: dtype[uint] | type[uint] | _UIntCodes | _SupportsDType[dtype[uint]] = ...,
227-
) -> NDArray[uint]: ...
227+
dtype: dtype[ulong] | type[ulong] | _ULongCodes | _SupportsDType[dtype[ulong]] = ...,
228+
) -> NDArray[ulong]: ...
228229
def bytes(self, length: int) -> builtins.bytes: ...
229230
@overload
230231
def choice(
@@ -241,7 +242,7 @@ class RandomState:
241242
size: _ShapeLike = ...,
242243
replace: bool = ...,
243244
p: None | _ArrayLikeFloat_co = ...,
244-
) -> NDArray[int_]: ...
245+
) -> NDArray[long]: ...
245246
@overload
246247
def choice(
247248
self,
@@ -283,7 +284,7 @@ class RandomState:
283284
low: _ArrayLikeInt_co,
284285
high: None | _ArrayLikeInt_co = ...,
285286
size: None | _ShapeLike = ...,
286-
) -> NDArray[int_]: ...
287+
) -> NDArray[long]: ...
287288
@overload
288289
def standard_normal(self, size: None = ...) -> float: ... # type: ignore[misc]
289290
@overload
@@ -449,31 +450,31 @@ class RandomState:
449450
@overload
450451
def binomial(
451452
self, n: _ArrayLikeInt_co, p: _ArrayLikeFloat_co, size: None | _ShapeLike = ...
452-
) -> NDArray[int_]: ...
453+
) -> NDArray[long]: ...
453454
@overload
454455
def negative_binomial(self, n: float, p: float, size: None = ...) -> int: ... # type: ignore[misc]
455456
@overload
456457
def negative_binomial(
457458
self, n: _ArrayLikeFloat_co, p: _ArrayLikeFloat_co, size: None | _ShapeLike = ...
458-
) -> NDArray[int_]: ...
459+
) -> NDArray[long]: ...
459460
@overload
460461
def poisson(self, lam: float = ..., size: None = ...) -> int: ... # type: ignore[misc]
461462
@overload
462463
def poisson(
463464
self, lam: _ArrayLikeFloat_co = ..., size: None | _ShapeLike = ...
464-
) -> NDArray[int_]: ...
465+
) -> NDArray[long]: ...
465466
@overload
466467
def zipf(self, a: float, size: None = ...) -> int: ... # type: ignore[misc]
467468
@overload
468469
def zipf(
469470
self, a: _ArrayLikeFloat_co, size: None | _ShapeLike = ...
470-
) -> NDArray[int_]: ...
471+
) -> NDArray[long]: ...
471472
@overload
472473
def geometric(self, p: float, size: None = ...) -> int: ... # type: ignore[misc]
473474
@overload
474475
def geometric(
475476
self, p: _ArrayLikeFloat_co, size: None | _ShapeLike = ...
476-
) -> NDArray[int_]: ...
477+
) -> NDArray[long]: ...
477478
@overload
478479
def hypergeometric(self, ngood: int, nbad: int, nsample: int, size: None = ...) -> int: ... # type: ignore[misc]
479480
@overload
@@ -483,13 +484,13 @@ class RandomState:
483484
nbad: _ArrayLikeInt_co,
484485
nsample: _ArrayLikeInt_co,
485486
size: None | _ShapeLike = ...,
486-
) -> NDArray[int_]: ...
487+
) -> NDArray[long]: ...
487488
@overload
488489
def logseries(self, p: float, size: None = ...) -> int: ... # type: ignore[misc]
489490
@overload
490491
def logseries(
491492
self, p: _ArrayLikeFloat_co, size: None | _ShapeLike = ...
492-
) -> NDArray[int_]: ...
493+
) -> NDArray[long]: ...
493494
def multivariate_normal(
494495
self,
495496
mean: _ArrayLikeFloat_co,
@@ -500,13 +501,13 @@ class RandomState:
500501
) -> NDArray[float64]: ...
501502
def multinomial(
502503
self, n: _ArrayLikeInt_co, pvals: _ArrayLikeFloat_co, size: None | _ShapeLike = ...
503-
) -> NDArray[int_]: ...
504+
) -> NDArray[long]: ...
504505
def dirichlet(
505506
self, alpha: _ArrayLikeFloat_co, size: None | _ShapeLike = ...
506507
) -> NDArray[float64]: ...
507508
def shuffle(self, x: ArrayLike) -> None: ...
508509
@overload
509-
def permutation(self, x: int) -> NDArray[int_]: ...
510+
def permutation(self, x: int) -> NDArray[long]: ...
510511
@overload
511512
def permutation(self, x: ArrayLike) -> NDArray[Any]: ...
512513

numpy/random/mtrand.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ cdef class RandomState:
796796
else:
797797
raise TypeError('Unsupported dtype %r for randint' % _dtype)
798798

799-
if size is None and dtype in (bool, int):
799+
if size is None and (dtype is bool or dtype is int):
800800
if np.array(ret).shape == ():
801801
return dtype(ret)
802802
return ret

0 commit comments

Comments
 (0)