Skip to content

Commit

Permalink
TYP: update mypy and small pyi fixes from ruff (#54085)
Browse files Browse the repository at this point in the history
* TYP: update mypy and small pyi fixes from ruff

* fix errors

* use pyright ignore comment (would prefer 'as TypeGuard')

* disable PLC0414

* Revert "disable PLC0414"

This reverts commit c0f7b2b.
  • Loading branch information
twoertwein authored Jul 13, 2023
1 parent 7d0b0e2 commit 9ff3322
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 23 deletions.
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
- 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 # pyright: ignore[reportUnusedImport]
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
4 changes: 3 additions & 1 deletion pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
from pandas.core.dtypes.inference import is_list_like

if TYPE_CHECKING:
from re import Pattern

from pandas._typing import (
ArrayLike,
DtypeObj,
Expand All @@ -69,7 +71,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] = ...,
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(
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

0 comments on commit 9ff3322

Please sign in to comment.