Skip to content

Commit 3ea9fcd

Browse files
committed
add future imports
1 parent 7d4757b commit 3ea9fcd

39 files changed

+246
-250
lines changed

pandas/_config/config.py

+16-19
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
4949
"""
5050

51+
from __future__ import annotations
52+
5153
from collections import namedtuple
5254
from contextlib import (
5355
ContextDecorator,
@@ -57,12 +59,7 @@
5759
from typing import (
5860
Any,
5961
Callable,
60-
Dict,
6162
Iterable,
62-
List,
63-
Optional,
64-
Tuple,
65-
Type,
6663
cast,
6764
)
6865
import warnings
@@ -73,16 +70,16 @@
7370
RegisteredOption = namedtuple("RegisteredOption", "key defval doc validator cb")
7471

7572
# holds deprecated option metadata
76-
_deprecated_options: Dict[str, DeprecatedOption] = {}
73+
_deprecated_options: dict[str, DeprecatedOption] = {}
7774

7875
# holds registered option metadata
79-
_registered_options: Dict[str, RegisteredOption] = {}
76+
_registered_options: dict[str, RegisteredOption] = {}
8077

8178
# holds the current values for registered options
82-
_global_config: Dict[str, Any] = {}
79+
_global_config: dict[str, Any] = {}
8380

8481
# keys which have a special meaning
85-
_reserved_keys: List[str] = ["all"]
82+
_reserved_keys: list[str] = ["all"]
8683

8784

8885
class OptionError(AttributeError, KeyError):
@@ -194,7 +191,7 @@ def get_default_val(pat: str):
194191
class DictWrapper:
195192
""" provide attribute-style access to a nested dict"""
196193

197-
def __init__(self, d: Dict[str, Any], prefix: str = ""):
194+
def __init__(self, d: dict[str, Any], prefix: str = ""):
198195
object.__setattr__(self, "d", d)
199196
object.__setattr__(self, "prefix", prefix)
200197

@@ -428,8 +425,8 @@ def register_option(
428425
key: str,
429426
defval: object,
430427
doc: str = "",
431-
validator: Optional[Callable[[Any], Any]] = None,
432-
cb: Optional[Callable[[str], Any]] = None,
428+
validator: Callable[[Any], Any] | None = None,
429+
cb: Callable[[str], Any] | None = None,
433430
) -> None:
434431
"""
435432
Register an option in the package-wide pandas config object
@@ -500,7 +497,7 @@ def register_option(
500497

501498

502499
def deprecate_option(
503-
key: str, msg: Optional[str] = None, rkey: Optional[str] = None, removal_ver=None
500+
key: str, msg: str | None = None, rkey: str | None = None, removal_ver=None
504501
) -> None:
505502
"""
506503
Mark option `key` as deprecated, if code attempts to access this option,
@@ -547,7 +544,7 @@ def deprecate_option(
547544
# functions internal to the module
548545

549546

550-
def _select_options(pat: str) -> List[str]:
547+
def _select_options(pat: str) -> list[str]:
551548
"""
552549
returns a list of keys matching `pat`
553550
@@ -565,7 +562,7 @@ def _select_options(pat: str) -> List[str]:
565562
return [k for k in keys if re.search(pat, k, re.I)]
566563

567564

568-
def _get_root(key: str) -> Tuple[Dict[str, Any], str]:
565+
def _get_root(key: str) -> tuple[dict[str, Any], str]:
569566
path = key.split(".")
570567
cursor = _global_config
571568
for p in path[:-1]:
@@ -674,7 +671,7 @@ def pp_options_list(keys: Iterable[str], width=80, _print: bool = False):
674671
from itertools import groupby
675672
from textwrap import wrap
676673

677-
def pp(name: str, ks: Iterable[str]) -> List[str]:
674+
def pp(name: str, ks: Iterable[str]) -> list[str]:
678675
pfx = "- " + name + ".[" if name else ""
679676
ls = wrap(
680677
", ".join(ks),
@@ -687,7 +684,7 @@ def pp(name: str, ks: Iterable[str]) -> List[str]:
687684
ls[-1] = ls[-1] + "]"
688685
return ls
689686

690-
ls: List[str] = []
687+
ls: list[str] = []
691688
singles = [x for x in sorted(keys) if x.find(".") < 0]
692689
if singles:
693690
ls += pp("", singles)
@@ -760,7 +757,7 @@ def inner(key: str, *args, **kwds):
760757
# arg in register_option
761758

762759

763-
def is_type_factory(_type: Type[Any]) -> Callable[[Any], None]:
760+
def is_type_factory(_type: type[Any]) -> Callable[[Any], None]:
764761
"""
765762
766763
Parameters
@@ -826,7 +823,7 @@ def inner(x) -> None:
826823
return inner
827824

828825

829-
def is_nonnegative_int(value: Optional[int]) -> None:
826+
def is_nonnegative_int(value: int | None) -> None:
830827
"""
831828
Verify that value is None or a positive int.
832829

pandas/_config/display.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
Unopinionated display configuration.
33
"""
44

5+
from __future__ import annotations
6+
57
import locale
68
import sys
7-
from typing import Optional
89

910
from pandas._config import config as cf
1011

1112
# -----------------------------------------------------------------------------
1213
# Global formatting options
13-
_initial_defencoding: Optional[str] = None
14+
_initial_defencoding: str | None = None
1415

1516

1617
def detect_console_encoding() -> str:

pandas/_testing/_io.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
from __future__ import annotations
2+
13
import bz2
24
from functools import wraps
35
import gzip
46
from typing import (
57
Any,
68
Callable,
7-
Optional,
8-
Tuple,
99
)
1010
import zipfile
1111

@@ -272,9 +272,7 @@ def can_connect(url, error_classes=None):
272272
# File-IO
273273

274274

275-
def round_trip_pickle(
276-
obj: Any, path: Optional[FilePathOrBuffer] = None
277-
) -> FrameOrSeries:
275+
def round_trip_pickle(obj: Any, path: FilePathOrBuffer | None = None) -> FrameOrSeries:
278276
"""
279277
Pickle an object and then read it again.
280278
@@ -298,7 +296,7 @@ def round_trip_pickle(
298296
return pd.read_pickle(temp_path)
299297

300298

301-
def round_trip_pathlib(writer, reader, path: Optional[str] = None):
299+
def round_trip_pathlib(writer, reader, path: str | None = None):
302300
"""
303301
Write an object to file specified by a pathlib.Path and read it back
304302
@@ -327,7 +325,7 @@ def round_trip_pathlib(writer, reader, path: Optional[str] = None):
327325
return obj
328326

329327

330-
def round_trip_localpath(writer, reader, path: Optional[str] = None):
328+
def round_trip_localpath(writer, reader, path: str | None = None):
331329
"""
332330
Write an object to file specified by a py.path LocalPath and read it back.
333331
@@ -375,7 +373,7 @@ def write_to_compressed(compression, path, data, dest="test"):
375373
------
376374
ValueError : An invalid compression value was passed in.
377375
"""
378-
args: Tuple[Any, ...] = (data,)
376+
args: tuple[Any, ...] = (data,)
379377
mode = "wb"
380378
method = "write"
381379
compress_method: Callable

pandas/_testing/_warnings.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1+
from __future__ import annotations
2+
13
from contextlib import contextmanager
24
import re
35
from typing import (
4-
Optional,
56
Sequence,
67
Type,
7-
Union,
88
cast,
99
)
1010
import warnings
1111

1212

1313
@contextmanager
1414
def assert_produces_warning(
15-
expected_warning: Optional[Union[Type[Warning], bool]] = Warning,
15+
expected_warning: type[Warning] | bool | None = Warning,
1616
filter_level="always",
1717
check_stacklevel: bool = True,
1818
raise_on_extra_warnings: bool = True,
19-
match: Optional[str] = None,
19+
match: str | None = None,
2020
):
2121
"""
2222
Context manager for running code expected to either raise a specific
@@ -99,8 +99,8 @@ class for all warnings. To check that no warning is returned,
9999
def _assert_caught_expected_warning(
100100
*,
101101
caught_warnings: Sequence[warnings.WarningMessage],
102-
expected_warning: Type[Warning],
103-
match: Optional[str],
102+
expected_warning: type[Warning],
103+
match: str | None,
104104
check_stacklevel: bool,
105105
) -> None:
106106
"""Assert that there was the expected warning among the caught warnings."""
@@ -135,7 +135,7 @@ def _assert_caught_expected_warning(
135135
def _assert_caught_no_extra_warnings(
136136
*,
137137
caught_warnings: Sequence[warnings.WarningMessage],
138-
expected_warning: Optional[Union[Type[Warning], bool]],
138+
expected_warning: type[Warning] | bool | None,
139139
) -> None:
140140
"""Assert that no extra warnings apart from the expected ones are caught."""
141141
extra_warnings = []
@@ -157,7 +157,7 @@ def _assert_caught_no_extra_warnings(
157157

158158
def _is_unexpected_warning(
159159
actual_warning: warnings.WarningMessage,
160-
expected_warning: Optional[Union[Type[Warning], bool]],
160+
expected_warning: type[Warning] | bool | None,
161161
) -> bool:
162162
"""Check if the actual warning issued is unexpected."""
163163
if actual_warning and not expected_warning:

pandas/_testing/asserters.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import (
2-
Union,
3-
cast,
4-
)
1+
from __future__ import annotations
2+
3+
from typing import cast
54
import warnings
65

76
import numpy as np
@@ -56,8 +55,8 @@
5655
def assert_almost_equal(
5756
left,
5857
right,
59-
check_dtype: Union[bool, str] = "equiv",
60-
check_less_precise: Union[bool, int, NoDefault] = no_default,
58+
check_dtype: bool | str = "equiv",
59+
check_less_precise: bool | int | NoDefault = no_default,
6160
rtol: float = 1.0e-5,
6261
atol: float = 1.0e-8,
6362
**kwargs,
@@ -169,7 +168,7 @@ def assert_almost_equal(
169168
)
170169

171170

172-
def _get_tol_from_less_precise(check_less_precise: Union[bool, int]) -> float:
171+
def _get_tol_from_less_precise(check_less_precise: bool | int) -> float:
173172
"""
174173
Return the tolerance equivalent to the deprecated `check_less_precise`
175174
parameter.
@@ -247,9 +246,9 @@ def assert_dict_equal(left, right, compare_keys: bool = True):
247246
def assert_index_equal(
248247
left: Index,
249248
right: Index,
250-
exact: Union[bool, str] = "equiv",
249+
exact: bool | str = "equiv",
251250
check_names: bool = True,
252-
check_less_precise: Union[bool, int, NoDefault] = no_default,
251+
check_less_precise: bool | int | NoDefault = no_default,
253252
check_exact: bool = True,
254253
check_categorical: bool = True,
255254
check_order: bool = True,
@@ -429,7 +428,7 @@ def _get_ilevel_values(index, level):
429428
assert_categorical_equal(left._values, right._values, obj=f"{obj} category")
430429

431430

432-
def assert_class_equal(left, right, exact: Union[bool, str] = True, obj="Input"):
431+
def assert_class_equal(left, right, exact: bool | str = True, obj="Input"):
433432
"""
434433
Checks classes are equal.
435434
"""

pandas/_testing/contexts.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from contextlib import contextmanager
24
import os
35
from pathlib import Path
@@ -8,7 +10,6 @@
810
from typing import (
911
IO,
1012
Any,
11-
Union,
1213
)
1314

1415
import numpy as np
@@ -111,7 +112,7 @@ def ensure_clean(filename=None, return_filelike: bool = False, **kwargs: Any):
111112

112113
path.touch()
113114

114-
handle_or_str: Union[str, IO] = str(path)
115+
handle_or_str: str | IO = str(path)
115116
if return_filelike:
116117
kwargs.setdefault("mode", "w+b")
117118
handle_or_str = open(path, **kwargs)

pandas/compat/_optional.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from __future__ import annotations
2+
13
import distutils.version
24
import importlib
35
import sys
46
import types
5-
from typing import Optional
67
import warnings
78

89
# Update install.rst when updating versions!
@@ -63,7 +64,7 @@ def import_optional_dependency(
6364
name: str,
6465
extra: str = "",
6566
errors: str = "raise",
66-
min_version: Optional[str] = None,
67+
min_version: str | None = None,
6768
):
6869
"""
6970
Import an optional dependency.

pandas/core/accessor.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,30 @@
44
that can be mixed into or pinned onto other pandas classes.
55
66
"""
7-
from typing import (
8-
FrozenSet,
9-
List,
10-
Set,
11-
)
7+
from __future__ import annotations
8+
129
import warnings
1310

1411
from pandas.util._decorators import doc
1512

1613

1714
class DirNamesMixin:
18-
_accessors: Set[str] = set()
19-
_hidden_attrs: FrozenSet[str] = frozenset()
15+
_accessors: set[str] = set()
16+
_hidden_attrs: frozenset[str] = frozenset()
2017

21-
def _dir_deletions(self) -> Set[str]:
18+
def _dir_deletions(self) -> set[str]:
2219
"""
2320
Delete unwanted __dir__ for this object.
2421
"""
2522
return self._accessors | self._hidden_attrs
2623

27-
def _dir_additions(self) -> Set[str]:
24+
def _dir_additions(self) -> set[str]:
2825
"""
2926
Add additional __dir__ for this object.
3027
"""
3128
return {accessor for accessor in self._accessors if hasattr(self, accessor)}
3229

33-
def __dir__(self) -> List[str]:
30+
def __dir__(self) -> list[str]:
3431
"""
3532
Provide method name lookup and completion.
3633

0 commit comments

Comments
 (0)