Skip to content

Commit 52f04db

Browse files
TYP: use from __future__ import annotations more - batch 5 (#41898)
1 parent 6d322e2 commit 52f04db

File tree

10 files changed

+55
-67
lines changed

10 files changed

+55
-67
lines changed

pandas/core/array_algos/putmask.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""
22
EA-compatible analogue to to np.putmask
33
"""
4-
from typing import (
5-
Any,
6-
Tuple,
7-
)
4+
from __future__ import annotations
5+
6+
from typing import Any
87
import warnings
98

109
import numpy as np
@@ -171,7 +170,7 @@ def putmask_without_repeat(values: np.ndarray, mask: np.ndarray, new: Any) -> No
171170
np.putmask(values, mask, new)
172171

173172

174-
def validate_putmask(values: ArrayLike, mask: np.ndarray) -> Tuple[np.ndarray, bool]:
173+
def validate_putmask(values: ArrayLike, mask: np.ndarray) -> tuple[np.ndarray, bool]:
175174
"""
176175
Validate mask and check if this putmask operation is a no-op.
177176
"""

pandas/core/arrays/_ranges.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
Helper functions to generate range-like data for DatetimeArray
33
(and possibly TimedeltaArray/PeriodArray)
44
"""
5-
6-
from typing import Union
5+
from __future__ import annotations
76

87
import numpy as np
98

@@ -17,8 +16,8 @@
1716

1817

1918
def generate_regular_range(
20-
start: Union[Timestamp, Timedelta],
21-
end: Union[Timestamp, Timedelta],
19+
start: Timestamp | Timedelta,
20+
end: Timestamp | Timedelta,
2221
periods: int,
2322
freq: BaseOffset,
2423
):

pandas/core/computation/engines.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
"""
22
Engine classes for :func:`~pandas.eval`
33
"""
4+
from __future__ import annotations
45

56
import abc
6-
from typing import (
7-
Dict,
8-
Type,
9-
)
107

118
from pandas.core.computation.align import (
129
align_terms,
@@ -140,7 +137,7 @@ def _evaluate(self) -> None:
140137
pass
141138

142139

143-
ENGINES: Dict[str, Type[AbstractEngine]] = {
140+
ENGINES: dict[str, type[AbstractEngine]] = {
144141
"numexpr": NumExprEngine,
145142
"python": PythonEngine,
146143
}

pandas/core/computation/parsing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
:func:`~pandas.eval` source string parsing functions
33
"""
4+
from __future__ import annotations
45

56
from io import StringIO
67
from keyword import iskeyword
@@ -9,7 +10,6 @@
910
from typing import (
1011
Hashable,
1112
Iterator,
12-
Tuple,
1313
)
1414

1515
# A token value Python's tokenizer probably will never use.
@@ -66,7 +66,7 @@ def create_valid_python_identifier(name: str) -> str:
6666
return name
6767

6868

69-
def clean_backtick_quoted_toks(tok: Tuple[int, str]) -> Tuple[int, str]:
69+
def clean_backtick_quoted_toks(tok: tuple[int, str]) -> tuple[int, str]:
7070
"""
7171
Clean up a column name if surrounded by backticks.
7272
@@ -131,7 +131,7 @@ def clean_column_name(name: Hashable) -> Hashable:
131131

132132
def tokenize_backtick_quoted_string(
133133
token_generator: Iterator[tokenize.TokenInfo], source: str, string_start: int
134-
) -> Tuple[int, str]:
134+
) -> tuple[int, str]:
135135
"""
136136
Creates a token from a backtick quoted string.
137137
@@ -163,7 +163,7 @@ def tokenize_backtick_quoted_string(
163163
return BACKTICK_QUOTED_STRING, source[string_start:string_end]
164164

165165

166-
def tokenize_string(source: str) -> Iterator[Tuple[int, str]]:
166+
def tokenize_string(source: str) -> Iterator[tuple[int, str]]:
167167
"""
168168
Tokenize a Python source code string.
169169

pandas/core/indexes/api.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1+
from __future__ import annotations
2+
13
import textwrap
2-
from typing import (
3-
List,
4-
Set,
5-
)
64

75
from pandas._libs import (
86
NaT,
@@ -98,12 +96,12 @@ def get_objs_combined_axis(
9896
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort, copy=copy)
9997

10098

101-
def _get_distinct_objs(objs: List[Index]) -> List[Index]:
99+
def _get_distinct_objs(objs: list[Index]) -> list[Index]:
102100
"""
103101
Return a list with distinct elements of "objs" (different ids).
104102
Preserves order.
105103
"""
106-
ids: Set[int] = set()
104+
ids: set[int] = set()
107105
res = []
108106
for obj in objs:
109107
if id(obj) not in ids:
@@ -113,7 +111,7 @@ def _get_distinct_objs(objs: List[Index]) -> List[Index]:
113111

114112

115113
def _get_combined_index(
116-
indexes: List[Index],
114+
indexes: list[Index],
117115
intersect: bool = False,
118116
sort: bool = False,
119117
copy: bool = False,

pandas/core/indexes/extension.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""
22
Shared methods for Index subclasses backed by ExtensionArray.
33
"""
4+
from __future__ import annotations
5+
46
from typing import (
57
Hashable,
6-
List,
7-
Type,
88
TypeVar,
9-
Union,
109
)
1110

1211
import numpy as np
@@ -117,7 +116,7 @@ def method(self, *args, **kwargs):
117116
return method
118117

119118

120-
def inherit_names(names: List[str], delegate, cache: bool = False, wrap: bool = False):
119+
def inherit_names(names: list[str], delegate, cache: bool = False, wrap: bool = False):
121120
"""
122121
Class decorator to pin attributes from an ExtensionArray to a Index subclass.
123122
@@ -227,20 +226,20 @@ class ExtensionIndex(Index):
227226
# The base class already passes through to _data:
228227
# size, __len__, dtype
229228

230-
_data: Union[IntervalArray, NDArrayBackedExtensionArray]
229+
_data: IntervalArray | NDArrayBackedExtensionArray
231230

232-
_data_cls: Union[
233-
Type[Categorical],
234-
Type[DatetimeArray],
235-
Type[TimedeltaArray],
236-
Type[PeriodArray],
237-
Type[IntervalArray],
238-
]
231+
_data_cls: (
232+
type[Categorical]
233+
| type[DatetimeArray]
234+
| type[TimedeltaArray]
235+
| type[PeriodArray]
236+
| type[IntervalArray]
237+
)
239238

240239
@classmethod
241240
def _simple_new(
242241
cls,
243-
array: Union[IntervalArray, NDArrayBackedExtensionArray],
242+
array: IntervalArray | NDArrayBackedExtensionArray,
244243
name: Hashable = None,
245244
):
246245
"""

pandas/tests/api/test_api.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from __future__ import annotations
2+
13
import subprocess
24
import sys
3-
from typing import List
45

56
import pytest
67

@@ -46,7 +47,7 @@ class TestPDApi(Base):
4647
]
4748

4849
# these are already deprecated; awaiting removal
49-
deprecated_modules: List[str] = ["np", "datetime"]
50+
deprecated_modules: list[str] = ["np", "datetime"]
5051

5152
# misc
5253
misc = ["IndexSlice", "NaT", "NA"]
@@ -98,13 +99,13 @@ class TestPDApi(Base):
9899
]
99100

100101
# these are already deprecated; awaiting removal
101-
deprecated_classes: List[str] = []
102+
deprecated_classes: list[str] = []
102103

103104
# these should be deprecated in the future
104-
deprecated_classes_in_future: List[str] = ["SparseArray"]
105+
deprecated_classes_in_future: list[str] = ["SparseArray"]
105106

106107
# external modules exposed in pandas namespace
107-
modules: List[str] = []
108+
modules: list[str] = []
108109

109110
# top-level functions
110111
funcs = [
@@ -181,10 +182,10 @@ class TestPDApi(Base):
181182
funcs_to = ["to_datetime", "to_numeric", "to_pickle", "to_timedelta"]
182183

183184
# top-level to deprecate in the future
184-
deprecated_funcs_in_future: List[str] = []
185+
deprecated_funcs_in_future: list[str] = []
185186

186187
# these are already deprecated; awaiting removal
187-
deprecated_funcs: List[str] = []
188+
deprecated_funcs: list[str] = []
188189

189190
# private modules in pandas namespace
190191
private_modules = [

pandas/tests/arithmetic/test_numeric.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# Arithmetic tests for DataFrame/Series/Index/Array classes that should
22
# behave identically.
33
# Specifically for numeric dtypes
4+
from __future__ import annotations
5+
46
from collections import abc
57
from decimal import Decimal
68
from itertools import combinations
79
import operator
8-
from typing import (
9-
Any,
10-
List,
11-
)
10+
from typing import Any
1211

1312
import numpy as np
1413
import pytest
@@ -56,8 +55,8 @@ def adjust_negative_zero(zero, expected):
5655
# TODO: remove this kludge once mypy stops giving false positives here
5756
# List comprehension has incompatible type List[PandasObject]; expected List[RangeIndex]
5857
# See GH#29725
59-
ser_or_index: List[Any] = [Series, Index]
60-
lefts: List[Any] = [RangeIndex(10, 40, 10)]
58+
ser_or_index: list[Any] = [Series, Index]
59+
lefts: list[Any] = [RangeIndex(10, 40, 10)]
6160
lefts.extend(
6261
[
6362
cls([10, 20, 30], dtype=dtype)

pandas/tests/arrays/masked/test_arithmetic.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import (
2-
Any,
3-
List,
4-
)
1+
from __future__ import annotations
2+
3+
from typing import Any
54

65
import numpy as np
76
import pytest
@@ -12,7 +11,7 @@
1211

1312
# integer dtypes
1413
arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_EA_INT_DTYPES]
15-
scalars: List[Any] = [2] * len(arrays)
14+
scalars: list[Any] = [2] * len(arrays)
1615
# floating dtypes
1716
arrays += [pd.array([0.1, 0.2, 0.3, None], dtype=dtype) for dtype in tm.FLOAT_EA_DTYPES]
1817
scalars += [0.2, 0.2]

pandas/tests/computation/test_eval.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1+
from __future__ import annotations
2+
13
from functools import reduce
24
from itertools import product
35
import operator
4-
from typing import (
5-
Dict,
6-
List,
7-
Type,
8-
)
96
import warnings
107

118
import numpy as np
@@ -147,8 +144,8 @@ def lhs(request):
147144

148145
@td.skip_if_no_ne
149146
class TestEvalNumexprPandas:
150-
exclude_cmp: List[str] = []
151-
exclude_bool: List[str] = []
147+
exclude_cmp: list[str] = []
148+
exclude_bool: list[str] = []
152149

153150
engine = "numexpr"
154151
parser = "pandas"
@@ -1125,7 +1122,7 @@ def test_performance_warning_for_poor_alignment(self, engine, parser):
11251122

11261123
@td.skip_if_no_ne
11271124
class TestOperationsNumExprPandas:
1128-
exclude_arith: List[str] = []
1125+
exclude_arith: list[str] = []
11291126

11301127
engine = "numexpr"
11311128
parser = "pandas"
@@ -1629,7 +1626,7 @@ def test_simple_in_ops(self):
16291626

16301627
@td.skip_if_no_ne
16311628
class TestOperationsNumExprPython(TestOperationsNumExprPandas):
1632-
exclude_arith: List[str] = ["in", "not in"]
1629+
exclude_arith: list[str] = ["in", "not in"]
16331630

16341631
engine = "numexpr"
16351632
parser = "python"
@@ -1723,7 +1720,7 @@ class TestOperationsPythonPython(TestOperationsNumExprPython):
17231720

17241721

17251722
class TestOperationsPythonPandas(TestOperationsNumExprPandas):
1726-
exclude_arith: List[str] = []
1723+
exclude_arith: list[str] = []
17271724

17281725
engine = "python"
17291726
parser = "pandas"
@@ -1878,7 +1875,7 @@ def test_invalid_parser():
18781875
pd.eval("x + y", local_dict={"x": 1, "y": 2}, parser="asdf")
18791876

18801877

1881-
_parsers: Dict[str, Type[BaseExprVisitor]] = {
1878+
_parsers: dict[str, type[BaseExprVisitor]] = {
18821879
"python": PythonExprVisitor,
18831880
"pytables": pytables.PyTablesExprVisitor,
18841881
"pandas": PandasExprVisitor,

0 commit comments

Comments
 (0)