Skip to content

Commit e34603d

Browse files
TYP: use from __future__ import annotations more - batch 3 (#41895)
1 parent db1db7e commit e34603d

File tree

10 files changed

+116
-138
lines changed

10 files changed

+116
-138
lines changed

pandas/core/internals/base.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
Base class for the internal managers. Both BlockManager and ArrayManager
33
inherit from this class.
44
"""
5-
from typing import (
6-
List,
7-
Optional,
8-
TypeVar,
9-
)
5+
from __future__ import annotations
6+
7+
from typing import TypeVar
108

119
from pandas._typing import (
1210
DtypeObj,
@@ -27,7 +25,7 @@ class DataManager(PandasObject):
2725

2826
# TODO share more methods/attributes
2927

30-
axes: List[Index]
28+
axes: list[Index]
3129

3230
@property
3331
def items(self) -> Index:
@@ -123,7 +121,7 @@ def equals(self, other: object) -> bool:
123121
def apply(
124122
self: T,
125123
f,
126-
align_keys: Optional[List[str]] = None,
124+
align_keys: list[str] | None = None,
127125
ignore_failures: bool = False,
128126
**kwargs,
129127
) -> T:
@@ -144,7 +142,7 @@ def array(self):
144142
return self.arrays[0] # type: ignore[attr-defined]
145143

146144

147-
def interleaved_dtype(dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
145+
def interleaved_dtype(dtypes: list[DtypeObj]) -> DtypeObj | None:
148146
"""
149147
Find the common dtype for `blocks`.
150148

pandas/core/ops/docstrings.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
"""
22
Templating for ops docstrings
33
"""
4-
from typing import (
5-
Dict,
6-
Optional,
7-
)
4+
from __future__ import annotations
85

96

107
def make_flex_doc(op_name: str, typ: str) -> str:
@@ -297,7 +294,7 @@ def make_flex_doc(op_name: str, typ: str) -> str:
297294

298295
_returns_tuple = """2-Tuple of Series\n The result of the operation."""
299296

300-
_op_descriptions: Dict[str, Dict[str, Optional[str]]] = {
297+
_op_descriptions: dict[str, dict[str, str | None]] = {
301298
# Arithmetic Operators
302299
"add": {
303300
"op": "+",

pandas/core/ops/mask_ops.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
"""
22
Ops for masked arrays.
33
"""
4-
from typing import (
5-
Optional,
6-
Union,
7-
)
4+
from __future__ import annotations
85

96
import numpy as np
107

@@ -15,10 +12,10 @@
1512

1613

1714
def kleene_or(
18-
left: Union[bool, np.ndarray],
19-
right: Union[bool, np.ndarray],
20-
left_mask: Optional[np.ndarray],
21-
right_mask: Optional[np.ndarray],
15+
left: bool | np.ndarray,
16+
right: bool | np.ndarray,
17+
left_mask: np.ndarray | None,
18+
right_mask: np.ndarray | None,
2219
):
2320
"""
2421
Boolean ``or`` using Kleene logic.
@@ -76,10 +73,10 @@ def kleene_or(
7673

7774

7875
def kleene_xor(
79-
left: Union[bool, np.ndarray],
80-
right: Union[bool, np.ndarray],
81-
left_mask: Optional[np.ndarray],
82-
right_mask: Optional[np.ndarray],
76+
left: bool | np.ndarray,
77+
right: bool | np.ndarray,
78+
left_mask: np.ndarray | None,
79+
right_mask: np.ndarray | None,
8380
):
8481
"""
8582
Boolean ``xor`` using Kleene logic.
@@ -125,10 +122,10 @@ def kleene_xor(
125122

126123

127124
def kleene_and(
128-
left: Union[bool, libmissing.NAType, np.ndarray],
129-
right: Union[bool, libmissing.NAType, np.ndarray],
130-
left_mask: Optional[np.ndarray],
131-
right_mask: Optional[np.ndarray],
125+
left: bool | libmissing.NAType | np.ndarray,
126+
right: bool | libmissing.NAType | np.ndarray,
127+
left_mask: np.ndarray | None,
128+
right_mask: np.ndarray | None,
132129
):
133130
"""
134131
Boolean ``and`` using Kleene logic.

pandas/core/tools/numeric.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from __future__ import annotations
22

33
import numpy as np
44

@@ -166,7 +166,7 @@ def to_numeric(arg, errors="raise", downcast=None):
166166

167167
# GH33013: for IntegerArray & FloatingArray extract non-null values for casting
168168
# save mask to reconstruct the full array after casting
169-
mask: Optional[np.ndarray] = None
169+
mask: np.ndarray | None = None
170170
if isinstance(values, NumericArray):
171171
mask = values._mask
172172
values = values._data[~mask]

pandas/core/tools/times.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
from __future__ import annotations
2+
13
from datetime import (
24
datetime,
35
time,
46
)
5-
from typing import (
6-
List,
7-
Optional,
8-
)
97

108
import numpy as np
119

@@ -61,7 +59,7 @@ def _convert_listlike(arg, format):
6159
if infer_time_format and format is None:
6260
format = _guess_time_format_for_array(arg)
6361

64-
times: List[Optional[time]] = []
62+
times: list[time | None] = []
6563
if format is not None:
6664
for element in arg:
6765
try:

pandas/core/util/numba_.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
"""Common utilities for Numba operations"""
2+
from __future__ import annotations
3+
24
import types
3-
from typing import (
4-
Callable,
5-
Dict,
6-
Optional,
7-
Tuple,
8-
)
5+
from typing import Callable
96

107
import numpy as np
118

@@ -15,10 +12,10 @@
1512
from pandas.util.version import Version
1613

1714
GLOBAL_USE_NUMBA: bool = False
18-
NUMBA_FUNC_CACHE: Dict[Tuple[Callable, str], Callable] = {}
15+
NUMBA_FUNC_CACHE: dict[tuple[Callable, str], Callable] = {}
1916

2017

21-
def maybe_use_numba(engine: Optional[str]) -> bool:
18+
def maybe_use_numba(engine: str | None) -> bool:
2219
"""Signal whether to use numba routines."""
2320
return engine == "numba" or (engine is None and GLOBAL_USE_NUMBA)
2421

@@ -31,8 +28,8 @@ def set_use_numba(enable: bool = False) -> None:
3128

3229

3330
def get_jit_arguments(
34-
engine_kwargs: Optional[Dict[str, bool]] = None, kwargs: Optional[Dict] = None
35-
) -> Tuple[bool, bool, bool]:
31+
engine_kwargs: dict[str, bool] | None = None, kwargs: dict | None = None
32+
) -> tuple[bool, bool, bool]:
3633
"""
3734
Return arguments to pass to numba.JIT, falling back on pandas default JIT settings.
3835

pandas/core/window/expanding.py

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
from __future__ import annotations
2+
13
from textwrap import dedent
24
from typing import (
35
Any,
46
Callable,
5-
Dict,
6-
Optional,
7-
Tuple,
87
)
98

109
from pandas._typing import (
@@ -184,10 +183,10 @@ def apply(
184183
self,
185184
func: Callable[..., Any],
186185
raw: bool = False,
187-
engine: Optional[str] = None,
188-
engine_kwargs: Optional[Dict[str, bool]] = None,
189-
args: Optional[Tuple[Any, ...]] = None,
190-
kwargs: Optional[Dict[str, Any]] = None,
186+
engine: str | None = None,
187+
engine_kwargs: dict[str, bool] | None = None,
188+
args: tuple[Any, ...] | None = None,
189+
kwargs: dict[str, Any] | None = None,
191190
):
192191
return super().apply(
193192
func,
@@ -217,8 +216,8 @@ def apply(
217216
def sum(
218217
self,
219218
*args,
220-
engine: Optional[str] = None,
221-
engine_kwargs: Optional[Dict[str, bool]] = None,
219+
engine: str | None = None,
220+
engine_kwargs: dict[str, bool] | None = None,
222221
**kwargs,
223222
):
224223
nv.validate_expanding_func("sum", args, kwargs)
@@ -243,8 +242,8 @@ def sum(
243242
def max(
244243
self,
245244
*args,
246-
engine: Optional[str] = None,
247-
engine_kwargs: Optional[Dict[str, bool]] = None,
245+
engine: str | None = None,
246+
engine_kwargs: dict[str, bool] | None = None,
248247
**kwargs,
249248
):
250249
nv.validate_expanding_func("max", args, kwargs)
@@ -269,8 +268,8 @@ def max(
269268
def min(
270269
self,
271270
*args,
272-
engine: Optional[str] = None,
273-
engine_kwargs: Optional[Dict[str, bool]] = None,
271+
engine: str | None = None,
272+
engine_kwargs: dict[str, bool] | None = None,
274273
**kwargs,
275274
):
276275
nv.validate_expanding_func("min", args, kwargs)
@@ -295,8 +294,8 @@ def min(
295294
def mean(
296295
self,
297296
*args,
298-
engine: Optional[str] = None,
299-
engine_kwargs: Optional[Dict[str, bool]] = None,
297+
engine: str | None = None,
298+
engine_kwargs: dict[str, bool] | None = None,
300299
**kwargs,
301300
):
302301
nv.validate_expanding_func("mean", args, kwargs)
@@ -319,8 +318,8 @@ def mean(
319318
)
320319
def median(
321320
self,
322-
engine: Optional[str] = None,
323-
engine_kwargs: Optional[Dict[str, bool]] = None,
321+
engine: str | None = None,
322+
engine_kwargs: dict[str, bool] | None = None,
324323
**kwargs,
325324
):
326325
return super().median(engine=engine, engine_kwargs=engine_kwargs, **kwargs)
@@ -592,8 +591,8 @@ def quantile(
592591
)
593592
def cov(
594593
self,
595-
other: Optional[FrameOrSeriesUnion] = None,
596-
pairwise: Optional[bool] = None,
594+
other: FrameOrSeriesUnion | None = None,
595+
pairwise: bool | None = None,
597596
ddof: int = 1,
598597
**kwargs,
599598
):
@@ -657,8 +656,8 @@ def cov(
657656
)
658657
def corr(
659658
self,
660-
other: Optional[FrameOrSeriesUnion] = None,
661-
pairwise: Optional[bool] = None,
659+
other: FrameOrSeriesUnion | None = None,
660+
pairwise: bool | None = None,
662661
ddof: int = 1,
663662
**kwargs,
664663
):

0 commit comments

Comments
 (0)