From 9ff33222d29d82c91bf6344f31f2d8ddc299977f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 13 Jul 2023 12:08:57 -0400 Subject: [PATCH] TYP: update mypy and small pyi fixes from ruff (#54085) * 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 c0f7b2bf132ed9b63ee3c7d7e92044b46c40ba76. --- .pre-commit-config.yaml | 4 ++-- doc/source/whatsnew/v2.1.0.rst | 2 +- environment.yml | 2 +- pandas/_libs/lib.pyi | 2 +- pandas/_libs/sas.pyi | 5 +---- pandas/_typing.py | 6 +++--- pandas/core/dtypes/missing.py | 4 +++- pandas/core/series.py | 23 ++++++++++++++++++----- requirements-dev.txt | 2 +- typings/numba.pyi | 6 ++---- 10 files changed, 33 insertions(+), 23 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8997dfe32dcb2..366db4337b0e1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] @@ -130,7 +130,7 @@ repos: types: [python] stages: [manual] additional_dependencies: &pyright_dependencies - - pyright@1.1.292 + - pyright@1.1.296 - id: pyright_reportGeneralTypeIssues # note: assumes python env is setup and activated name: pyright reportGeneralTypeIssues diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 30b2f03dec98c..b377ed4bfad60 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -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 | +-----------------+-----------------+----------+---------+ diff --git a/environment.yml b/environment.yml index 8e3c3a26ffc0f..e85e55e76775b 100644 --- a/environment.yml +++ b/environment.yml @@ -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 diff --git a/pandas/_libs/lib.pyi b/pandas/_libs/lib.pyi index 4a2c7a874238a..ee190ad8db2d9 100644 --- a/pandas/_libs/lib.pyi +++ b/pandas/_libs/lib.pyi @@ -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 diff --git a/pandas/_libs/sas.pyi b/pandas/_libs/sas.pyi index 73068693aa2c6..5d65e2b56b591 100644 --- a/pandas/_libs/sas.pyi +++ b/pandas/_libs/sas.pyi @@ -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: ... diff --git a/pandas/_typing.py b/pandas/_typing.py index ef53c117b7b45..6a61b37ff4a94 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -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 diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 5536a28157b63..de99f828d604f 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -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, @@ -69,7 +71,7 @@ @overload -def isna(obj: Scalar) -> bool: +def isna(obj: Scalar | Pattern) -> bool: ... diff --git a/pandas/core/series.py b/pandas/core/series.py index 265be87be40f1..f17a633259816 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -18,7 +18,6 @@ Any, Callable, Literal, - Union, cast, overload, ) @@ -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 = ..., @@ -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 = ..., @@ -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", @@ -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" diff --git a/requirements-dev.txt b/requirements-dev.txt index 7576b2d49614f..0d00d8b2fb693 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -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 diff --git a/typings/numba.pyi b/typings/numba.pyi index 0d9184af19a0f..36cccb894049b 100644 --- a/typings/numba.pyi +++ b/typings/numba.pyi @@ -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