Skip to content
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

TYP: update mypy and small pyi fixes from ruff #54085

Merged
merged 6 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.275
rev: v0.0.277
hooks:
- id: ruff
args: [--exit-non-zero-on-fix]
Expand Down Expand Up @@ -130,7 +130,7 @@ repos:
types: [python]
stages: [manual]
additional_dependencies: &pyright_dependencies
- [email protected].292
- [email protected].296
- id: pyright_reportGeneralTypeIssues
# note: assumes python env is setup and activated
name: pyright reportGeneralTypeIssues
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ If installed, we now require:
+=================+=================+==========+=========+
| numpy | 1.21.6 | X | X |
+-----------------+-----------------+----------+---------+
| mypy (dev) | 1.2 | | X |
| mypy (dev) | 1.4.1 | | X |
+-----------------+-----------------+----------+---------+
| beautifulsoup4 | 4.11.1 | | X |
+-----------------+-----------------+----------+---------+
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ dependencies:

# code checks
- flake8=6.0.0 # run in subprocess over docstring examples
- mypy=1.2 # pre-commit uses locally installed mypy
- mypy=1.4.1 # pre-commit uses locally installed mypy
mroeschke marked this conversation as resolved.
Show resolved Hide resolved
- tokenize-rt # scripts/check_for_inconsistent_pandas_namespace.py
- pre-commit>=2.15.0

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ from enum import Enum
class _NoDefault(Enum):
no_default = ...

no_default: Final = _NoDefault.no_default # noqa: PYI015
no_default: Final = _NoDefault.no_default
NoDefault = Literal[_NoDefault.no_default]

i8max: int
Expand Down
5 changes: 1 addition & 4 deletions pandas/_libs/sas.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pandas.io.sas.sas7bdat import SAS7BDATReader
from pandas.io.sas.sas7bdat import SAS7BDATReader

class Parser:
def __init__(self, parser: SAS7BDATReader) -> None: ...
Expand Down
6 changes: 3 additions & 3 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@
NumpySorter = Optional[npt._ArrayLikeInt_co] # type: ignore[name-defined]

if sys.version_info >= (3, 10):
from typing import TypeGuard
from typing import TypeGuard as TypeGuard
else:
from typing_extensions import TypeGuard # pyright: reportUnusedImport = false
from typing_extensions import TypeGuard # pyright: ignore[reportUnusedImport]

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self # pyright: reportUnusedImport = false
from typing_extensions import Self # pyright: ignore[reportUnusedImport]
else:
npt: Any = None
Self: Any = None
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,10 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype):
# "Dict[int, PandasExtensionDtype]", base class "PandasExtensionDtype"
# defined the type as "Dict[str, PandasExtensionDtype]") [assignment]
_cache_dtypes: dict[BaseOffset, PeriodDtype] = {} # type: ignore[assignment] # noqa: E501
__hash__ = PeriodDtypeBase.__hash__
# error: Incompatible types in assignment (expression has type "Callable[[
# PeriodDtypeBase], int]", base class "PandasExtensionDtype" defined the type
# as "Callable[[PandasExtensionDtype], int]")
__hash__ = PeriodDtypeBase.__hash__ # type: ignore[assignment]
_freq: BaseOffset

def __new__(cls, freq):
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from decimal import Decimal
from functools import partial
from re import Pattern
from typing import (
TYPE_CHECKING,
overload,
Expand Down Expand Up @@ -69,7 +70,7 @@


@overload
def isna(obj: Scalar) -> bool:
def isna(obj: Scalar | Pattern) -> bool:
...


Expand Down
23 changes: 18 additions & 5 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
Any,
Callable,
Literal,
Union,
cast,
overload,
)
Expand Down Expand Up @@ -3459,7 +3458,7 @@ def sort_values(
self,
*,
axis: Axis = ...,
ascending: bool | int | Sequence[bool] | Sequence[int] = ...,
Copy link
Member Author

Choose a reason for hiding this comment

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

This is undocumented (and incompatible with the declarations in NDFrame) - but it might work at runtime.

ascending: bool | Sequence[bool] = ...,
inplace: Literal[False] = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
Expand All @@ -3473,7 +3472,7 @@ def sort_values(
self,
*,
axis: Axis = ...,
ascending: bool | int | Sequence[bool] | Sequence[int] = ...,
ascending: bool | Sequence[bool] = ...,
inplace: Literal[True],
kind: SortKind = ...,
na_position: NaPosition = ...,
Expand All @@ -3482,11 +3481,25 @@ def sort_values(
) -> None:
...

@overload
def sort_values(
Copy link
Member Author

Choose a reason for hiding this comment

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

NDFrame declared this fallback overload, mypy seems to be stricter now and requires it also in the subclasses.

self,
*,
axis: Axis = ...,
ascending: bool | Sequence[bool] = ...,
inplace: bool = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
ignore_index: bool = ...,
key: ValueKeyFunc = ...,
) -> Series | None:
...

def sort_values(
self,
*,
axis: Axis = 0,
ascending: bool | int | Sequence[bool] | Sequence[int] = True,
ascending: bool | Sequence[bool] = True,
inplace: bool = False,
kind: SortKind = "quicksort",
na_position: NaPosition = "last",
Expand Down Expand Up @@ -3647,7 +3660,7 @@ def sort_values(
)

if is_list_like(ascending):
ascending = cast(Sequence[Union[bool, int]], ascending)
ascending = cast(Sequence[bool], ascending)
if len(ascending) != 1:
raise ValueError(
f"Length of ascending ({len(ascending)}) must be 1 for Series"
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ moto
flask
asv>=0.5.1
flake8==6.0.0
mypy==1.2
mypy==1.4.1
tokenize-rt
pre-commit>=2.15.0
gitpython
Expand Down
6 changes: 2 additions & 4 deletions typings/numba.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# pyright: reportIncompleteStub = false
from typing import (
TYPE_CHECKING,
Any,
Callable,
Literal,
overload,
)

if TYPE_CHECKING:
import numba
import numba

from pandas._typing import F
from pandas._typing import F

def __getattr__(name: str) -> Any: ... # incomplete
@overload
Expand Down