Skip to content

Commit db0aa16

Browse files
committed
STY: remove --keep-runtime-typing from pyupgrade #40759 Part-1
1 parent 16eaa36 commit db0aa16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1627
-1321
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ repos:
5454
rev: v2.11.0
5555
hooks:
5656
- id: pyupgrade
57-
args: [--py37-plus]
57+
args: [--py37-plus, --keep-runtime-typing]
5858
- repo: https://github.com/pre-commit/pygrep-hooks
5959
rev: v1.8.0
6060
hooks:

pandas/core/arrays/categorical.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from shutil import get_terminal_size
77
from typing import (
88
TYPE_CHECKING,
9+
Dict,
910
Hashable,
11+
List,
12+
Optional,
1013
Sequence,
1114
Tuple,
1215
Type,
@@ -356,7 +359,7 @@ def __init__(
356359
values,
357360
categories=None,
358361
ordered=None,
359-
dtype: Dtype | None = None,
362+
dtype: Optional[Dtype] = None,
360363
fastpath=False,
361364
copy: bool = True,
362365
):
@@ -470,11 +473,11 @@ def dtype(self) -> CategoricalDtype:
470473
return self._dtype
471474

472475
@property
473-
def _constructor(self) -> type[Categorical]:
476+
def _constructor(self) -> Type[Categorical]:
474477
return Categorical
475478

476479
@classmethod
477-
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
480+
def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False):
478481
return Categorical(scalars, dtype=dtype, copy=copy)
479482

480483
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
@@ -544,7 +547,7 @@ def itemsize(self) -> int:
544547
"""
545548
return self.categories.itemsize
546549

547-
def tolist(self) -> list[Scalar]:
550+
def tolist(self) -> List[Scalar]:
548551
"""
549552
Return a list of the values.
550553
@@ -627,7 +630,7 @@ def _from_inferred_categories(
627630

628631
@classmethod
629632
def from_codes(
630-
cls, codes, categories=None, ordered=None, dtype: Dtype | None = None
633+
cls, codes, categories=None, ordered=None, dtype: Optional[Dtype] = None
631634
):
632635
"""
633636
Make a Categorical type from codes and categories or dtype.
@@ -1366,7 +1369,7 @@ def _validate_fill_value(self, fill_value):
13661369

13671370
# -------------------------------------------------------------
13681371

1369-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
1372+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
13701373
"""
13711374
The numpy array interface.
13721375
@@ -1957,7 +1960,7 @@ def _validate_setitem_value(self, value):
19571960
codes = self.categories.get_indexer(rvalue)
19581961
return codes.astype(self._ndarray.dtype, copy=False)
19591962

1960-
def _reverse_indexer(self) -> dict[Hashable, np.ndarray]:
1963+
def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
19611964
"""
19621965
Compute the inverse of a categorical, returning
19631966
a dict of categories -> indexers.
@@ -2184,7 +2187,7 @@ def equals(self, other: object) -> bool:
21842187

21852188
@classmethod
21862189
def _concat_same_type(
2187-
cls: type[CategoricalT], to_concat: Sequence[CategoricalT], axis: int = 0
2190+
cls: Type[CategoricalT], to_concat: Sequence[CategoricalT], axis: int = 0
21882191
) -> CategoricalT:
21892192
from pandas.core.dtypes.concat import union_categoricals
21902193

pandas/core/arrays/datetimelike.py

+32-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
TYPE_CHECKING,
1010
Any,
1111
Callable,
12+
Optional,
1213
Sequence,
14+
Tuple,
15+
Type,
1316
TypeVar,
1417
Union,
1518
cast,
@@ -153,25 +156,25 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
153156
"""
154157

155158
# _infer_matches -> which infer_dtype strings are close enough to our own
156-
_infer_matches: tuple[str, ...]
159+
_infer_matches: Tuple[str, ...]
157160
_is_recognized_dtype: Callable[[DtypeObj], bool]
158-
_recognized_scalars: tuple[type, ...]
161+
_recognized_scalars: Tuple[Type, ...]
159162
_ndarray: np.ndarray
160163

161-
def __init__(self, data, dtype: Dtype | None = None, freq=None, copy=False):
164+
def __init__(self, data, dtype: Optional[Dtype] = None, freq=None, copy=False):
162165
raise AbstractMethodError(self)
163166

164167
@classmethod
165168
def _simple_new(
166-
cls: type[DatetimeLikeArrayT],
169+
cls: Type[DatetimeLikeArrayT],
167170
values: np.ndarray,
168-
freq: BaseOffset | None = None,
169-
dtype: Dtype | None = None,
171+
freq: Optional[BaseOffset] = None,
172+
dtype: Optional[Dtype] = None,
170173
) -> DatetimeLikeArrayT:
171174
raise AbstractMethodError(cls)
172175

173176
@property
174-
def _scalar_type(self) -> type[DatetimeLikeScalar]:
177+
def _scalar_type(self) -> Type[DatetimeLikeScalar]:
175178
"""
176179
The scalar associated with this datelike
177180
@@ -203,7 +206,7 @@ def _scalar_from_string(self, value: str) -> DTScalarOrNaT:
203206

204207
def _unbox_scalar(
205208
self, value: DTScalarOrNaT, setitem: bool = False
206-
) -> np.int64 | np.datetime64 | np.timedelta64:
209+
) -> Union[np.int64, np.datetime64, np.timedelta64]:
207210
"""
208211
Unbox the integer value of a scalar `value`.
209212
@@ -331,15 +334,15 @@ def _formatter(self, boxed: bool = False):
331334
# ----------------------------------------------------------------
332335
# Array-Like / EA-Interface Methods
333336

334-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
337+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
335338
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
336339
if is_object_dtype(dtype):
337340
return np.array(list(self), dtype=object)
338341
return self._ndarray
339342

340343
def __getitem__(
341-
self, key: int | slice | np.ndarray
342-
) -> DatetimeLikeArrayMixin | DTScalarOrNaT:
344+
self, key: Union[int, slice, np.ndarray]
345+
) -> Union[DatetimeLikeArrayMixin, DTScalarOrNaT]:
343346
"""
344347
This getitem defers to the underlying array, which by-definition can
345348
only handle list-likes, slices, and integer scalars
@@ -351,7 +354,7 @@ def __getitem__(
351354
result._freq = self._get_getitem_freq(key)
352355
return result
353356

354-
def _get_getitem_freq(self, key) -> BaseOffset | None:
357+
def _get_getitem_freq(self, key) -> Optional[BaseOffset]:
355358
"""
356359
Find the `freq` attribute to assign to the result of a __getitem__ lookup.
357360
"""
@@ -383,8 +386,8 @@ def _get_getitem_freq(self, key) -> BaseOffset | None:
383386
# ndarray]"
384387
def __setitem__( # type: ignore[override]
385388
self,
386-
key: int | Sequence[int] | Sequence[bool] | slice,
387-
value: NaTType | Any | Sequence[Any],
389+
key: Union[int, Sequence[int], Sequence[bool], slice],
390+
value: Union[NaTType, Any, Sequence[Any]],
388391
) -> None:
389392
# I'm fudging the types a bit here. "Any" above really depends
390393
# on type(self). For PeriodArray, it's Period (or stuff coercible
@@ -466,10 +469,10 @@ def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray:
466469
...
467470

468471
@overload
469-
def view(self, dtype: Dtype | None = ...) -> ArrayLike:
472+
def view(self, dtype: Optional[Dtype] = ...) -> ArrayLike:
470473
...
471474

472-
def view(self, dtype: Dtype | None = None) -> ArrayLike:
475+
def view(self, dtype: Optional[Dtype] = None) -> ArrayLike:
473476
# We handle datetime64, datetime64tz, timedelta64, and period
474477
# dtypes here. Everything else we pass through to the underlying
475478
# ndarray.
@@ -506,7 +509,7 @@ def view(self, dtype: Dtype | None = None) -> ArrayLike:
506509

507510
@classmethod
508511
def _concat_same_type(
509-
cls: type[DatetimeLikeArrayT],
512+
cls: Type[DatetimeLikeArrayT],
510513
to_concat: Sequence[DatetimeLikeArrayT],
511514
axis: int = 0,
512515
) -> DatetimeLikeArrayT:
@@ -542,7 +545,7 @@ def _values_for_factorize(self):
542545

543546
@classmethod
544547
def _from_factorized(
545-
cls: type[DatetimeLikeArrayT], values, original: DatetimeLikeArrayT
548+
cls: Type[DatetimeLikeArrayT], values, original: DatetimeLikeArrayT
546549
) -> DatetimeLikeArrayT:
547550
return cls(values, dtype=original.dtype)
548551

@@ -786,7 +789,7 @@ def _validate_setitem_value(self, value):
786789

787790
def _unbox(
788791
self, other, setitem: bool = False
789-
) -> np.int64 | np.datetime64 | np.timedelta64 | np.ndarray:
792+
) -> Union[np.int64, np.datetime64, np.timedelta64, np.ndarray]:
790793
"""
791794
Unbox either a scalar with _unbox_scalar or an instance of our own type.
792795
"""
@@ -936,7 +939,7 @@ def freq(self, value):
936939
self._freq = value
937940

938941
@property
939-
def freqstr(self) -> str | None:
942+
def freqstr(self) -> Optional[str]:
940943
"""
941944
Return the frequency object as a string if its set, otherwise None.
942945
"""
@@ -945,7 +948,7 @@ def freqstr(self) -> str | None:
945948
return self.freq.freqstr
946949

947950
@property # NB: override with cache_readonly in immutable subclasses
948-
def inferred_freq(self) -> str | None:
951+
def inferred_freq(self) -> Optional[str]:
949952
"""
950953
Tries to return a string representing a frequency guess,
951954
generated by infer_freq. Returns None if it can't autodetect the
@@ -959,7 +962,7 @@ def inferred_freq(self) -> str | None:
959962
return None
960963

961964
@property # NB: override with cache_readonly in immutable subclasses
962-
def _resolution_obj(self) -> Resolution | None:
965+
def _resolution_obj(self) -> Optional[Resolution]:
963966
freqstr = self.freqstr
964967
if freqstr is None:
965968
return None
@@ -1017,7 +1020,7 @@ def _validate_frequency(cls, index, freq, **kwargs):
10171020

10181021
@classmethod
10191022
def _generate_range(
1020-
cls: type[DatetimeLikeArrayT], start, end, periods, freq, *args, **kwargs
1023+
cls: Type[DatetimeLikeArrayT], start, end, periods, freq, *args, **kwargs
10211024
) -> DatetimeLikeArrayT:
10221025
raise AbstractMethodError(cls)
10231026

@@ -1440,7 +1443,7 @@ def __isub__(self, other):
14401443
# --------------------------------------------------------------
14411444
# Reductions
14421445

1443-
def min(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
1446+
def min(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
14441447
"""
14451448
Return the minimum value of the Array or minimum along
14461449
an axis.
@@ -1469,7 +1472,7 @@ def min(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
14691472
result = nanops.nanmin(self._ndarray, axis=axis, skipna=skipna)
14701473
return self._wrap_reduction_result(axis, result)
14711474

1472-
def max(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
1475+
def max(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
14731476
"""
14741477
Return the maximum value of the Array or maximum along
14751478
an axis.
@@ -1500,7 +1503,7 @@ def max(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
15001503
result = nanops.nanmax(self._ndarray, axis=axis, skipna=skipna)
15011504
return self._wrap_reduction_result(axis, result)
15021505

1503-
def mean(self, *, skipna: bool = True, axis: int | None = 0):
1506+
def mean(self, *, skipna: bool = True, axis: Optional[int] = 0):
15041507
"""
15051508
Return the mean value of the Array.
15061509
@@ -1539,7 +1542,7 @@ def mean(self, *, skipna: bool = True, axis: int | None = 0):
15391542
)
15401543
return self._wrap_reduction_result(axis, result)
15411544

1542-
def median(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
1545+
def median(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
15431546
nv.validate_median((), kwargs)
15441547

15451548
if axis is not None and abs(axis) >= self.ndim:
@@ -1749,11 +1752,11 @@ def ceil(self, freq, ambiguous="raise", nonexistent="raise"):
17491752
# --------------------------------------------------------------
17501753
# Reductions
17511754

1752-
def any(self, *, axis: int | None = None, skipna: bool = True):
1755+
def any(self, *, axis: Optional[int] = None, skipna: bool = True):
17531756
# GH#34479 discussion of desired behavior long-term
17541757
return nanops.nanany(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())
17551758

1756-
def all(self, *, axis: int | None = None, skipna: bool = True):
1759+
def all(self, *, axis: Optional[int] = None, skipna: bool = True):
17571760
# GH#34479 discussion of desired behavior long-term
17581761
return nanops.nanall(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())
17591762

0 commit comments

Comments
 (0)