Skip to content

Commit fcab848

Browse files
committed
TYP: Many typing constructs are invariant
1 parent dbc3afa commit fcab848

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

pandas/_typing.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
npt: Any = None
7575

7676

77+
# Functions that take Dict/Mapping/List/Sequence/Callable can be tricky to type:
78+
# - keys of Dict and Mapping do not accept sub-classes
79+
# - items of List and Sequence do not accept sub-classes
80+
# - input argument to Callable cannot be sub-classes
81+
# If you want to allow any type and it's sub-classes in the above cases, you can
82+
# use TypeVar("AllowsSubclasses", bound=class)
83+
HashableT = TypeVar("HashableT", bound=Hashable)
84+
7785
# array-like
7886

7987
ArrayLike = Union["ExtensionArray", np.ndarray]
@@ -105,7 +113,7 @@
105113
NDFrameT = TypeVar("NDFrameT", bound="NDFrame")
106114

107115
Axis = Union[str, int]
108-
IndexLabel = Union[Hashable, Sequence[Hashable]]
116+
IndexLabel = Union[Hashable, Sequence[HashableT]]
109117
Level = Union[Hashable, int]
110118
Shape = Tuple[int, ...]
111119
Suffixes = Tuple[Optional[str], Optional[str]]
@@ -127,19 +135,19 @@
127135
Dtype = Union["ExtensionDtype", NpDtype]
128136
AstypeArg = Union["ExtensionDtype", "npt.DTypeLike"]
129137
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
130-
DtypeArg = Union[Dtype, Dict[Hashable, Dtype]]
138+
DtypeArg = Union[Dtype, Dict[HashableT, Dtype]]
131139
DtypeObj = Union[np.dtype, "ExtensionDtype"]
132140

133141
# converters
134-
ConvertersArg = Dict[Hashable, Callable[[Dtype], Dtype]]
142+
ConvertersArg = Dict[HashableT, Callable[[Dtype], Dtype]]
135143

136144
# parse_dates
137145
ParseDatesArg = Union[
138-
bool, List[Hashable], List[List[Hashable]], Dict[Hashable, List[Hashable]]
146+
bool, List[HashableT], List[List[HashableT]], Dict[HashableT, List[Hashable]]
139147
]
140148

141149
# For functions like rename that convert one label to another
142-
Renamer = Union[Mapping[Hashable, Any], Callable[[Hashable], Hashable]]
150+
Renamer = Union[Mapping[HashableT, Any], Callable[[HashableT], Hashable]]
143151

144152
# to maintain type information across generic functions and parametrization
145153
T = TypeVar("T")
@@ -156,7 +164,7 @@
156164

157165
# types of `func` kwarg for DataFrame.aggregate and Series.aggregate
158166
AggFuncTypeBase = Union[Callable, str]
159-
AggFuncTypeDict = Dict[Hashable, Union[AggFuncTypeBase, List[AggFuncTypeBase]]]
167+
AggFuncTypeDict = Dict[HashableT, Union[AggFuncTypeBase, List[AggFuncTypeBase]]]
160168
AggFuncType = Union[
161169
AggFuncTypeBase,
162170
List[AggFuncTypeBase],
@@ -260,10 +268,10 @@ def closed(self) -> bool:
260268
FormattersType = Union[
261269
List[Callable], Tuple[Callable, ...], Mapping[Union[str, int], Callable]
262270
]
263-
ColspaceType = Mapping[Hashable, Union[str, int]]
271+
ColspaceType = Mapping[HashableT, Union[str, int]]
264272
FloatFormatType = Union[str, Callable, "EngFormatter"]
265273
ColspaceArgType = Union[
266-
str, int, Sequence[Union[str, int]], Mapping[Hashable, Union[str, int]]
274+
str, int, Sequence[Union[str, int]], Mapping[HashableT, Union[str, int]]
267275
]
268276

269277
# Arguments for fillna()

0 commit comments

Comments
 (0)