Skip to content

Commit 7b48899

Browse files
authored
TYP: Enable mypy stricter typing for defs/calls (#54271)
* TYP: Enable disallowing untyped defs * finish rest of core * add typings * Install google auth for typing * type another credentials, list[str]
1 parent 498c438 commit 7b48899

File tree

12 files changed

+169
-19
lines changed

12 files changed

+169
-19
lines changed

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ dependencies:
8383
# documentation
8484
- gitpython # obtain contributors from git for whatsnew
8585
- gitdb
86+
- google-auth
8687
- natsort # DataFrame.sort_values doctest
8788
- numpydoc
8889
- pydata-sphinx-theme

pandas/compat/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from pandas._typing import F
3737

3838

39-
def set_function_name(f: F, name: str, cls) -> F:
39+
def set_function_name(f: F, name: str, cls: type) -> F:
4040
"""
4141
Bind the name/qualname attributes of the function.
4242
"""

pandas/core/array_algos/transforms.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
import numpy as np
1010

1111
if TYPE_CHECKING:
12-
from pandas._typing import AxisInt
12+
from pandas._typing import (
13+
AxisInt,
14+
Scalar,
15+
)
1316

1417

15-
def shift(values: np.ndarray, periods: int, axis: AxisInt, fill_value) -> np.ndarray:
18+
def shift(
19+
values: np.ndarray, periods: int, axis: AxisInt, fill_value: Scalar
20+
) -> np.ndarray:
1621
new_values = values
1722

1823
if periods == 0 or values.size == 0:

pandas/core/dtypes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ class StorageExtensionDtype(ExtensionDtype):
398398
name: str
399399
_metadata = ("storage",)
400400

401-
def __init__(self, storage=None) -> None:
401+
def __init__(self, storage: str | None = None) -> None:
402402
self.storage = storage
403403

404404
def __repr__(self) -> str:

pandas/core/interchange/buffer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import Any
4+
35
import numpy as np
46

57
from pandas.core.interchange.dataframe_protocol import (
@@ -49,7 +51,7 @@ def ptr(self) -> int:
4951
"""
5052
return self._x.__array_interface__["data"][0]
5153

52-
def __dlpack__(self):
54+
def __dlpack__(self) -> Any:
5355
"""
5456
Represent this structure as DLPack interface.
5557
"""

pandas/core/interchange/dataframe.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
from pandas.core.interchange.dataframe_protocol import DataFrame as DataFrameXchg
88

99
if TYPE_CHECKING:
10+
from collections.abc import (
11+
Iterable,
12+
Sequence,
13+
)
14+
1015
from pandas import (
1116
DataFrame,
1217
Index,
@@ -72,7 +77,7 @@ def get_columns(self) -> list[PandasColumn]:
7277
for name in self._df.columns
7378
]
7479

75-
def select_columns(self, indices) -> PandasDataFrameXchg:
80+
def select_columns(self, indices: Sequence[int]) -> PandasDataFrameXchg:
7681
if not isinstance(indices, abc.Sequence):
7782
raise ValueError("`indices` is not a sequence")
7883
if not isinstance(indices, list):
@@ -82,7 +87,7 @@ def select_columns(self, indices) -> PandasDataFrameXchg:
8287
self._df.iloc[:, indices], self._nan_as_null, self._allow_copy
8388
)
8489

85-
def select_columns_by_name(self, names) -> PandasDataFrameXchg:
90+
def select_columns_by_name(self, names: list[str]) -> PandasDataFrameXchg: # type: ignore[override] # noqa: E501
8691
if not isinstance(names, abc.Sequence):
8792
raise ValueError("`names` is not a sequence")
8893
if not isinstance(names, list):
@@ -92,7 +97,7 @@ def select_columns_by_name(self, names) -> PandasDataFrameXchg:
9297
self._df.loc[:, names], self._nan_as_null, self._allow_copy
9398
)
9499

95-
def get_chunks(self, n_chunks: int | None = None):
100+
def get_chunks(self, n_chunks: int | None = None) -> Iterable[PandasDataFrameXchg]:
96101
"""
97102
Return an iterator yielding the chunks.
98103
"""

pandas/io/feather_format.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
""" feather-format compat """
22
from __future__ import annotations
33

4-
from typing import TYPE_CHECKING
4+
from typing import (
5+
TYPE_CHECKING,
6+
Any,
7+
)
58

69
from pandas._libs import lib
710
from pandas.compat._optional import import_optional_dependency
@@ -34,7 +37,7 @@ def to_feather(
3437
df: DataFrame,
3538
path: FilePath | WriteBuffer[bytes],
3639
storage_options: StorageOptions | None = None,
37-
**kwargs,
40+
**kwargs: Any,
3841
) -> None:
3942
"""
4043
Write a DataFrame to the binary Feather format.
@@ -70,7 +73,7 @@ def read_feather(
7073
use_threads: bool = True,
7174
storage_options: StorageOptions | None = None,
7275
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
73-
):
76+
) -> DataFrame:
7477
"""
7578
Load a feather-format object from the file path.
7679

pandas/io/gbq.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from pandas.compat._optional import import_optional_dependency
1010

1111
if TYPE_CHECKING:
12+
import google.auth
13+
1214
from pandas import DataFrame
1315

1416

@@ -33,7 +35,7 @@ def read_gbq(
3335
dialect: str | None = None,
3436
location: str | None = None,
3537
configuration: dict[str, Any] | None = None,
36-
credentials=None,
38+
credentials: google.auth.credentials.Credentials | None = None,
3739
use_bqstorage_api: bool | None = None,
3840
max_results: int | None = None,
3941
progress_bar_type: str | None = None,
@@ -215,7 +217,7 @@ def to_gbq(
215217
table_schema: list[dict[str, str]] | None = None,
216218
location: str | None = None,
217219
progress_bar: bool = True,
218-
credentials=None,
220+
credentials: google.auth.credentials.Credentials | None = None,
219221
) -> None:
220222
pandas_gbq = _try_import()
221223
pandas_gbq.to_gbq(

pandas/io/orc.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
)
3131

3232
if TYPE_CHECKING:
33+
import fsspec
34+
import pyarrow.fs
35+
3336
from pandas._typing import (
3437
DtypeBackend,
3538
FilePath,
@@ -44,8 +47,8 @@ def read_orc(
4447
path: FilePath | ReadBuffer[bytes],
4548
columns: list[str] | None = None,
4649
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
47-
filesystem=None,
48-
**kwargs,
50+
filesystem: pyarrow.fs.FileSystem | fsspec.spec.AbstractFileSystem | None = None,
51+
**kwargs: Any,
4952
) -> DataFrame:
5053
"""
5154
Load an ORC object from the file path, returning a DataFrame.

pandas/io/pickle.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
WriteBuffer,
2525
)
2626

27+
from pandas import (
28+
DataFrame,
29+
Series,
30+
)
31+
2732

2833
@doc(
2934
storage_options=_shared_docs["storage_options"],
@@ -116,7 +121,7 @@ def read_pickle(
116121
filepath_or_buffer: FilePath | ReadPickleBuffer,
117122
compression: CompressionOptions = "infer",
118123
storage_options: StorageOptions | None = None,
119-
):
124+
) -> DataFrame | Series:
120125
"""
121126
Load pickled pandas object (or any object) from file.
122127

0 commit comments

Comments
 (0)