Skip to content

TYP: Enable mypy stricter typing for defs/calls #54271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ dependencies:
# documentation
- gitpython # obtain contributors from git for whatsnew
- gitdb
- google-auth
- natsort # DataFrame.sort_values doctest
- numpydoc
- pydata-sphinx-theme
Expand Down
2 changes: 1 addition & 1 deletion pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from pandas._typing import F


def set_function_name(f: F, name: str, cls) -> F:
def set_function_name(f: F, name: str, cls: type) -> F:
"""
Bind the name/qualname attributes of the function.
"""
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/array_algos/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
import numpy as np

if TYPE_CHECKING:
from pandas._typing import AxisInt
from pandas._typing import (
AxisInt,
Scalar,
)


def shift(values: np.ndarray, periods: int, axis: AxisInt, fill_value) -> np.ndarray:
def shift(
values: np.ndarray, periods: int, axis: AxisInt, fill_value: Scalar
) -> np.ndarray:
new_values = values

if periods == 0 or values.size == 0:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class StorageExtensionDtype(ExtensionDtype):
name: str
_metadata = ("storage",)

def __init__(self, storage=None) -> None:
def __init__(self, storage: str | None = None) -> None:
self.storage = storage

def __repr__(self) -> str:
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/interchange/buffer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import Any

import numpy as np

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

def __dlpack__(self):
def __dlpack__(self) -> Any:
"""
Represent this structure as DLPack interface.
"""
Expand Down
11 changes: 8 additions & 3 deletions pandas/core/interchange/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
from pandas.core.interchange.dataframe_protocol import DataFrame as DataFrameXchg

if TYPE_CHECKING:
from collections.abc import (
Iterable,
Sequence,
)

from pandas import (
DataFrame,
Index,
Expand Down Expand Up @@ -72,7 +77,7 @@ def get_columns(self) -> list[PandasColumn]:
for name in self._df.columns
]

def select_columns(self, indices) -> PandasDataFrameXchg:
def select_columns(self, indices: Sequence[int]) -> PandasDataFrameXchg:
if not isinstance(indices, abc.Sequence):
raise ValueError("`indices` is not a sequence")
if not isinstance(indices, list):
Expand All @@ -82,7 +87,7 @@ def select_columns(self, indices) -> PandasDataFrameXchg:
self._df.iloc[:, indices], self._nan_as_null, self._allow_copy
)

def select_columns_by_name(self, names) -> PandasDataFrameXchg:
def select_columns_by_name(self, names: list[str]) -> PandasDataFrameXchg: # type: ignore[override] # noqa: E501
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty interesting that you have to do the override. In some sense, the method DataFrameXchg.select_columns_by_name() is wrong when it says names is Sequence[str], because probably the people who wrote that aren't aware that a pure str will match.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this might be an oversight on the original spec. I opened an issue regarding this data-apis/dataframe-api#212

if not isinstance(names, abc.Sequence):
raise ValueError("`names` is not a sequence")
if not isinstance(names, list):
Expand All @@ -92,7 +97,7 @@ def select_columns_by_name(self, names) -> PandasDataFrameXchg:
self._df.loc[:, names], self._nan_as_null, self._allow_copy
)

def get_chunks(self, n_chunks: int | None = None):
def get_chunks(self, n_chunks: int | None = None) -> Iterable[PandasDataFrameXchg]:
"""
Return an iterator yielding the chunks.
"""
Expand Down
9 changes: 6 additions & 3 deletions pandas/io/feather_format.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
""" feather-format compat """
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import (
TYPE_CHECKING,
Any,
)

from pandas._libs import lib
from pandas.compat._optional import import_optional_dependency
Expand Down Expand Up @@ -34,7 +37,7 @@ def to_feather(
df: DataFrame,
path: FilePath | WriteBuffer[bytes],
storage_options: StorageOptions | None = None,
**kwargs,
**kwargs: Any,
) -> None:
"""
Write a DataFrame to the binary Feather format.
Expand Down Expand Up @@ -70,7 +73,7 @@ def read_feather(
use_threads: bool = True,
storage_options: StorageOptions | None = None,
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
):
) -> DataFrame:
"""
Load a feather-format object from the file path.

Expand Down
6 changes: 4 additions & 2 deletions pandas/io/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from pandas.compat._optional import import_optional_dependency

if TYPE_CHECKING:
import google.auth

from pandas import DataFrame


Expand All @@ -33,7 +35,7 @@ def read_gbq(
dialect: str | None = None,
location: str | None = None,
configuration: dict[str, Any] | None = None,
credentials=None,
credentials: google.auth.credentials.Credentials | None = None,
use_bqstorage_api: bool | None = None,
max_results: int | None = None,
progress_bar_type: str | None = None,
Expand Down Expand Up @@ -215,7 +217,7 @@ def to_gbq(
table_schema: list[dict[str, str]] | None = None,
location: str | None = None,
progress_bar: bool = True,
credentials=None,
credentials: google.auth.credentials.Credentials | None = None,
) -> None:
pandas_gbq = _try_import()
pandas_gbq.to_gbq(
Expand Down
7 changes: 5 additions & 2 deletions pandas/io/orc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
)

if TYPE_CHECKING:
import fsspec
import pyarrow.fs

from pandas._typing import (
DtypeBackend,
FilePath,
Expand All @@ -44,8 +47,8 @@ def read_orc(
path: FilePath | ReadBuffer[bytes],
columns: list[str] | None = None,
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
filesystem=None,
**kwargs,
filesystem: pyarrow.fs.FileSystem | fsspec.spec.AbstractFileSystem | None = None,
**kwargs: Any,
) -> DataFrame:
"""
Load an ORC object from the file path, returning a DataFrame.
Expand Down
7 changes: 6 additions & 1 deletion pandas/io/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
WriteBuffer,
)

from pandas import (
DataFrame,
Series,
)


@doc(
storage_options=_shared_docs["storage_options"],
Expand Down Expand Up @@ -116,7 +121,7 @@ def read_pickle(
filepath_or_buffer: FilePath | ReadPickleBuffer,
compression: CompressionOptions = "infer",
storage_options: StorageOptions | None = None,
):
) -> DataFrame | Series:
"""
Load pickled pandas object (or any object) from file.

Expand Down
129 changes: 126 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,9 @@ disallow_any_explicit = false # TODO
disallow_any_generics = false # TODO
disallow_subclassing_any = false # TODO
# Untyped definitions and calls
disallow_untyped_calls = false # TODO
disallow_untyped_defs = false # TODO
disallow_incomplete_defs = false # TODO
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true
# None and Optional handling
Expand All @@ -566,6 +566,129 @@ show_error_context = false
show_column_numbers = false
show_error_codes = true

[[tool.mypy.overrides]]
module = [
"pandas._config.config", # TODO
"pandas._libs.*",
"pandas._testing.*", # TODO
"pandas.arrays", # TODO
"pandas.compat.numpy.function", # TODO
"pandas.compat._optional", # TODO
"pandas.compat.compressors", # TODO
"pandas.compat.pickle_compat", # TODO
"pandas.core._numba.executor", # TODO
"pandas.core.array_algos.datetimelike_accumulations", # TODO
"pandas.core.array_algos.masked_accumulations", # TODO
"pandas.core.array_algos.masked_reductions", # TODO
"pandas.core.array_algos.putmask", # TODO
"pandas.core.array_algos.quantile", # TODO
"pandas.core.array_algos.replace", # TODO
"pandas.core.array_algos.take", # TODO
"pandas.core.arrays.*", # TODO
"pandas.core.computation.*", # TODO
"pandas.core.dtypes.astype", # TODO
"pandas.core.dtypes.cast", # TODO
"pandas.core.dtypes.common", # TODO
"pandas.core.dtypes.concat", # TODO
"pandas.core.dtypes.dtypes", # TODO
"pandas.core.dtypes.generic", # TODO
"pandas.core.dtypes.inference", # TODO
"pandas.core.dtypes.missing", # TODO
"pandas.core.groupby.categorical", # TODO
"pandas.core.groupby.generic", # TODO
"pandas.core.groupby.grouper", # TODO
"pandas.core.groupby.groupby", # TODO
"pandas.core.groupby.ops", # TODO
"pandas.core.indexers.*", # TODO
"pandas.core.indexes.*", # TODO
"pandas.core.interchange.column", # TODO
"pandas.core.interchange.dataframe_protocol", # TODO
"pandas.core.interchange.from_dataframe", # TODO
"pandas.core.internals.*", # TODO
"pandas.core.methods.*", # TODO
"pandas.core.ops.array_ops", # TODO
"pandas.core.ops.common", # TODO
"pandas.core.ops.invalid", # TODO
"pandas.core.ops.mask_ops", # TODO
"pandas.core.ops.missing", # TODO
"pandas.core.reshape.*", # TODO
"pandas.core.strings.*", # TODO
"pandas.core.tools.*", # TODO
"pandas.core.window.common", # TODO
"pandas.core.window.ewm", # TODO
"pandas.core.window.expanding", # TODO
"pandas.core.window.numba_", # TODO
"pandas.core.window.online", # TODO
"pandas.core.window.rolling", # TODO
"pandas.core.accessor", # TODO
"pandas.core.algorithms", # TODO
"pandas.core.apply", # TODO
"pandas.core.arraylike", # TODO
"pandas.core.base", # TODO
"pandas.core.common", # TODO
"pandas.core.config_init", # TODO
"pandas.core.construction", # TODO
"pandas.core.flags", # TODO
"pandas.core.frame", # TODO
"pandas.core.generic", # TODO
"pandas.core.indexing", # TODO
"pandas.core.missing", # TODO
"pandas.core.nanops", # TODO
"pandas.core.resample", # TODO
"pandas.core.roperator", # TODO
"pandas.core.sample", # TODO
"pandas.core.series", # TODO
"pandas.core.sorting", # TODO
"pandas.errors", # TODO
"pandas.io.clipboard", # TODO
"pandas.io.excel._base", # TODO
"pandas.io.excel._odfreader", # TODO
"pandas.io.excel._odswriter", # TODO
"pandas.io.excel._openpyxl", # TODO
"pandas.io.excel._pyxlsb", # TODO
"pandas.io.excel._xlrd", # TODO
"pandas.io.excel._xlsxwriter", # TODO
"pandas.io.formats.console", # TODO
"pandas.io.formats.css", # TODO
"pandas.io.formats.excel", # TODO
"pandas.io.formats.format", # TODO
"pandas.io.formats.info", # TODO
"pandas.io.formats.printing", # TODO
"pandas.io.formats.style", # TODO
"pandas.io.formats.style_render", # TODO
"pandas.io.formats.xml", # TODO
"pandas.io.json.*", # TODO
"pandas.io.parsers.*", # TODO
"pandas.io.sas.sas_xport", # TODO
"pandas.io.sas.sas7bdat", # TODO
"pandas.io.clipboards", # TODO
"pandas.io.common", # TODO
"pandas.io.gbq", # TODO
"pandas.io.html", # TODO
"pandas.io.gbq", # TODO
"pandas.io.parquet", # TODO
"pandas.io.pytables", # TODO
"pandas.io.sql", # TODO
"pandas.io.stata", # TODO
"pandas.io.xml", # TODO
"pandas.plotting.*", # TODO
"pandas.tests.*",
"pandas.tseries.frequencies", # TODO
"pandas.tseries.holiday", # TODO
"pandas.util._decorators", # TODO
"pandas.util._doctools", # TODO
"pandas.util._print_versions", # TODO
"pandas.util._test_decorators", # TODO
"pandas.util._validators", # TODO
"pandas.util", # TODO
"pandas._version",
"pandas.conftest",
"pandas"
]
disallow_untyped_calls = false
disallow_untyped_defs = false
disallow_incomplete_defs = false

[[tool.mypy.overrides]]
module = [
"pandas.tests.*",
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ tokenize-rt
pre-commit>=2.15.0
gitpython
gitdb
google-auth
natsort
numpydoc
pydata-sphinx-theme
Expand Down