diff --git a/pandas/core/array_algos/putmask.py b/pandas/core/array_algos/putmask.py index 3daf1b3ae3902..3a67f7d871f86 100644 --- a/pandas/core/array_algos/putmask.py +++ b/pandas/core/array_algos/putmask.py @@ -1,10 +1,9 @@ """ EA-compatible analogue to to np.putmask """ -from typing import ( - Any, - Tuple, -) +from __future__ import annotations + +from typing import Any import warnings import numpy as np @@ -171,7 +170,7 @@ def putmask_without_repeat(values: np.ndarray, mask: np.ndarray, new: Any) -> No np.putmask(values, mask, new) -def validate_putmask(values: ArrayLike, mask: np.ndarray) -> Tuple[np.ndarray, bool]: +def validate_putmask(values: ArrayLike, mask: np.ndarray) -> tuple[np.ndarray, bool]: """ Validate mask and check if this putmask operation is a no-op. """ diff --git a/pandas/core/arrays/_ranges.py b/pandas/core/arrays/_ranges.py index a537951786646..cac9fcd40fa52 100644 --- a/pandas/core/arrays/_ranges.py +++ b/pandas/core/arrays/_ranges.py @@ -2,8 +2,7 @@ Helper functions to generate range-like data for DatetimeArray (and possibly TimedeltaArray/PeriodArray) """ - -from typing import Union +from __future__ import annotations import numpy as np @@ -17,8 +16,8 @@ def generate_regular_range( - start: Union[Timestamp, Timedelta], - end: Union[Timestamp, Timedelta], + start: Timestamp | Timedelta, + end: Timestamp | Timedelta, periods: int, freq: BaseOffset, ): diff --git a/pandas/core/computation/engines.py b/pandas/core/computation/engines.py index 7452cf03d0038..62732402dbeea 100644 --- a/pandas/core/computation/engines.py +++ b/pandas/core/computation/engines.py @@ -1,12 +1,9 @@ """ Engine classes for :func:`~pandas.eval` """ +from __future__ import annotations import abc -from typing import ( - Dict, - Type, -) from pandas.core.computation.align import ( align_terms, @@ -140,7 +137,7 @@ def _evaluate(self) -> None: pass -ENGINES: Dict[str, Type[AbstractEngine]] = { +ENGINES: dict[str, type[AbstractEngine]] = { "numexpr": NumExprEngine, "python": PythonEngine, } diff --git a/pandas/core/computation/parsing.py b/pandas/core/computation/parsing.py index f3321fc55ad80..b0f817d2c1ff3 100644 --- a/pandas/core/computation/parsing.py +++ b/pandas/core/computation/parsing.py @@ -1,6 +1,7 @@ """ :func:`~pandas.eval` source string parsing functions """ +from __future__ import annotations from io import StringIO from keyword import iskeyword @@ -9,7 +10,6 @@ from typing import ( Hashable, Iterator, - Tuple, ) # A token value Python's tokenizer probably will never use. @@ -66,7 +66,7 @@ def create_valid_python_identifier(name: str) -> str: return name -def clean_backtick_quoted_toks(tok: Tuple[int, str]) -> Tuple[int, str]: +def clean_backtick_quoted_toks(tok: tuple[int, str]) -> tuple[int, str]: """ Clean up a column name if surrounded by backticks. @@ -131,7 +131,7 @@ def clean_column_name(name: Hashable) -> Hashable: def tokenize_backtick_quoted_string( token_generator: Iterator[tokenize.TokenInfo], source: str, string_start: int -) -> Tuple[int, str]: +) -> tuple[int, str]: """ Creates a token from a backtick quoted string. @@ -163,7 +163,7 @@ def tokenize_backtick_quoted_string( return BACKTICK_QUOTED_STRING, source[string_start:string_end] -def tokenize_string(source: str) -> Iterator[Tuple[int, str]]: +def tokenize_string(source: str) -> Iterator[tuple[int, str]]: """ Tokenize a Python source code string. diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index f56e13775460b..304c42321e72a 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -1,8 +1,6 @@ +from __future__ import annotations + import textwrap -from typing import ( - List, - Set, -) from pandas._libs import ( NaT, @@ -98,12 +96,12 @@ def get_objs_combined_axis( return _get_combined_index(obs_idxes, intersect=intersect, sort=sort, copy=copy) -def _get_distinct_objs(objs: List[Index]) -> List[Index]: +def _get_distinct_objs(objs: list[Index]) -> list[Index]: """ Return a list with distinct elements of "objs" (different ids). Preserves order. """ - ids: Set[int] = set() + ids: set[int] = set() res = [] for obj in objs: if id(obj) not in ids: @@ -113,7 +111,7 @@ def _get_distinct_objs(objs: List[Index]) -> List[Index]: def _get_combined_index( - indexes: List[Index], + indexes: list[Index], intersect: bool = False, sort: bool = False, copy: bool = False, diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index b1cabf92bf985..6ff20f7d009bc 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -1,12 +1,11 @@ """ Shared methods for Index subclasses backed by ExtensionArray. """ +from __future__ import annotations + from typing import ( Hashable, - List, - Type, TypeVar, - Union, ) import numpy as np @@ -117,7 +116,7 @@ def method(self, *args, **kwargs): return method -def inherit_names(names: List[str], delegate, cache: bool = False, wrap: bool = False): +def inherit_names(names: list[str], delegate, cache: bool = False, wrap: bool = False): """ Class decorator to pin attributes from an ExtensionArray to a Index subclass. @@ -227,20 +226,20 @@ class ExtensionIndex(Index): # The base class already passes through to _data: # size, __len__, dtype - _data: Union[IntervalArray, NDArrayBackedExtensionArray] + _data: IntervalArray | NDArrayBackedExtensionArray - _data_cls: Union[ - Type[Categorical], - Type[DatetimeArray], - Type[TimedeltaArray], - Type[PeriodArray], - Type[IntervalArray], - ] + _data_cls: ( + type[Categorical] + | type[DatetimeArray] + | type[TimedeltaArray] + | type[PeriodArray] + | type[IntervalArray] + ) @classmethod def _simple_new( cls, - array: Union[IntervalArray, NDArrayBackedExtensionArray], + array: IntervalArray | NDArrayBackedExtensionArray, name: Hashable = None, ): """ diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index 8b7070e945439..38984238ecf65 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -1,6 +1,7 @@ +from __future__ import annotations + import subprocess import sys -from typing import List import pytest @@ -46,7 +47,7 @@ class TestPDApi(Base): ] # these are already deprecated; awaiting removal - deprecated_modules: List[str] = ["np", "datetime"] + deprecated_modules: list[str] = ["np", "datetime"] # misc misc = ["IndexSlice", "NaT", "NA"] @@ -98,13 +99,13 @@ class TestPDApi(Base): ] # these are already deprecated; awaiting removal - deprecated_classes: List[str] = [] + deprecated_classes: list[str] = [] # these should be deprecated in the future - deprecated_classes_in_future: List[str] = ["SparseArray"] + deprecated_classes_in_future: list[str] = ["SparseArray"] # external modules exposed in pandas namespace - modules: List[str] = [] + modules: list[str] = [] # top-level functions funcs = [ @@ -181,10 +182,10 @@ class TestPDApi(Base): funcs_to = ["to_datetime", "to_numeric", "to_pickle", "to_timedelta"] # top-level to deprecate in the future - deprecated_funcs_in_future: List[str] = [] + deprecated_funcs_in_future: list[str] = [] # these are already deprecated; awaiting removal - deprecated_funcs: List[str] = [] + deprecated_funcs: list[str] = [] # private modules in pandas namespace private_modules = [ diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 9ca7d0b465250..844bdd4bd1944 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -1,14 +1,13 @@ # Arithmetic tests for DataFrame/Series/Index/Array classes that should # behave identically. # Specifically for numeric dtypes +from __future__ import annotations + from collections import abc from decimal import Decimal from itertools import combinations import operator -from typing import ( - Any, - List, -) +from typing import Any import numpy as np import pytest @@ -56,8 +55,8 @@ def adjust_negative_zero(zero, expected): # TODO: remove this kludge once mypy stops giving false positives here # List comprehension has incompatible type List[PandasObject]; expected List[RangeIndex] # See GH#29725 -ser_or_index: List[Any] = [Series, Index] -lefts: List[Any] = [RangeIndex(10, 40, 10)] +ser_or_index: list[Any] = [Series, Index] +lefts: list[Any] = [RangeIndex(10, 40, 10)] lefts.extend( [ cls([10, 20, 30], dtype=dtype) diff --git a/pandas/tests/arrays/masked/test_arithmetic.py b/pandas/tests/arrays/masked/test_arithmetic.py index 29998831777f8..bea94095452bd 100644 --- a/pandas/tests/arrays/masked/test_arithmetic.py +++ b/pandas/tests/arrays/masked/test_arithmetic.py @@ -1,7 +1,6 @@ -from typing import ( - Any, - List, -) +from __future__ import annotations + +from typing import Any import numpy as np import pytest @@ -12,7 +11,7 @@ # integer dtypes arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_EA_INT_DTYPES] -scalars: List[Any] = [2] * len(arrays) +scalars: list[Any] = [2] * len(arrays) # floating dtypes arrays += [pd.array([0.1, 0.2, 0.3, None], dtype=dtype) for dtype in tm.FLOAT_EA_DTYPES] scalars += [0.2, 0.2] diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index 0467bb1dad676..7cf319e1d134c 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -1,11 +1,8 @@ +from __future__ import annotations + from functools import reduce from itertools import product import operator -from typing import ( - Dict, - List, - Type, -) import warnings import numpy as np @@ -147,8 +144,8 @@ def lhs(request): @td.skip_if_no_ne class TestEvalNumexprPandas: - exclude_cmp: List[str] = [] - exclude_bool: List[str] = [] + exclude_cmp: list[str] = [] + exclude_bool: list[str] = [] engine = "numexpr" parser = "pandas" @@ -1125,7 +1122,7 @@ def test_performance_warning_for_poor_alignment(self, engine, parser): @td.skip_if_no_ne class TestOperationsNumExprPandas: - exclude_arith: List[str] = [] + exclude_arith: list[str] = [] engine = "numexpr" parser = "pandas" @@ -1629,7 +1626,7 @@ def test_simple_in_ops(self): @td.skip_if_no_ne class TestOperationsNumExprPython(TestOperationsNumExprPandas): - exclude_arith: List[str] = ["in", "not in"] + exclude_arith: list[str] = ["in", "not in"] engine = "numexpr" parser = "python" @@ -1723,7 +1720,7 @@ class TestOperationsPythonPython(TestOperationsNumExprPython): class TestOperationsPythonPandas(TestOperationsNumExprPandas): - exclude_arith: List[str] = [] + exclude_arith: list[str] = [] engine = "python" parser = "pandas" @@ -1878,7 +1875,7 @@ def test_invalid_parser(): pd.eval("x + y", local_dict={"x": 1, "y": 2}, parser="asdf") -_parsers: Dict[str, Type[BaseExprVisitor]] = { +_parsers: dict[str, type[BaseExprVisitor]] = { "python": PythonExprVisitor, "pytables": pytables.PyTablesExprVisitor, "pandas": PandasExprVisitor,