Skip to content

Commit 50658ef

Browse files
authored
STY: remove --keep-runtime-typing from pyupgrade #40759 Part-3 (#40803)
1 parent e960fa0 commit 50658ef

19 files changed

+394
-494
lines changed

pandas/core/dtypes/base.py

+13-17
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
from typing import (
88
TYPE_CHECKING,
99
Any,
10-
List,
11-
Optional,
12-
Tuple,
13-
Type,
14-
Union,
1510
)
1611

1712
import numpy as np
1813

19-
from pandas._typing import DtypeObj
14+
from pandas._typing import (
15+
DtypeObj,
16+
type_t,
17+
)
2018
from pandas.errors import AbstractMethodError
2119

2220
from pandas.core.dtypes.generic import (
@@ -101,7 +99,7 @@ def __from_arrow__(
10199
provided for registering virtual subclasses.
102100
"""
103101

104-
_metadata: Tuple[str, ...] = ()
102+
_metadata: tuple[str, ...] = ()
105103

106104
def __str__(self) -> str:
107105
return self.name
@@ -153,7 +151,7 @@ def na_value(self) -> object:
153151
return np.nan
154152

155153
@property
156-
def type(self) -> Type[Any]:
154+
def type(self) -> type[Any]:
157155
"""
158156
The scalar type for the array, e.g. ``int``
159157
@@ -190,7 +188,7 @@ def name(self) -> str:
190188
raise AbstractMethodError(self)
191189

192190
@property
193-
def names(self) -> Optional[List[str]]:
191+
def names(self) -> list[str] | None:
194192
"""
195193
Ordered list of field names, or None if there are no fields.
196194
@@ -200,7 +198,7 @@ def names(self) -> Optional[List[str]]:
200198
return None
201199

202200
@classmethod
203-
def construct_array_type(cls) -> Type[ExtensionArray]:
201+
def construct_array_type(cls) -> type_t[ExtensionArray]:
204202
"""
205203
Return the array type associated with this dtype.
206204
@@ -337,7 +335,7 @@ def _is_boolean(self) -> bool:
337335
"""
338336
return False
339337

340-
def _get_common_dtype(self, dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
338+
def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
341339
"""
342340
Return the common dtype, if one exists.
343341
@@ -366,7 +364,7 @@ def _get_common_dtype(self, dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
366364
return None
367365

368366

369-
def register_extension_dtype(cls: Type[ExtensionDtype]) -> Type[ExtensionDtype]:
367+
def register_extension_dtype(cls: type[ExtensionDtype]) -> type[ExtensionDtype]:
370368
"""
371369
Register an ExtensionType with pandas as class decorator.
372370
@@ -409,9 +407,9 @@ class Registry:
409407
"""
410408

411409
def __init__(self):
412-
self.dtypes: List[Type[ExtensionDtype]] = []
410+
self.dtypes: list[type[ExtensionDtype]] = []
413411

414-
def register(self, dtype: Type[ExtensionDtype]) -> None:
412+
def register(self, dtype: type[ExtensionDtype]) -> None:
415413
"""
416414
Parameters
417415
----------
@@ -422,9 +420,7 @@ def register(self, dtype: Type[ExtensionDtype]) -> None:
422420

423421
self.dtypes.append(dtype)
424422

425-
def find(
426-
self, dtype: Union[Type[ExtensionDtype], str]
427-
) -> Optional[Type[ExtensionDtype]]:
423+
def find(self, dtype: type[ExtensionDtype] | str) -> type[ExtensionDtype] | None:
428424
"""
429425
Parameters
430426
----------

pandas/core/dtypes/cast.py

+17-26
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@
1515
from typing import (
1616
TYPE_CHECKING,
1717
Any,
18-
Dict,
19-
List,
20-
Optional,
2118
Sequence,
22-
Set,
2319
Sized,
24-
Tuple,
25-
Type,
26-
Union,
2720
cast,
2821
overload,
2922
)
@@ -125,7 +118,7 @@
125118

126119

127120
def maybe_convert_platform(
128-
values: Union[list, tuple, range, np.ndarray, ExtensionArray]
121+
values: list | tuple | range | np.ndarray | ExtensionArray,
129122
) -> ArrayLike:
130123
""" try to do platform conversion, allow ndarray or list here """
131124
if isinstance(values, (list, tuple, range)):
@@ -159,7 +152,7 @@ def is_nested_object(obj) -> bool:
159152
)
160153

161154

162-
def maybe_box_datetimelike(value: Scalar, dtype: Optional[Dtype] = None) -> Scalar:
155+
def maybe_box_datetimelike(value: Scalar, dtype: Dtype | None = None) -> Scalar:
163156
"""
164157
Cast scalar to Timestamp or Timedelta if scalar is datetime-like
165158
and dtype is not object.
@@ -245,9 +238,7 @@ def _disallow_mismatched_datetimelike(value, dtype: DtypeObj):
245238
raise TypeError(f"Cannot cast {repr(value)} to {dtype}")
246239

247240

248-
def maybe_downcast_to_dtype(
249-
result: ArrayLike, dtype: Union[str, np.dtype]
250-
) -> ArrayLike:
241+
def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLike:
251242
"""
252243
try to cast to the specified dtype (e.g. convert back to bool/int
253244
or could be an astype of float64->float32
@@ -460,7 +451,7 @@ def maybe_cast_result_dtype(dtype: DtypeObj, how: str) -> DtypeObj:
460451

461452

462453
def maybe_cast_to_extension_array(
463-
cls: Type[ExtensionArray], obj: ArrayLike, dtype: Optional[ExtensionDtype] = None
454+
cls: type[ExtensionArray], obj: ArrayLike, dtype: ExtensionDtype | None = None
464455
) -> ArrayLike:
465456
"""
466457
Call to `_from_sequence` that returns the object unchanged on Exception.
@@ -727,7 +718,7 @@ def _ensure_dtype_type(value, dtype: np.dtype):
727718
return dtype.type(value)
728719

729720

730-
def infer_dtype_from(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
721+
def infer_dtype_from(val, pandas_dtype: bool = False) -> tuple[DtypeObj, Any]:
731722
"""
732723
Interpret the dtype from a scalar or array.
733724
@@ -744,7 +735,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
744735
return infer_dtype_from_array(val, pandas_dtype=pandas_dtype)
745736

746737

747-
def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
738+
def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj, Any]:
748739
"""
749740
Interpret the dtype from a scalar.
750741
@@ -834,7 +825,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
834825
return dtype, val
835826

836827

837-
def dict_compat(d: Dict[Scalar, Scalar]) -> Dict[Scalar, Scalar]:
828+
def dict_compat(d: dict[Scalar, Scalar]) -> dict[Scalar, Scalar]:
838829
"""
839830
Convert datetimelike-keyed dicts to a Timestamp-keyed dict.
840831
@@ -852,7 +843,7 @@ def dict_compat(d: Dict[Scalar, Scalar]) -> Dict[Scalar, Scalar]:
852843

853844
def infer_dtype_from_array(
854845
arr, pandas_dtype: bool = False
855-
) -> Tuple[DtypeObj, ArrayLike]:
846+
) -> tuple[DtypeObj, ArrayLike]:
856847
"""
857848
Infer the dtype from an array.
858849
@@ -944,7 +935,7 @@ def maybe_upcast(
944935
values: np.ndarray,
945936
fill_value: Scalar = np.nan,
946937
copy: bool = False,
947-
) -> Tuple[np.ndarray, Scalar]:
938+
) -> tuple[np.ndarray, Scalar]:
948939
"""
949940
Provide explicit type promotion and coercion.
950941
@@ -970,7 +961,7 @@ def maybe_upcast(
970961
return values, fill_value
971962

972963

973-
def invalidate_string_dtypes(dtype_set: Set[DtypeObj]):
964+
def invalidate_string_dtypes(dtype_set: set[DtypeObj]):
974965
"""
975966
Change string like dtypes to object for
976967
``DataFrame.select_dtypes()``.
@@ -1524,7 +1515,7 @@ def maybe_castable(dtype: np.dtype) -> bool:
15241515
return dtype.name not in POSSIBLY_CAST_DTYPES
15251516

15261517

1527-
def maybe_infer_to_datetimelike(value: Union[np.ndarray, List]):
1518+
def maybe_infer_to_datetimelike(value: np.ndarray | list):
15281519
"""
15291520
we might have a array (or single object) that is datetime like,
15301521
and no dtype is passed don't change the value unless we find a
@@ -1619,8 +1610,8 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
16191610

16201611

16211612
def maybe_cast_to_datetime(
1622-
value: Union[ExtensionArray, np.ndarray, list], dtype: Optional[DtypeObj]
1623-
) -> Union[ExtensionArray, np.ndarray, list]:
1613+
value: ExtensionArray | np.ndarray | list, dtype: DtypeObj | None
1614+
) -> ExtensionArray | np.ndarray | list:
16241615
"""
16251616
try to cast the array/value to a datetimelike dtype, converting float
16261617
nan to iNaT
@@ -1784,7 +1775,7 @@ def ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
17841775
return dtype
17851776

17861777

1787-
def find_common_type(types: List[DtypeObj]) -> DtypeObj:
1778+
def find_common_type(types: list[DtypeObj]) -> DtypeObj:
17881779
"""
17891780
Find a common data type among the given dtypes.
17901781
@@ -1873,7 +1864,7 @@ def construct_2d_arraylike_from_scalar(
18731864

18741865

18751866
def construct_1d_arraylike_from_scalar(
1876-
value: Scalar, length: int, dtype: Optional[DtypeObj]
1867+
value: Scalar, length: int, dtype: DtypeObj | None
18771868
) -> ArrayLike:
18781869
"""
18791870
create a np.ndarray / pandas type of specified shape and dtype
@@ -1947,7 +1938,7 @@ def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
19471938

19481939

19491940
def construct_1d_ndarray_preserving_na(
1950-
values: Sequence, dtype: Optional[DtypeObj] = None, copy: bool = False
1941+
values: Sequence, dtype: DtypeObj | None = None, copy: bool = False
19511942
) -> np.ndarray:
19521943
"""
19531944
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
@@ -1997,7 +1988,7 @@ def construct_1d_ndarray_preserving_na(
19971988

19981989

19991990
def maybe_cast_to_integer_array(
2000-
arr: Union[list, np.ndarray], dtype: np.dtype, copy: bool = False
1991+
arr: list | np.ndarray, dtype: np.dtype, copy: bool = False
20011992
):
20021993
"""
20031994
Takes any dtype and returns the casted version, raising for when data is

0 commit comments

Comments
 (0)