Skip to content

STY: remove --keep-runtime-typing from pyupgrade Part-3 #40803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Optional,
Sequence,
Tuple,
Type,
Type as type_t,
TypeVar,
Union,
)
Expand Down Expand Up @@ -119,7 +119,7 @@
# dtypes
NpDtype = Union[str, np.dtype]
Dtype = Union[
"ExtensionDtype", NpDtype, Type[Union[str, float, int, complex, bool, object]]
"ExtensionDtype", NpDtype, type_t[Union[str, float, int, complex, bool, object]]
]
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
DtypeArg = Union[Dtype, Dict[Hashable, Dtype]]
Expand Down
30 changes: 13 additions & 17 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
from typing import (
TYPE_CHECKING,
Any,
List,
Optional,
Tuple,
Type,
Union,
)

import numpy as np

from pandas._typing import DtypeObj
from pandas._typing import (
DtypeObj,
type_t,
)
from pandas.errors import AbstractMethodError

from pandas.core.dtypes.generic import (
Expand Down Expand Up @@ -101,7 +99,7 @@ def __from_arrow__(
provided for registering virtual subclasses.
"""

_metadata: Tuple[str, ...] = ()
_metadata: tuple[str, ...] = ()

def __str__(self) -> str:
return self.name
Expand Down Expand Up @@ -153,7 +151,7 @@ def na_value(self) -> object:
return np.nan

@property
def type(self) -> Type[Any]:
def type(self) -> type[Any]:
"""
The scalar type for the array, e.g. ``int``

Expand Down Expand Up @@ -190,7 +188,7 @@ def name(self) -> str:
raise AbstractMethodError(self)

@property
def names(self) -> Optional[List[str]]:
def names(self) -> list[str] | None:
"""
Ordered list of field names, or None if there are no fields.

Expand All @@ -200,7 +198,7 @@ def names(self) -> Optional[List[str]]:
return None

@classmethod
def construct_array_type(cls) -> Type[ExtensionArray]:
def construct_array_type(cls) -> type_t[ExtensionArray]:
"""
Return the array type associated with this dtype.

Expand Down Expand Up @@ -337,7 +335,7 @@ def _is_boolean(self) -> bool:
"""
return False

def _get_common_dtype(self, dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
"""
Return the common dtype, if one exists.

Expand Down Expand Up @@ -366,7 +364,7 @@ def _get_common_dtype(self, dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
return None


def register_extension_dtype(cls: Type[ExtensionDtype]) -> Type[ExtensionDtype]:
def register_extension_dtype(cls: type[ExtensionDtype]) -> type[ExtensionDtype]:
"""
Register an ExtensionType with pandas as class decorator.

Expand Down Expand Up @@ -409,9 +407,9 @@ class Registry:
"""

def __init__(self):
self.dtypes: List[Type[ExtensionDtype]] = []
self.dtypes: list[type[ExtensionDtype]] = []

def register(self, dtype: Type[ExtensionDtype]) -> None:
def register(self, dtype: type[ExtensionDtype]) -> None:
"""
Parameters
----------
Expand All @@ -422,9 +420,7 @@ def register(self, dtype: Type[ExtensionDtype]) -> None:

self.dtypes.append(dtype)

def find(
self, dtype: Union[Type[ExtensionDtype], str]
) -> Optional[Type[ExtensionDtype]]:
def find(self, dtype: type[ExtensionDtype] | str) -> type[ExtensionDtype] | None:
"""
Parameters
----------
Expand Down
43 changes: 17 additions & 26 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,8 @@
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Optional,
Sequence,
Set,
Sized,
Tuple,
Type,
Union,
cast,
overload,
)
Expand Down Expand Up @@ -125,7 +118,7 @@


def maybe_convert_platform(
values: Union[list, tuple, range, np.ndarray, ExtensionArray]
values: list | tuple | range | np.ndarray | ExtensionArray,
) -> ArrayLike:
""" try to do platform conversion, allow ndarray or list here """
if isinstance(values, (list, tuple, range)):
Expand Down Expand Up @@ -159,7 +152,7 @@ def is_nested_object(obj) -> bool:
)


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


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


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


def infer_dtype_from(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
def infer_dtype_from(val, pandas_dtype: bool = False) -> tuple[DtypeObj, Any]:
"""
Interpret the dtype from a scalar or array.

Expand All @@ -744,7 +735,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
return infer_dtype_from_array(val, pandas_dtype=pandas_dtype)


def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj, Any]:
"""
Interpret the dtype from a scalar.

Expand Down Expand Up @@ -834,7 +825,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
return dtype, val


def dict_compat(d: Dict[Scalar, Scalar]) -> Dict[Scalar, Scalar]:
def dict_compat(d: dict[Scalar, Scalar]) -> dict[Scalar, Scalar]:
"""
Convert datetimelike-keyed dicts to a Timestamp-keyed dict.

Expand All @@ -852,7 +843,7 @@ def dict_compat(d: Dict[Scalar, Scalar]) -> Dict[Scalar, Scalar]:

def infer_dtype_from_array(
arr, pandas_dtype: bool = False
) -> Tuple[DtypeObj, ArrayLike]:
) -> tuple[DtypeObj, ArrayLike]:
"""
Infer the dtype from an array.

Expand Down Expand Up @@ -944,7 +935,7 @@ def maybe_upcast(
values: np.ndarray,
fill_value: Scalar = np.nan,
copy: bool = False,
) -> Tuple[np.ndarray, Scalar]:
) -> tuple[np.ndarray, Scalar]:
"""
Provide explicit type promotion and coercion.

Expand All @@ -970,7 +961,7 @@ def maybe_upcast(
return values, fill_value


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


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


def maybe_cast_to_datetime(
value: Union[ExtensionArray, np.ndarray, list], dtype: Optional[DtypeObj]
) -> Union[ExtensionArray, np.ndarray, list]:
value: ExtensionArray | np.ndarray | list, dtype: DtypeObj | None
) -> ExtensionArray | np.ndarray | list:
"""
try to cast the array/value to a datetimelike dtype, converting float
nan to iNaT
Expand Down Expand Up @@ -1784,7 +1775,7 @@ def ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
return dtype


def find_common_type(types: List[DtypeObj]) -> DtypeObj:
def find_common_type(types: list[DtypeObj]) -> DtypeObj:
"""
Find a common data type among the given dtypes.

Expand Down Expand Up @@ -1873,7 +1864,7 @@ def construct_2d_arraylike_from_scalar(


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


def construct_1d_ndarray_preserving_na(
values: Sequence, dtype: Optional[DtypeObj] = None, copy: bool = False
values: Sequence, dtype: DtypeObj | None = None, copy: bool = False
) -> np.ndarray:
"""
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
Expand Down Expand Up @@ -1997,7 +1988,7 @@ def construct_1d_ndarray_preserving_na(


def maybe_cast_to_integer_array(
arr: Union[list, np.ndarray], dtype: np.dtype, copy: bool = False
arr: list | np.ndarray, dtype: np.dtype, copy: bool = False
):
"""
Takes any dtype and returns the casted version, raising for when data is
Expand Down
Loading