Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da54e54

Browse filesBrowse files
authored
Merge pull request #18185 from BvB93/x-like
MAINT: Give the `_<X>Like` and `_ArrayLike<X>` type aliases a more descriptive name
2 parents e51b87a + 2a5647a commit da54e54
Copy full SHA for da54e54

File tree

Expand file treeCollapse file tree

8 files changed

+162
-154
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+162
-154
lines changed

‎numpy/__init__.pyi

Copy file name to clipboardExpand all lines: numpy/__init__.pyi
+81-72Lines changed: 81 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ from contextlib import ContextDecorator
77

88
from numpy.core._internal import _ctypes
99
from numpy.typing import (
10+
# Arrays
1011
ArrayLike,
12+
13+
# DTypes
1114
DTypeLike,
12-
_Shape,
13-
_ShapeLike,
14-
_CharLike,
15-
_BoolLike,
16-
_IntLike,
17-
_FloatLike,
18-
_ComplexLike,
19-
_TD64Like,
20-
_NumberLike,
2115
_SupportsDType,
2216
_VoidDTypeLike,
17+
18+
# Shapes
19+
_Shape,
20+
_ShapeLike,
21+
22+
# Scalars
23+
_CharLike_co,
24+
_BoolLike_co,
25+
_IntLike_co,
26+
_FloatLike_co,
27+
_ComplexLike_co,
28+
_TD64Like_co,
29+
_NumberLike_co,
30+
31+
# `number` precision
2332
NBitBase,
2433
_256Bit,
2534
_128Bit,
@@ -39,8 +48,8 @@ from numpy.typing import (
3948
_NBitSingle,
4049
_NBitDouble,
4150
_NBitLongDouble,
42-
)
43-
from numpy.typing import (
51+
52+
# Character codes
4453
_BoolCodes,
4554
_UInt8Codes,
4655
_UInt16Codes,
@@ -858,11 +867,11 @@ _PartitionKind = Literal["introselect"]
858867
_SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"]
859868
_SortSide = Literal["left", "right"]
860869

861-
_ArrayLikeBool = Union[_BoolLike, Sequence[_BoolLike], ndarray]
870+
_ArrayLikeBool = Union[_BoolLike_co, Sequence[_BoolLike_co], ndarray]
862871
_ArrayLikeIntOrBool = Union[
863-
_IntLike,
872+
_IntLike_co,
864873
ndarray,
865-
Sequence[_IntLike],
874+
Sequence[_IntLike_co],
866875
Sequence[Sequence[Any]], # TODO: wait for support for recursive types
867876
]
868877

@@ -1073,7 +1082,7 @@ class _ArrayOrScalarCommon:
10731082
axis: None = ...,
10741083
out: None = ...,
10751084
keepdims: Literal[False] = ...,
1076-
initial: _NumberLike = ...,
1085+
initial: _NumberLike_co = ...,
10771086
where: _ArrayLikeBool = ...,
10781087
) -> number: ...
10791088
@overload
@@ -1082,7 +1091,7 @@ class _ArrayOrScalarCommon:
10821091
axis: Optional[_ShapeLike] = ...,
10831092
out: None = ...,
10841093
keepdims: bool = ...,
1085-
initial: _NumberLike = ...,
1094+
initial: _NumberLike_co = ...,
10861095
where: _ArrayLikeBool = ...,
10871096
) -> Union[number, ndarray]: ...
10881097
@overload
@@ -1091,7 +1100,7 @@ class _ArrayOrScalarCommon:
10911100
axis: Optional[_ShapeLike] = ...,
10921101
out: _NdArraySubClass = ...,
10931102
keepdims: bool = ...,
1094-
initial: _NumberLike = ...,
1103+
initial: _NumberLike_co = ...,
10951104
where: _ArrayLikeBool = ...,
10961105
) -> _NdArraySubClass: ...
10971106
@overload
@@ -1124,7 +1133,7 @@ class _ArrayOrScalarCommon:
11241133
axis: None = ...,
11251134
out: None = ...,
11261135
keepdims: Literal[False] = ...,
1127-
initial: _NumberLike = ...,
1136+
initial: _NumberLike_co = ...,
11281137
where: _ArrayLikeBool = ...,
11291138
) -> number: ...
11301139
@overload
@@ -1133,7 +1142,7 @@ class _ArrayOrScalarCommon:
11331142
axis: Optional[_ShapeLike] = ...,
11341143
out: None = ...,
11351144
keepdims: bool = ...,
1136-
initial: _NumberLike = ...,
1145+
initial: _NumberLike_co = ...,
11371146
where: _ArrayLikeBool = ...,
11381147
) -> Union[number, ndarray]: ...
11391148
@overload
@@ -1142,7 +1151,7 @@ class _ArrayOrScalarCommon:
11421151
axis: Optional[_ShapeLike] = ...,
11431152
out: _NdArraySubClass = ...,
11441153
keepdims: bool = ...,
1145-
initial: _NumberLike = ...,
1154+
initial: _NumberLike_co = ...,
11461155
where: _ArrayLikeBool = ...,
11471156
) -> _NdArraySubClass: ...
11481157
def newbyteorder(self: _ArraySelf, __new_order: _ByteOrder = ...) -> _ArraySelf: ...
@@ -1153,7 +1162,7 @@ class _ArrayOrScalarCommon:
11531162
dtype: DTypeLike = ...,
11541163
out: None = ...,
11551164
keepdims: Literal[False] = ...,
1156-
initial: _NumberLike = ...,
1165+
initial: _NumberLike_co = ...,
11571166
where: _ArrayLikeBool = ...,
11581167
) -> number: ...
11591168
@overload
@@ -1163,7 +1172,7 @@ class _ArrayOrScalarCommon:
11631172
dtype: DTypeLike = ...,
11641173
out: None = ...,
11651174
keepdims: bool = ...,
1166-
initial: _NumberLike = ...,
1175+
initial: _NumberLike_co = ...,
11671176
where: _ArrayLikeBool = ...,
11681177
) -> Union[number, ndarray]: ...
11691178
@overload
@@ -1173,7 +1182,7 @@ class _ArrayOrScalarCommon:
11731182
dtype: DTypeLike = ...,
11741183
out: _NdArraySubClass = ...,
11751184
keepdims: bool = ...,
1176-
initial: _NumberLike = ...,
1185+
initial: _NumberLike_co = ...,
11771186
where: _ArrayLikeBool = ...,
11781187
) -> _NdArraySubClass: ...
11791188
@overload
@@ -1234,7 +1243,7 @@ class _ArrayOrScalarCommon:
12341243
dtype: DTypeLike = ...,
12351244
out: None = ...,
12361245
keepdims: Literal[False] = ...,
1237-
initial: _NumberLike = ...,
1246+
initial: _NumberLike_co = ...,
12381247
where: _ArrayLikeBool = ...,
12391248
) -> number: ...
12401249
@overload
@@ -1244,7 +1253,7 @@ class _ArrayOrScalarCommon:
12441253
dtype: DTypeLike = ...,
12451254
out: None = ...,
12461255
keepdims: bool = ...,
1247-
initial: _NumberLike = ...,
1256+
initial: _NumberLike_co = ...,
12481257
where: _ArrayLikeBool = ...,
12491258
) -> Union[number, ndarray]: ...
12501259
@overload
@@ -1254,13 +1263,13 @@ class _ArrayOrScalarCommon:
12541263
dtype: DTypeLike = ...,
12551264
out: _NdArraySubClass = ...,
12561265
keepdims: bool = ...,
1257-
initial: _NumberLike = ...,
1266+
initial: _NumberLike_co = ...,
12581267
where: _ArrayLikeBool = ...,
12591268
) -> _NdArraySubClass: ...
12601269
@overload
12611270
def take(
12621271
self,
1263-
indices: _IntLike,
1272+
indices: _IntLike_co,
12641273
axis: Optional[int] = ...,
12651274
out: None = ...,
12661275
mode: _ModeKind = ...,
@@ -1577,10 +1586,10 @@ class number(generic, Generic[_NBit_co]): # type: ignore
15771586
__rpow__: _NumberOp
15781587
__truediv__: _NumberOp
15791588
__rtruediv__: _NumberOp
1580-
__lt__: _ComparisonOp[_NumberLike]
1581-
__le__: _ComparisonOp[_NumberLike]
1582-
__gt__: _ComparisonOp[_NumberLike]
1583-
__ge__: _ComparisonOp[_NumberLike]
1589+
__lt__: _ComparisonOp[_NumberLike_co]
1590+
__le__: _ComparisonOp[_NumberLike_co]
1591+
__gt__: _ComparisonOp[_NumberLike_co]
1592+
__ge__: _ComparisonOp[_NumberLike_co]
15841593

15851594
class bool_(generic):
15861595
def __init__(self, __value: object = ...) -> None: ...
@@ -1619,10 +1628,10 @@ class bool_(generic):
16191628
__rmod__: _BoolMod
16201629
__divmod__: _BoolDivMod
16211630
__rdivmod__: _BoolDivMod
1622-
__lt__: _ComparisonOp[_NumberLike]
1623-
__le__: _ComparisonOp[_NumberLike]
1624-
__gt__: _ComparisonOp[_NumberLike]
1625-
__ge__: _ComparisonOp[_NumberLike]
1631+
__lt__: _ComparisonOp[_NumberLike_co]
1632+
__le__: _ComparisonOp[_NumberLike_co]
1633+
__gt__: _ComparisonOp[_NumberLike_co]
1634+
__ge__: _ComparisonOp[_NumberLike_co]
16261635

16271636
class object_(generic):
16281637
def __init__(self, __value: object = ...) -> None: ...
@@ -1635,21 +1644,21 @@ class datetime64(generic):
16351644
@overload
16361645
def __init__(
16371646
self,
1638-
__value: Union[None, datetime64, _CharLike, dt.datetime] = ...,
1639-
__format: Union[_CharLike, Tuple[_CharLike, _IntLike]] = ...,
1647+
__value: Union[None, datetime64, _CharLike_co, dt.datetime] = ...,
1648+
__format: Union[_CharLike_co, Tuple[_CharLike_co, _IntLike_co]] = ...,
16401649
) -> None: ...
16411650
@overload
16421651
def __init__(
16431652
self,
16441653
__value: int,
1645-
__format: Union[_CharLike, Tuple[_CharLike, _IntLike]]
1654+
__format: Union[_CharLike_co, Tuple[_CharLike_co, _IntLike_co]]
16461655
) -> None: ...
1647-
def __add__(self, other: _TD64Like) -> datetime64: ...
1648-
def __radd__(self, other: _TD64Like) -> datetime64: ...
1656+
def __add__(self, other: _TD64Like_co) -> datetime64: ...
1657+
def __radd__(self, other: _TD64Like_co) -> datetime64: ...
16491658
@overload
16501659
def __sub__(self, other: datetime64) -> timedelta64: ...
16511660
@overload
1652-
def __sub__(self, other: _TD64Like) -> datetime64: ...
1661+
def __sub__(self, other: _TD64Like_co) -> datetime64: ...
16531662
def __rsub__(self, other: datetime64) -> timedelta64: ...
16541663
__lt__: _ComparisonOp[datetime64]
16551664
__le__: _ComparisonOp[datetime64]
@@ -1658,34 +1667,34 @@ class datetime64(generic):
16581667

16591668
# Support for `__index__` was added in python 3.8 (bpo-20092)
16601669
if sys.version_info >= (3, 8):
1661-
_IntValue = Union[SupportsInt, _CharLike, SupportsIndex]
1662-
_FloatValue = Union[None, _CharLike, SupportsFloat, SupportsIndex]
1663-
_ComplexValue = Union[None, _CharLike, SupportsFloat, SupportsComplex, SupportsIndex]
1670+
_IntValue = Union[SupportsInt, _CharLike_co, SupportsIndex]
1671+
_FloatValue = Union[None, _CharLike_co, SupportsFloat, SupportsIndex]
1672+
_ComplexValue = Union[None, _CharLike_co, SupportsFloat, SupportsComplex, SupportsIndex]
16641673
else:
1665-
_IntValue = Union[SupportsInt, _CharLike]
1666-
_FloatValue = Union[None, _CharLike, SupportsFloat]
1667-
_ComplexValue = Union[None, _CharLike, SupportsFloat, SupportsComplex]
1674+
_IntValue = Union[SupportsInt, _CharLike_co]
1675+
_FloatValue = Union[None, _CharLike_co, SupportsFloat]
1676+
_ComplexValue = Union[None, _CharLike_co, SupportsFloat, SupportsComplex]
16681677

16691678
class integer(number[_NBit_co]): # type: ignore
16701679
# NOTE: `__index__` is technically defined in the bottom-most
16711680
# sub-classes (`int64`, `uint32`, etc)
16721681
def __index__(self) -> int: ...
16731682
__truediv__: _IntTrueDiv[_NBit_co]
16741683
__rtruediv__: _IntTrueDiv[_NBit_co]
1675-
def __mod__(self, value: _IntLike) -> integer: ...
1676-
def __rmod__(self, value: _IntLike) -> integer: ...
1684+
def __mod__(self, value: _IntLike_co) -> integer: ...
1685+
def __rmod__(self, value: _IntLike_co) -> integer: ...
16771686
def __invert__(self: _IntType) -> _IntType: ...
16781687
# Ensure that objects annotated as `integer` support bit-wise operations
1679-
def __lshift__(self, other: _IntLike) -> integer: ...
1680-
def __rlshift__(self, other: _IntLike) -> integer: ...
1681-
def __rshift__(self, other: _IntLike) -> integer: ...
1682-
def __rrshift__(self, other: _IntLike) -> integer: ...
1683-
def __and__(self, other: _IntLike) -> integer: ...
1684-
def __rand__(self, other: _IntLike) -> integer: ...
1685-
def __or__(self, other: _IntLike) -> integer: ...
1686-
def __ror__(self, other: _IntLike) -> integer: ...
1687-
def __xor__(self, other: _IntLike) -> integer: ...
1688-
def __rxor__(self, other: _IntLike) -> integer: ...
1688+
def __lshift__(self, other: _IntLike_co) -> integer: ...
1689+
def __rlshift__(self, other: _IntLike_co) -> integer: ...
1690+
def __rshift__(self, other: _IntLike_co) -> integer: ...
1691+
def __rrshift__(self, other: _IntLike_co) -> integer: ...
1692+
def __and__(self, other: _IntLike_co) -> integer: ...
1693+
def __rand__(self, other: _IntLike_co) -> integer: ...
1694+
def __or__(self, other: _IntLike_co) -> integer: ...
1695+
def __ror__(self, other: _IntLike_co) -> integer: ...
1696+
def __xor__(self, other: _IntLike_co) -> integer: ...
1697+
def __rxor__(self, other: _IntLike_co) -> integer: ...
16891698

16901699
class signedinteger(integer[_NBit_co]):
16911700
def __init__(self, __value: _IntValue = ...) -> None: ...
@@ -1730,21 +1739,21 @@ longlong = signedinteger[_NBitLongLong]
17301739
class timedelta64(generic):
17311740
def __init__(
17321741
self,
1733-
__value: Union[None, int, _CharLike, dt.timedelta, timedelta64] = ...,
1734-
__format: Union[_CharLike, Tuple[_CharLike, _IntLike]] = ...,
1742+
__value: Union[None, int, _CharLike_co, dt.timedelta, timedelta64] = ...,
1743+
__format: Union[_CharLike_co, Tuple[_CharLike_co, _IntLike_co]] = ...,
17351744
) -> None: ...
17361745
def __int__(self) -> int: ...
17371746
def __float__(self) -> float: ...
17381747
def __complex__(self) -> complex: ...
17391748
def __neg__(self: _ArraySelf) -> _ArraySelf: ...
17401749
def __pos__(self: _ArraySelf) -> _ArraySelf: ...
17411750
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
1742-
def __add__(self, other: _TD64Like) -> timedelta64: ...
1743-
def __radd__(self, other: _TD64Like) -> timedelta64: ...
1744-
def __sub__(self, other: _TD64Like) -> timedelta64: ...
1745-
def __rsub__(self, other: _TD64Like) -> timedelta64: ...
1746-
def __mul__(self, other: _FloatLike) -> timedelta64: ...
1747-
def __rmul__(self, other: _FloatLike) -> timedelta64: ...
1751+
def __add__(self, other: _TD64Like_co) -> timedelta64: ...
1752+
def __radd__(self, other: _TD64Like_co) -> timedelta64: ...
1753+
def __sub__(self, other: _TD64Like_co) -> timedelta64: ...
1754+
def __rsub__(self, other: _TD64Like_co) -> timedelta64: ...
1755+
def __mul__(self, other: _FloatLike_co) -> timedelta64: ...
1756+
def __rmul__(self, other: _FloatLike_co) -> timedelta64: ...
17481757
__truediv__: _TD64Div[float64]
17491758
__floordiv__: _TD64Div[int64]
17501759
def __rtruediv__(self, other: timedelta64) -> float64: ...
@@ -1753,10 +1762,10 @@ class timedelta64(generic):
17531762
def __rmod__(self, other: timedelta64) -> timedelta64: ...
17541763
def __divmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ...
17551764
def __rdivmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ...
1756-
__lt__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]]
1757-
__le__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]]
1758-
__gt__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]]
1759-
__ge__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]]
1765+
__lt__: _ComparisonOp[Union[timedelta64, _IntLike_co, _BoolLike_co]]
1766+
__le__: _ComparisonOp[Union[timedelta64, _IntLike_co, _BoolLike_co]]
1767+
__gt__: _ComparisonOp[Union[timedelta64, _IntLike_co, _BoolLike_co]]
1768+
__ge__: _ComparisonOp[Union[timedelta64, _IntLike_co, _BoolLike_co]]
17601769

17611770
class unsignedinteger(integer[_NBit_co]):
17621771
# NOTE: `uint64 + signedinteger -> float64`
@@ -1873,7 +1882,7 @@ longcomplex = complexfloating[_NBitLongDouble, _NBitLongDouble]
18731882
class flexible(generic): ... # type: ignore
18741883

18751884
class void(flexible):
1876-
def __init__(self, __value: Union[_IntLike, bytes]): ...
1885+
def __init__(self, __value: Union[_IntLike_co, bytes]): ...
18771886
@property
18781887
def real(self: _ArraySelf) -> _ArraySelf: ...
18791888
@property

‎numpy/core/arrayprint.pyi

Copy file name to clipboardExpand all lines: numpy/core/arrayprint.pyi
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ from numpy import (
2121
longdouble,
2222
clongdouble,
2323
)
24-
from numpy.typing import ArrayLike, _CharLike, _FloatLike
24+
from numpy.typing import ArrayLike, _CharLike_co, _FloatLike_co
2525

2626
if sys.version_info > (3, 8):
2727
from typing import Literal, TypedDict
@@ -40,13 +40,13 @@ class _FormatDict(TypedDict, total=False):
4040
complexfloat: Callable[[complexfloating[Any, Any]], str]
4141
longcomplexfloat: Callable[[clongdouble], str]
4242
void: Callable[[void], str]
43-
numpystr: Callable[[_CharLike], str]
43+
numpystr: Callable[[_CharLike_co], str]
4444
object: Callable[[object], str]
4545
all: Callable[[object], str]
4646
int_kind: Callable[[integer[Any]], str]
4747
float_kind: Callable[[floating[Any]], str]
4848
complex_kind: Callable[[complexfloating[Any, Any]], str]
49-
str_kind: Callable[[_CharLike], str]
49+
str_kind: Callable[[_CharLike_co], str]
5050

5151
class _FormatOptions(TypedDict):
5252
precision: int
@@ -96,7 +96,7 @@ def array2string(
9696
legacy: Optional[Literal[False, "1.13"]] = ...,
9797
) -> str: ...
9898
def format_float_scientific(
99-
x: _FloatLike,
99+
x: _FloatLike_co,
100100
precision: Optional[int] = ...,
101101
unique: bool = ...,
102102
trim: Literal["k", ".", "0", "-"] = ...,
@@ -105,7 +105,7 @@ def format_float_scientific(
105105
exp_digits: Optional[int] = ...,
106106
) -> str: ...
107107
def format_float_positional(
108-
x: _FloatLike,
108+
x: _FloatLike_co,
109109
precision: Optional[int] = ...,
110110
unique: bool = ...,
111111
fractional: bool = ...,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.