Skip to content

Commit a3d9049

Browse files
authored
CLN: Use TypeAlias in code where types are declared (#61504)
1 parent 0ab10aa commit a3d9049

File tree

15 files changed

+52
-55
lines changed

15 files changed

+52
-55
lines changed

pandas/core/apply.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
TYPE_CHECKING,
1111
Any,
1212
Literal,
13+
TypeAlias,
1314
cast,
1415
)
1516

@@ -71,7 +72,7 @@
7172
from pandas.core.resample import Resampler
7273
from pandas.core.window.rolling import BaseWindow
7374

74-
ResType = dict[int, Any]
75+
ResType: TypeAlias = dict[int, Any]
7576

7677

7778
class BaseExecutionEngine(abc.ABC):

pandas/core/arrays/datetimelike.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
TYPE_CHECKING,
1111
Any,
1212
Literal,
13+
TypeAlias,
1314
Union,
1415
cast,
1516
final,
@@ -161,7 +162,7 @@
161162
TimedeltaArray,
162163
)
163164

164-
DTScalarOrNaT = Union[DatetimeLikeScalar, NaTType]
165+
DTScalarOrNaT: TypeAlias = DatetimeLikeScalar | NaTType
165166

166167

167168
def _make_unpacked_invalid_op(op_name: str):
@@ -386,7 +387,7 @@ def __getitem__(self, key: PositionalIndexer2D) -> Self | DTScalarOrNaT:
386387
# Use cast as we know we will get back a DatetimeLikeArray or DTScalar,
387388
# but skip evaluating the Union at runtime for performance
388389
# (see https://github.com/pandas-dev/pandas/pull/44624)
389-
result = cast("Union[Self, DTScalarOrNaT]", super().__getitem__(key))
390+
result = cast(Union[Self, DTScalarOrNaT], super().__getitem__(key))
390391
if lib.is_scalar(result):
391392
return result
392393
else:

pandas/core/arrays/interval.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import (
1010
TYPE_CHECKING,
1111
Literal,
12-
Union,
12+
TypeAlias,
1313
overload,
1414
)
1515

@@ -109,8 +109,8 @@
109109
)
110110

111111

112-
IntervalSide = Union[TimeArrayLike, np.ndarray]
113-
IntervalOrNA = Union[Interval, float]
112+
IntervalSide: TypeAlias = TimeArrayLike | np.ndarray
113+
IntervalOrNA: TypeAlias = Interval | float
114114

115115
_interval_shared_docs: dict[str, str] = {}
116116

pandas/core/arrays/string_arrow.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import re
55
from typing import (
66
TYPE_CHECKING,
7-
Union,
87
)
98
import warnings
109

@@ -63,9 +62,6 @@
6362
from pandas import Series
6463

6564

66-
ArrowStringScalarOrNAT = Union[str, libmissing.NAType]
67-
68-
6965
def _chk_pyarrow_available() -> None:
7066
if pa_version_under10p1:
7167
msg = "pyarrow>=10.0.1 is required for PyArrow backed ArrowExtensionArray."

pandas/core/groupby/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
Any,
1818
Literal,
1919
NamedTuple,
20+
TypeAlias,
2021
TypeVar,
21-
Union,
2222
cast,
2323
)
2424
import warnings
@@ -102,7 +102,7 @@
102102
from pandas.core.generic import NDFrame
103103

104104
# TODO(typing) the return value on this callable should be any *scalar*.
105-
AggScalar = Union[str, Callable[..., Any]]
105+
AggScalar: TypeAlias = str | Callable[..., Any]
106106
# TODO: validate types on ScalarResult and move to _typing
107107
# Blocked from using by https://github.com/python/mypy/issues/1484
108108
# See note at _mangle_lambda_list

pandas/core/groupby/groupby.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class providing the base-class of operations.
2626
from typing import (
2727
TYPE_CHECKING,
2828
Literal,
29+
TypeAlias,
2930
TypeVar,
3031
Union,
3132
cast,
@@ -449,13 +450,13 @@ def f(self):
449450
return attr
450451

451452

452-
_KeysArgType = Union[
453-
Hashable,
454-
list[Hashable],
455-
Callable[[Hashable], Hashable],
456-
list[Callable[[Hashable], Hashable]],
457-
Mapping[Hashable, Hashable],
458-
]
453+
_KeysArgType: TypeAlias = (
454+
Hashable
455+
| list[Hashable]
456+
| Callable[[Hashable], Hashable]
457+
| list[Callable[[Hashable], Hashable]]
458+
| Mapping[Hashable, Hashable]
459+
)
459460

460461

461462
class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):
@@ -957,9 +958,8 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
957958
level = self.level
958959
result = self._grouper.get_iterator(self._selected_obj)
959960
# mypy: Argument 1 to "len" has incompatible type "Hashable"; expected "Sized"
960-
if (
961-
(is_list_like(level) and len(level) == 1) # type: ignore[arg-type]
962-
or (isinstance(keys, list) and len(keys) == 1)
961+
if (is_list_like(level) and len(level) == 1) or ( # type: ignore[arg-type]
962+
isinstance(keys, list) and len(keys) == 1
963963
):
964964
# GH#42795 - when keys is a list, return tuples even when length is 1
965965
result = (((key,), group) for key, group in result)

pandas/core/indexing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import (
66
TYPE_CHECKING,
77
Any,
8-
TypeVar,
98
cast,
109
final,
1110
)
@@ -83,6 +82,7 @@
8382
Axis,
8483
AxisInt,
8584
Self,
85+
T,
8686
npt,
8787
)
8888

@@ -91,7 +91,6 @@
9191
Series,
9292
)
9393

94-
T = TypeVar("T")
9594
# "null slice"
9695
_NS = slice(None, None)
9796
_one_ellipsis_message = "indexer may only contain one '...' entry"

pandas/core/tools/datetimes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from itertools import islice
77
from typing import (
88
TYPE_CHECKING,
9+
TypeAlias,
910
TypedDict,
1011
Union,
1112
cast,
@@ -93,13 +94,12 @@
9394
# ---------------------------------------------------------------------
9495
# types used in annotations
9596

96-
ArrayConvertible = Union[list, tuple, AnyArrayLike]
97-
Scalar = Union[float, str]
98-
DatetimeScalar = Union[Scalar, date, np.datetime64]
97+
ArrayConvertible: TypeAlias = list | tuple | AnyArrayLike
98+
Scalar: TypeAlias = float | str
99+
DatetimeScalar: TypeAlias = Scalar | date | np.datetime64
99100

100-
DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, ArrayConvertible]
101-
102-
DatetimeDictArg = Union[list[Scalar], tuple[Scalar, ...], AnyArrayLike]
101+
DatetimeScalarOrArrayConvertible: TypeAlias = DatetimeScalar | ArrayConvertible
102+
DatetimeDictArg: TypeAlias = list[Scalar] | tuple[Scalar, ...] | AnyArrayLike
103103

104104

105105
class YearMonthDayDict(TypedDict, total=True):

pandas/io/excel/_calamine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import (
1010
TYPE_CHECKING,
1111
Any,
12-
Union,
12+
TypeAlias,
1313
)
1414

1515
from pandas.compat._optional import import_optional_dependency
@@ -34,7 +34,7 @@
3434
StorageOptions,
3535
)
3636

37-
_CellValue = Union[int, float, str, bool, time, date, datetime, timedelta]
37+
_CellValue: TypeAlias = int | float | str | bool | time | date | datetime | timedelta
3838

3939

4040
class CalamineReader(BaseExcelReader["CalamineWorkbook"]):

pandas/io/formats/printing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from typing import (
1515
TYPE_CHECKING,
1616
Any,
17+
TypeAlias,
1718
TypeVar,
18-
Union,
1919
)
2020
from unicodedata import east_asian_width
2121

@@ -27,7 +27,7 @@
2727

2828
if TYPE_CHECKING:
2929
from pandas._typing import ListLike
30-
EscapeChars = Union[Mapping[str, str], Iterable[str]]
30+
EscapeChars: TypeAlias = Mapping[str, str] | Iterable[str]
3131
_KT = TypeVar("_KT")
3232
_VT = TypeVar("_VT")
3333

0 commit comments

Comments
 (0)