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

+1
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

+1-1
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

+7-2
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

+1-1
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

+3-1
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

+8-3
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

+6-3
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

+4-2
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

+5-2
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

+6-1
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

pyproject.toml

+126-3
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,9 @@ disallow_any_explicit = false # TODO
538538
disallow_any_generics = false # TODO
539539
disallow_subclassing_any = false # TODO
540540
# Untyped definitions and calls
541-
disallow_untyped_calls = false # TODO
542-
disallow_untyped_defs = false # TODO
543-
disallow_incomplete_defs = false # TODO
541+
disallow_untyped_calls = true
542+
disallow_untyped_defs = true
543+
disallow_incomplete_defs = true
544544
check_untyped_defs = true
545545
disallow_untyped_decorators = true
546546
# None and Optional handling
@@ -566,6 +566,129 @@ show_error_context = false
566566
show_column_numbers = false
567567
show_error_codes = true
568568

569+
[[tool.mypy.overrides]]
570+
module = [
571+
"pandas._config.config", # TODO
572+
"pandas._libs.*",
573+
"pandas._testing.*", # TODO
574+
"pandas.arrays", # TODO
575+
"pandas.compat.numpy.function", # TODO
576+
"pandas.compat._optional", # TODO
577+
"pandas.compat.compressors", # TODO
578+
"pandas.compat.pickle_compat", # TODO
579+
"pandas.core._numba.executor", # TODO
580+
"pandas.core.array_algos.datetimelike_accumulations", # TODO
581+
"pandas.core.array_algos.masked_accumulations", # TODO
582+
"pandas.core.array_algos.masked_reductions", # TODO
583+
"pandas.core.array_algos.putmask", # TODO
584+
"pandas.core.array_algos.quantile", # TODO
585+
"pandas.core.array_algos.replace", # TODO
586+
"pandas.core.array_algos.take", # TODO
587+
"pandas.core.arrays.*", # TODO
588+
"pandas.core.computation.*", # TODO
589+
"pandas.core.dtypes.astype", # TODO
590+
"pandas.core.dtypes.cast", # TODO
591+
"pandas.core.dtypes.common", # TODO
592+
"pandas.core.dtypes.concat", # TODO
593+
"pandas.core.dtypes.dtypes", # TODO
594+
"pandas.core.dtypes.generic", # TODO
595+
"pandas.core.dtypes.inference", # TODO
596+
"pandas.core.dtypes.missing", # TODO
597+
"pandas.core.groupby.categorical", # TODO
598+
"pandas.core.groupby.generic", # TODO
599+
"pandas.core.groupby.grouper", # TODO
600+
"pandas.core.groupby.groupby", # TODO
601+
"pandas.core.groupby.ops", # TODO
602+
"pandas.core.indexers.*", # TODO
603+
"pandas.core.indexes.*", # TODO
604+
"pandas.core.interchange.column", # TODO
605+
"pandas.core.interchange.dataframe_protocol", # TODO
606+
"pandas.core.interchange.from_dataframe", # TODO
607+
"pandas.core.internals.*", # TODO
608+
"pandas.core.methods.*", # TODO
609+
"pandas.core.ops.array_ops", # TODO
610+
"pandas.core.ops.common", # TODO
611+
"pandas.core.ops.invalid", # TODO
612+
"pandas.core.ops.mask_ops", # TODO
613+
"pandas.core.ops.missing", # TODO
614+
"pandas.core.reshape.*", # TODO
615+
"pandas.core.strings.*", # TODO
616+
"pandas.core.tools.*", # TODO
617+
"pandas.core.window.common", # TODO
618+
"pandas.core.window.ewm", # TODO
619+
"pandas.core.window.expanding", # TODO
620+
"pandas.core.window.numba_", # TODO
621+
"pandas.core.window.online", # TODO
622+
"pandas.core.window.rolling", # TODO
623+
"pandas.core.accessor", # TODO
624+
"pandas.core.algorithms", # TODO
625+
"pandas.core.apply", # TODO
626+
"pandas.core.arraylike", # TODO
627+
"pandas.core.base", # TODO
628+
"pandas.core.common", # TODO
629+
"pandas.core.config_init", # TODO
630+
"pandas.core.construction", # TODO
631+
"pandas.core.flags", # TODO
632+
"pandas.core.frame", # TODO
633+
"pandas.core.generic", # TODO
634+
"pandas.core.indexing", # TODO
635+
"pandas.core.missing", # TODO
636+
"pandas.core.nanops", # TODO
637+
"pandas.core.resample", # TODO
638+
"pandas.core.roperator", # TODO
639+
"pandas.core.sample", # TODO
640+
"pandas.core.series", # TODO
641+
"pandas.core.sorting", # TODO
642+
"pandas.errors", # TODO
643+
"pandas.io.clipboard", # TODO
644+
"pandas.io.excel._base", # TODO
645+
"pandas.io.excel._odfreader", # TODO
646+
"pandas.io.excel._odswriter", # TODO
647+
"pandas.io.excel._openpyxl", # TODO
648+
"pandas.io.excel._pyxlsb", # TODO
649+
"pandas.io.excel._xlrd", # TODO
650+
"pandas.io.excel._xlsxwriter", # TODO
651+
"pandas.io.formats.console", # TODO
652+
"pandas.io.formats.css", # TODO
653+
"pandas.io.formats.excel", # TODO
654+
"pandas.io.formats.format", # TODO
655+
"pandas.io.formats.info", # TODO
656+
"pandas.io.formats.printing", # TODO
657+
"pandas.io.formats.style", # TODO
658+
"pandas.io.formats.style_render", # TODO
659+
"pandas.io.formats.xml", # TODO
660+
"pandas.io.json.*", # TODO
661+
"pandas.io.parsers.*", # TODO
662+
"pandas.io.sas.sas_xport", # TODO
663+
"pandas.io.sas.sas7bdat", # TODO
664+
"pandas.io.clipboards", # TODO
665+
"pandas.io.common", # TODO
666+
"pandas.io.gbq", # TODO
667+
"pandas.io.html", # TODO
668+
"pandas.io.gbq", # TODO
669+
"pandas.io.parquet", # TODO
670+
"pandas.io.pytables", # TODO
671+
"pandas.io.sql", # TODO
672+
"pandas.io.stata", # TODO
673+
"pandas.io.xml", # TODO
674+
"pandas.plotting.*", # TODO
675+
"pandas.tests.*",
676+
"pandas.tseries.frequencies", # TODO
677+
"pandas.tseries.holiday", # TODO
678+
"pandas.util._decorators", # TODO
679+
"pandas.util._doctools", # TODO
680+
"pandas.util._print_versions", # TODO
681+
"pandas.util._test_decorators", # TODO
682+
"pandas.util._validators", # TODO
683+
"pandas.util", # TODO
684+
"pandas._version",
685+
"pandas.conftest",
686+
"pandas"
687+
]
688+
disallow_untyped_calls = false
689+
disallow_untyped_defs = false
690+
disallow_incomplete_defs = false
691+
569692
[[tool.mypy.overrides]]
570693
module = [
571694
"pandas.tests.*",

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ tokenize-rt
5858
pre-commit>=2.15.0
5959
gitpython
6060
gitdb
61+
google-auth
6162
natsort
6263
numpydoc
6364
pydata-sphinx-theme

0 commit comments

Comments
 (0)