Skip to content

Commit 4019d41

Browse files
authored
STY: pyupgrade to 3.9 (#54044)
1 parent f967260 commit 4019d41

File tree

149 files changed

+619
-537
lines changed

Some content is hidden

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

149 files changed

+619
-537
lines changed

.pre-commit-config.yaml

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repos:
2020
rev: 23.3.0
2121
hooks:
2222
- id: black
23-
- repo: https://github.com/charliermarsh/ruff-pre-commit
23+
- repo: https://github.com/astral-sh/ruff-pre-commit
2424
rev: v0.0.270
2525
hooks:
2626
- id: ruff
@@ -94,7 +94,7 @@ repos:
9494
rev: v3.4.0
9595
hooks:
9696
- id: pyupgrade
97-
args: [--py38-plus]
97+
args: [--py39-plus]
9898
- repo: https://github.com/pre-commit/pygrep-hooks
9999
rev: v1.10.0
100100
hooks:
@@ -179,9 +179,6 @@ repos:
179179
|np\.bool[^_8`]
180180
|np\.object[^_8`]
181181
182-
# imports from collections.abc instead of `from collections import abc`
183-
|from\ collections\.abc\ import
184-
185182
# Numpy
186183
|from\ numpy\ import\ random
187184
|from\ numpy\.random\ import

doc/source/conf.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -586,14 +586,7 @@ class AccessorCallableDocumenter(AccessorLevelDocumenter, MethodDocumenter):
586586
priority = 0.5
587587

588588
def format_name(self):
589-
if sys.version_info < (3, 9):
590-
# NOTE pyupgrade will remove this when we run it with --py39-plus
591-
# so don't remove the unnecessary `else` statement below
592-
from pandas.util._str_methods import removesuffix
593-
594-
return removesuffix(MethodDocumenter.format_name(self), ".__call__")
595-
else:
596-
return MethodDocumenter.format_name(self).removesuffix(".__call__")
589+
return MethodDocumenter.format_name(self).removesuffix(".__call__")
597590

598591

599592
class PandasAutosummary(Autosummary):

pandas/_config/config.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@
5656
)
5757
import re
5858
from typing import (
59+
TYPE_CHECKING,
5960
Any,
6061
Callable,
61-
Generator,
6262
Generic,
63-
Iterable,
6463
NamedTuple,
6564
cast,
6665
)
@@ -72,6 +71,12 @@
7271
)
7372
from pandas.util._exceptions import find_stack_level
7473

74+
if TYPE_CHECKING:
75+
from collections.abc import (
76+
Generator,
77+
Iterable,
78+
)
79+
7580

7681
class DeprecatedOption(NamedTuple):
7782
key: str

pandas/_config/localization.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
import platform
1111
import re
1212
import subprocess
13-
from typing import Generator
13+
from typing import TYPE_CHECKING
1414

1515
from pandas._config.config import options
1616

17+
if TYPE_CHECKING:
18+
from collections.abc import Generator
19+
1720

1821
@contextmanager
1922
def set_locale(

pandas/_testing/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import collections
4+
from collections import Counter
45
from datetime import datetime
56
from decimal import Decimal
67
import operator
@@ -12,8 +13,6 @@
1213
TYPE_CHECKING,
1314
Callable,
1415
ContextManager,
15-
Counter,
16-
Iterable,
1716
cast,
1817
)
1918

@@ -109,6 +108,8 @@
109108
from pandas.core.construction import extract_array
110109

111110
if TYPE_CHECKING:
111+
from collections.abc import Iterable
112+
112113
from pandas._typing import (
113114
Dtype,
114115
Frequency,

pandas/_testing/_warnings.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import re
88
import sys
99
from typing import (
10-
Generator,
10+
TYPE_CHECKING,
1111
Literal,
12-
Sequence,
13-
Type,
1412
cast,
1513
)
1614
import warnings
1715

16+
if TYPE_CHECKING:
17+
from collections.abc import (
18+
Generator,
19+
Sequence,
20+
)
21+
1822

1923
@contextmanager
2024
def assert_produces_warning(
@@ -91,7 +95,7 @@ class for all warnings. To raise multiple types of exceptions,
9195
yield w
9296
finally:
9397
if expected_warning:
94-
expected_warning = cast(Type[Warning], expected_warning)
98+
expected_warning = cast(type[Warning], expected_warning)
9599
_assert_caught_expected_warning(
96100
caught_warnings=w,
97101
expected_warning=expected_warning,
@@ -195,7 +199,7 @@ def _is_unexpected_warning(
195199
"""Check if the actual warning issued is unexpected."""
196200
if actual_warning and not expected_warning:
197201
return True
198-
expected_warning = cast(Type[Warning], expected_warning)
202+
expected_warning = cast(type[Warning], expected_warning)
199203
return bool(not issubclass(actual_warning.category, expected_warning))
200204

201205

pandas/_testing/contexts.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
IO,
99
TYPE_CHECKING,
1010
Any,
11-
Generator,
1211
)
1312
import uuid
1413

@@ -20,6 +19,8 @@
2019
from pandas.io.common import get_handle
2120

2221
if TYPE_CHECKING:
22+
from collections.abc import Generator
23+
2324
from pandas._typing import (
2425
BaseBuffer,
2526
CompressionOptions,

pandas/_typing.py

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from __future__ import annotations
22

3+
from collections.abc import (
4+
Hashable,
5+
Iterator,
6+
Mapping,
7+
Sequence,
8+
)
39
from datetime import (
410
datetime,
511
timedelta,
@@ -11,16 +17,9 @@
1117
TYPE_CHECKING,
1218
Any,
1319
Callable,
14-
Dict,
15-
Hashable,
16-
Iterator,
17-
List,
1820
Literal,
19-
Mapping,
2021
Optional,
2122
Protocol,
22-
Sequence,
23-
Tuple,
2423
Type as type_t,
2524
TypeVar,
2625
Union,
@@ -111,7 +110,7 @@
111110
# Cannot use `Sequence` because a string is a sequence, and we don't want to
112111
# accept that. Could refine if https://github.com/python/typing/issues/256 is
113112
# resolved to differentiate between Sequence[str] and str
114-
ListLike = Union[AnyArrayLike, List, range]
113+
ListLike = Union[AnyArrayLike, list, range]
115114

116115
# scalars
117116

@@ -146,10 +145,10 @@
146145
Axis = Union[AxisInt, Literal["index", "columns", "rows"]]
147146
IndexLabel = Union[Hashable, Sequence[Hashable]]
148147
Level = Hashable
149-
Shape = Tuple[int, ...]
150-
Suffixes = Tuple[Optional[str], Optional[str]]
148+
Shape = tuple[int, ...]
149+
Suffixes = tuple[Optional[str], Optional[str]]
151150
Ordered = Optional[bool]
152-
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]
151+
JSONSerializable = Optional[Union[PythonScalar, list, dict]]
153152
Frequency = Union[str, "BaseOffset"]
154153
Axes = ListLike
155154

@@ -166,15 +165,15 @@
166165
Dtype = Union["ExtensionDtype", NpDtype]
167166
AstypeArg = Union["ExtensionDtype", "npt.DTypeLike"]
168167
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
169-
DtypeArg = Union[Dtype, Dict[Hashable, Dtype]]
168+
DtypeArg = Union[Dtype, dict[Hashable, Dtype]]
170169
DtypeObj = Union[np.dtype, "ExtensionDtype"]
171170

172171
# converters
173-
ConvertersArg = Dict[Hashable, Callable[[Dtype], Dtype]]
172+
ConvertersArg = dict[Hashable, Callable[[Dtype], Dtype]]
174173

175174
# parse_dates
176175
ParseDatesArg = Union[
177-
bool, List[Hashable], List[List[Hashable]], Dict[Hashable, List[Hashable]]
176+
bool, list[Hashable], list[list[Hashable]], dict[Hashable, list[Hashable]]
178177
]
179178

180179
# For functions like rename that convert one label to another
@@ -195,10 +194,10 @@
195194

196195
# types of `func` kwarg for DataFrame.aggregate and Series.aggregate
197196
AggFuncTypeBase = Union[Callable, str]
198-
AggFuncTypeDict = Dict[Hashable, Union[AggFuncTypeBase, List[AggFuncTypeBase]]]
197+
AggFuncTypeDict = dict[Hashable, Union[AggFuncTypeBase, list[AggFuncTypeBase]]]
199198
AggFuncType = Union[
200199
AggFuncTypeBase,
201-
List[AggFuncTypeBase],
200+
list[AggFuncTypeBase],
202201
AggFuncTypeDict,
203202
]
204203
AggObjType = Union[
@@ -286,18 +285,18 @@ def closed(self) -> bool:
286285
FilePath = Union[str, "PathLike[str]"]
287286

288287
# for arbitrary kwargs passed during reading/writing files
289-
StorageOptions = Optional[Dict[str, Any]]
288+
StorageOptions = Optional[dict[str, Any]]
290289

291290

292291
# compression keywords and compression
293-
CompressionDict = Dict[str, Any]
292+
CompressionDict = dict[str, Any]
294293
CompressionOptions = Optional[
295294
Union[Literal["infer", "gzip", "bz2", "zip", "xz", "zstd", "tar"], CompressionDict]
296295
]
297296

298297
# types in DataFrameFormatter
299298
FormattersType = Union[
300-
List[Callable], Tuple[Callable, ...], Mapping[Union[str, int], Callable]
299+
list[Callable], tuple[Callable, ...], Mapping[Union[str, int], Callable]
301300
]
302301
ColspaceType = Mapping[Hashable, Union[str, int]]
303302
FloatFormatType = Union[str, Callable, "EngFormatter"]
@@ -347,9 +346,9 @@ def closed(self) -> bool:
347346
# https://bugs.python.org/issue41810
348347
# Using List[int] here rather than Sequence[int] to disallow tuples.
349348
ScalarIndexer = Union[int, np.integer]
350-
SequenceIndexer = Union[slice, List[int], np.ndarray]
349+
SequenceIndexer = Union[slice, list[int], np.ndarray]
351350
PositionalIndexer = Union[ScalarIndexer, SequenceIndexer]
352-
PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer]
351+
PositionalIndexerTuple = tuple[PositionalIndexer, PositionalIndexer]
353352
PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple]
354353
if TYPE_CHECKING:
355354
TakeIndexer = Union[Sequence[int], Sequence[np.integer], npt.NDArray[np.integer]]

pandas/_version.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import re
1717
import subprocess
1818
import sys
19-
from typing import (
20-
Callable,
21-
Dict,
22-
)
19+
from typing import Callable
2320

2421

2522
def get_keywords():
@@ -57,8 +54,8 @@ class NotThisMethod(Exception):
5754
"""Exception raised if a method is not valid for the current scenario."""
5855

5956

60-
LONG_VERSION_PY: Dict[str, str] = {}
61-
HANDLERS: Dict[str, Dict[str, Callable]] = {}
57+
LONG_VERSION_PY: dict[str, str] = {}
58+
HANDLERS: dict[str, dict[str, Callable]] = {}
6259

6360

6461
def register_vcs_handler(vcs, method): # decorator

pandas/compat/pickle_compat.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import copy
88
import io
99
import pickle as pkl
10-
from typing import Generator
10+
from typing import TYPE_CHECKING
1111

1212
import numpy as np
1313

@@ -22,6 +22,9 @@
2222
)
2323
from pandas.core.internals import BlockManager
2424

25+
if TYPE_CHECKING:
26+
from collections.abc import Generator
27+
2528

2629
def load_reduce(self):
2730
stack = self.stack

pandas/conftest.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
import os
3333
from pathlib import Path
3434
from typing import (
35+
TYPE_CHECKING,
3536
Callable,
36-
Hashable,
37-
Iterator,
3837
)
3938

4039
from dateutil.tz import (
@@ -73,6 +72,12 @@
7372
MultiIndex,
7473
)
7574

75+
if TYPE_CHECKING:
76+
from collections.abc import (
77+
Hashable,
78+
Iterator,
79+
)
80+
7681
try:
7782
import pyarrow as pa
7883
except ImportError:

0 commit comments

Comments
 (0)