Skip to content

Commit e9655f5

Browse files
authored
STYLE add future annotations where possible (#47769)
add future annotations
1 parent 2ff1d0a commit e9655f5

30 files changed

+97
-48
lines changed

.pre-commit-config.yaml

+12-6
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ repos:
9494
stages: [manual]
9595
additional_dependencies: &pyright_dependencies
9696
97-
- repo: local
98-
hooks:
9997
- id: pyright_reportGeneralTypeIssues
10098
name: pyright reportGeneralTypeIssues
10199
entry: pyright --skipunannotated -p pyright_reportGeneralTypeIssues.json
@@ -105,8 +103,6 @@ repos:
105103
types: [python]
106104
stages: [manual]
107105
additional_dependencies: *pyright_dependencies
108-
- repo: local
109-
hooks:
110106
- id: mypy
111107
name: mypy
112108
entry: mypy
@@ -115,8 +111,6 @@ repos:
115111
pass_filenames: false
116112
types: [python]
117113
stages: [manual]
118-
- repo: local
119-
hooks:
120114
- id: flake8-rst
121115
name: flake8-rst
122116
description: Run flake8 on code snippets in docstrings or RST files
@@ -237,3 +231,15 @@ repos:
237231
additional_dependencies:
238232
- flake8==4.0.1
239233
- flake8-pyi==22.5.1
234+
- id: future-annotations
235+
name: import annotations from __future__
236+
entry: 'from __future__ import annotations'
237+
language: pygrep
238+
args: [--negate]
239+
files: ^pandas/
240+
types: [python]
241+
exclude: |
242+
(?x)
243+
/(__init__\.py)|(api\.py)|(_version\.py)|(testing\.py)|(conftest\.py)$
244+
|/tests/
245+
|/_testing/

pandas/_config/dates.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
config for datetime formatting
33
"""
4+
from __future__ import annotations
5+
46
from pandas._config import config as cf
57

68
pc_date_dayfirst_doc = """

pandas/compat/chainmap.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import (
24
ChainMap,
35
TypeVar,

pandas/compat/pyarrow.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
""" support pyarrow compatibility across versions """
22

3+
from __future__ import annotations
4+
35
from pandas.util.version import Version
46

57
try:

pandas/core/_numba/kernels/shared.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import numba
24
import numpy as np
35

pandas/core/array_algos/transforms.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
transforms.py is for shape-preserving functions.
33
"""
44

5+
from __future__ import annotations
6+
57
import numpy as np
68

79

pandas/core/arraylike.py

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Index
55
ExtensionArray
66
"""
7+
from __future__ import annotations
8+
79
import operator
810
from typing import Any
911
import warnings

pandas/core/computation/check.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pandas.compat._optional import import_optional_dependency
24

35
ne = import_optional_dependency("numexpr", errors="warn")

pandas/core/computation/common.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from functools import reduce
24

35
import numpy as np

pandas/core/config_init.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
module is imported, register them here rather than in the module.
1010
1111
"""
12+
from __future__ import annotations
13+
1214
import os
1315
from typing import Callable
1416
import warnings

pandas/core/dtypes/inference.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
""" basic inference routines """
22

3+
from __future__ import annotations
4+
35
from collections import abc
46
from numbers import Number
57
import re

pandas/core/exchange/buffer.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from typing import (
2-
Optional,
3-
Tuple,
4-
)
1+
from __future__ import annotations
52

63
import numpy as np
74
from packaging import version
@@ -60,7 +57,7 @@ def __dlpack__(self):
6057
return self._x.__dlpack__()
6158
raise NotImplementedError("__dlpack__")
6259

63-
def __dlpack_device__(self) -> Tuple[DlpackDeviceType, Optional[int]]:
60+
def __dlpack_device__(self) -> tuple[DlpackDeviceType, int | None]:
6461
"""
6562
Device type and device ID for where the data in the buffer resides.
6663
"""

pandas/core/exchange/dataframe_protocol.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
A verbatim copy (vendored) of the spec from https://github.com/data-apis/dataframe-api
33
"""
44

5+
from __future__ import annotations
6+
57
from abc import (
68
ABC,
79
abstractmethod,
810
)
911
import enum
1012
from typing import (
1113
Any,
12-
Dict,
1314
Iterable,
14-
Optional,
1515
Sequence,
16-
Tuple,
1716
TypedDict,
1817
)
1918

@@ -90,18 +89,18 @@ class ColumnNullType(enum.IntEnum):
9089
class ColumnBuffers(TypedDict):
9190
# first element is a buffer containing the column data;
9291
# second element is the data buffer's associated dtype
93-
data: Tuple["Buffer", Any]
92+
data: tuple[Buffer, Any]
9493

9594
# first element is a buffer containing mask values indicating missing data;
9695
# second element is the mask value buffer's associated dtype.
9796
# None if the null representation is not a bit or byte mask
98-
validity: Optional[Tuple["Buffer", Any]]
97+
validity: tuple[Buffer, Any] | None
9998

10099
# first element is a buffer containing the offset values for
101100
# variable-size binary data (e.g., variable-length strings);
102101
# second element is the offsets buffer's associated dtype.
103102
# None if the data buffer does not have an associated offsets buffer
104-
offsets: Optional[Tuple["Buffer", Any]]
103+
offsets: tuple[Buffer, Any] | None
105104

106105

107106
class CategoricalDescription(TypedDict):
@@ -111,7 +110,7 @@ class CategoricalDescription(TypedDict):
111110
is_dictionary: bool
112111
# Python-level only (e.g. ``{int: str}``).
113112
# None if not a dictionary-style categorical.
114-
mapping: Optional[dict]
113+
mapping: dict | None
115114

116115

117116
class Buffer(ABC):
@@ -161,7 +160,7 @@ def __dlpack__(self):
161160
raise NotImplementedError("__dlpack__")
162161

163162
@abstractmethod
164-
def __dlpack_device__(self) -> Tuple[DlpackDeviceType, Optional[int]]:
163+
def __dlpack_device__(self) -> tuple[DlpackDeviceType, int | None]:
165164
"""
166165
Device type and device ID for where the data in the buffer resides.
167166
Uses device type codes matching DLPack.
@@ -239,7 +238,7 @@ def offset(self) -> int:
239238

240239
@property
241240
@abstractmethod
242-
def dtype(self) -> Tuple[DtypeKind, int, str, str]:
241+
def dtype(self) -> tuple[DtypeKind, int, str, str]:
243242
"""
244243
Dtype description as a tuple ``(kind, bit-width, format string, endianness)``.
245244
@@ -293,7 +292,7 @@ def describe_categorical(self) -> CategoricalDescription:
293292

294293
@property
295294
@abstractmethod
296-
def describe_null(self) -> Tuple[ColumnNullType, Any]:
295+
def describe_null(self) -> tuple[ColumnNullType, Any]:
297296
"""
298297
Return the missing value (or "null") representation the column dtype
299298
uses, as a tuple ``(kind, value)``.
@@ -306,7 +305,7 @@ def describe_null(self) -> Tuple[ColumnNullType, Any]:
306305

307306
@property
308307
@abstractmethod
309-
def null_count(self) -> Optional[int]:
308+
def null_count(self) -> int | None:
310309
"""
311310
Number of null elements, if known.
312311
@@ -316,7 +315,7 @@ def null_count(self) -> Optional[int]:
316315

317316
@property
318317
@abstractmethod
319-
def metadata(self) -> Dict[str, Any]:
318+
def metadata(self) -> dict[str, Any]:
320319
"""
321320
The metadata for the column. See `DataFrame.metadata` for more details.
322321
"""
@@ -330,7 +329,7 @@ def num_chunks(self) -> int:
330329
pass
331330

332331
@abstractmethod
333-
def get_chunks(self, n_chunks: Optional[int] = None) -> Iterable["Column"]:
332+
def get_chunks(self, n_chunks: int | None = None) -> Iterable[Column]:
334333
"""
335334
Return an iterator yielding the chunks.
336335
@@ -395,7 +394,7 @@ def __dataframe__(self, nan_as_null: bool = False, allow_copy: bool = True):
395394

396395
@property
397396
@abstractmethod
398-
def metadata(self) -> Dict[str, Any]:
397+
def metadata(self) -> dict[str, Any]:
399398
"""
400399
The metadata for the data frame, as a dictionary with string keys. The
401400
contents of `metadata` may be anything, they are meant for a library
@@ -415,7 +414,7 @@ def num_columns(self) -> int:
415414
pass
416415

417416
@abstractmethod
418-
def num_rows(self) -> Optional[int]:
417+
def num_rows(self) -> int | None:
419418
# TODO: not happy with Optional, but need to flag it may be expensive
420419
# why include it if it may be None - what do we expect consumers
421420
# to do here?
@@ -460,21 +459,21 @@ def get_columns(self) -> Iterable[Column]:
460459
pass
461460

462461
@abstractmethod
463-
def select_columns(self, indices: Sequence[int]) -> "DataFrame":
462+
def select_columns(self, indices: Sequence[int]) -> DataFrame:
464463
"""
465464
Create a new DataFrame by selecting a subset of columns by index.
466465
"""
467466
pass
468467

469468
@abstractmethod
470-
def select_columns_by_name(self, names: Sequence[str]) -> "DataFrame":
469+
def select_columns_by_name(self, names: Sequence[str]) -> DataFrame:
471470
"""
472471
Create a new DataFrame by selecting a subset of columns by name.
473472
"""
474473
pass
475474

476475
@abstractmethod
477-
def get_chunks(self, n_chunks: Optional[int] = None) -> Iterable["DataFrame"]:
476+
def get_chunks(self, n_chunks: int | None = None) -> Iterable[DataFrame]:
478477
"""
479478
Return an iterator yielding the chunks.
480479

pandas/core/exchange/from_dataframe.py

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1+
from __future__ import annotations
2+
13
import ctypes
24
import re
3-
from typing import (
4-
Any,
5-
Dict,
6-
List,
7-
Optional,
8-
Tuple,
9-
Union,
10-
)
5+
from typing import Any
116

127
import numpy as np
138

@@ -24,7 +19,7 @@
2419
Endianness,
2520
)
2621

27-
_NP_DTYPES: Dict[DtypeKind, Dict[int, Any]] = {
22+
_NP_DTYPES: dict[DtypeKind, dict[int, Any]] = {
2823
DtypeKind.INT: {8: np.int8, 16: np.int16, 32: np.int32, 64: np.int64},
2924
DtypeKind.UINT: {8: np.uint8, 16: np.uint16, 32: np.uint32, 64: np.uint64},
3025
DtypeKind.FLOAT: {32: np.float32, 64: np.float64},
@@ -108,7 +103,7 @@ def protocol_df_chunk_to_pandas(df: DataFrameXchg) -> pd.DataFrame:
108103
"""
109104
# We need a dict of columns here, with each column being a NumPy array (at
110105
# least for now, deal with non-NumPy dtypes later).
111-
columns: Dict[str, Any] = {}
106+
columns: dict[str, Any] = {}
112107
buffers = [] # hold on to buffers, keeps memory alive
113108
for name in df.column_names():
114109
if not isinstance(name, str):
@@ -140,7 +135,7 @@ def protocol_df_chunk_to_pandas(df: DataFrameXchg) -> pd.DataFrame:
140135
return pandas_df
141136

142137

143-
def primitive_column_to_ndarray(col: Column) -> Tuple[np.ndarray, Any]:
138+
def primitive_column_to_ndarray(col: Column) -> tuple[np.ndarray, Any]:
144139
"""
145140
Convert a column holding one of the primitive dtypes to a NumPy array.
146141
@@ -165,7 +160,7 @@ def primitive_column_to_ndarray(col: Column) -> Tuple[np.ndarray, Any]:
165160
return data, buffers
166161

167162

168-
def categorical_column_to_series(col: Column) -> Tuple[pd.Series, Any]:
163+
def categorical_column_to_series(col: Column) -> tuple[pd.Series, Any]:
169164
"""
170165
Convert a column holding categorical data to a pandas Series.
171166
@@ -205,7 +200,7 @@ def categorical_column_to_series(col: Column) -> Tuple[pd.Series, Any]:
205200
return data, buffers
206201

207202

208-
def string_column_to_ndarray(col: Column) -> Tuple[np.ndarray, Any]:
203+
def string_column_to_ndarray(col: Column) -> tuple[np.ndarray, Any]:
209204
"""
210205
Convert a column holding string data to a NumPy array.
211206
@@ -268,7 +263,7 @@ def string_column_to_ndarray(col: Column) -> Tuple[np.ndarray, Any]:
268263
null_pos = ~null_pos
269264

270265
# Assemble the strings from the code units
271-
str_list: List[Union[None, float, str]] = [None] * col.size
266+
str_list: list[None | float | str] = [None] * col.size
272267
for i in range(col.size):
273268
# Check for missing values
274269
if null_pos is not None and null_pos[i]:
@@ -324,7 +319,7 @@ def parse_datetime_format_str(format_str, data):
324319
raise NotImplementedError(f"DateTime kind is not supported: {format_str}")
325320

326321

327-
def datetime_column_to_ndarray(col: Column) -> Tuple[np.ndarray, Any]:
322+
def datetime_column_to_ndarray(col: Column) -> tuple[np.ndarray, Any]:
328323
"""
329324
Convert a column holding DateTime data to a NumPy array.
330325
@@ -362,9 +357,9 @@ def datetime_column_to_ndarray(col: Column) -> Tuple[np.ndarray, Any]:
362357

363358
def buffer_to_ndarray(
364359
buffer: Buffer,
365-
dtype: Tuple[DtypeKind, int, str, str],
360+
dtype: tuple[DtypeKind, int, str, str],
366361
offset: int = 0,
367-
length: Optional[int] = None,
362+
length: int | None = None,
368363
) -> np.ndarray:
369364
"""
370365
Build a NumPy array from the passed buffer.
@@ -470,9 +465,9 @@ def bitmask_to_bool_ndarray(
470465

471466

472467
def set_nulls(
473-
data: Union[np.ndarray, pd.Series],
468+
data: np.ndarray | pd.Series,
474469
col: Column,
475-
validity: Optional[Tuple[Buffer, Tuple[DtypeKind, int, str, str]]],
470+
validity: tuple[Buffer, tuple[DtypeKind, int, str, str]] | None,
476471
allow_modify_inplace: bool = True,
477472
):
478473
"""

pandas/core/exchange/utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Utility functions and objects for implementing the exchange API.
33
"""
44

5+
from __future__ import annotations
6+
57
import re
68
import typing
79

0 commit comments

Comments
 (0)