From 729f53c186065aefaf63782cf56ac1b37d31961a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Tue, 11 Jul 2023 15:12:40 -0400 Subject: [PATCH 1/5] TYP: update mypy and small pyi fixes from ruff --- .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/dtypes.py | 5 ++++- pandas/core/dtypes/missing.py | 3 ++- pandas/core/series.py | 23 ++++++++++++++++++----- requirements-dev.txt | 2 +- typings/numba.pyi | 6 ++---- 11 files changed, 36 insertions(+), 24 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 ed774412d51e7..d1103814a3e44 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..9bb4d8e4f73ad 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 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 diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 04e2b00744156..0dae9e18d6c29 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -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): diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 5536a28157b63..c0178629c4286 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -5,6 +5,7 @@ from decimal import Decimal from functools import partial +from re import Pattern from typing import ( TYPE_CHECKING, overload, @@ -69,7 +70,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 From f104c0f7d683bee77e1e87ee0dde1fd6290a7a9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Tue, 11 Jul 2023 16:43:59 -0400 Subject: [PATCH 2/5] fix errors --- pandas/_typing.py | 2 +- pandas/core/dtypes/dtypes.py | 5 +---- pandas/core/dtypes/missing.py | 3 ++- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 9bb4d8e4f73ad..e35f3c5da739b 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -84,7 +84,7 @@ NumpySorter = Optional[npt._ArrayLikeInt_co] # type: ignore[name-defined] if sys.version_info >= (3, 10): - from typing import TypeGuard as TypeGuard + from typing import TypeGuard else: from typing_extensions import TypeGuard # pyright: ignore[reportUnusedImport] diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 0dae9e18d6c29..04e2b00744156 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -951,10 +951,7 @@ 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 - # 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] + __hash__ = PeriodDtypeBase.__hash__ _freq: BaseOffset def __new__(cls, freq): diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index c0178629c4286..de99f828d604f 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -5,7 +5,6 @@ from decimal import Decimal from functools import partial -from re import Pattern from typing import ( TYPE_CHECKING, overload, @@ -47,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, From 883f05eb52424a0766faab1da4e20ec060e5b078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Tue, 11 Jul 2023 19:41:22 -0400 Subject: [PATCH 3/5] use pyright ignore comment (would prefer 'as TypeGuard') --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index e35f3c5da739b..6a61b37ff4a94 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -84,7 +84,7 @@ 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: ignore[reportUnusedImport] From c0f7b2bf132ed9b63ee3c7d7e92044b46c40ba76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Tue, 11 Jul 2023 20:14:39 -0400 Subject: [PATCH 4/5] disable PLC0414 --- pandas/_typing.py | 8 ++++---- pyproject.toml | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 6a61b37ff4a94..04c9b7d41ead4 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 # pyright: ignore[reportUnusedImport] + from typing import TypeGuard as TypeGuard else: - from typing_extensions import TypeGuard # pyright: ignore[reportUnusedImport] + from typing_extensions import TypeGuard as TypeGuard if sys.version_info >= (3, 11): - from typing import Self + from typing import Self as Self else: - from typing_extensions import Self # pyright: ignore[reportUnusedImport] + from typing_extensions import Self as Self else: npt: Any = None Self: Any = None diff --git a/pyproject.toml b/pyproject.toml index 58671ee80d300..25a3de584e1e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -278,6 +278,8 @@ ignore = [ # "B301", # not yet implemented # Only works with python >=3.10 "B905", + # allow explicit re-exporting imports in py files + "PLC0414", # Too many arguments to function call "PLR0913", # Too many returns From 3c7f0721a38875127040cd37e02b79ad460d67af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 12 Jul 2023 16:42:38 -0400 Subject: [PATCH 5/5] Revert "disable PLC0414" This reverts commit c0f7b2bf132ed9b63ee3c7d7e92044b46c40ba76. --- pandas/_typing.py | 8 ++++---- pyproject.toml | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 04c9b7d41ead4..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 as TypeGuard + from typing import TypeGuard # pyright: ignore[reportUnusedImport] else: - from typing_extensions import TypeGuard as TypeGuard + from typing_extensions import TypeGuard # pyright: ignore[reportUnusedImport] if sys.version_info >= (3, 11): - from typing import Self as Self + from typing import Self else: - from typing_extensions import Self as Self + from typing_extensions import Self # pyright: ignore[reportUnusedImport] else: npt: Any = None Self: Any = None diff --git a/pyproject.toml b/pyproject.toml index 25a3de584e1e3..58671ee80d300 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -278,8 +278,6 @@ ignore = [ # "B301", # not yet implemented # Only works with python >=3.10 "B905", - # allow explicit re-exporting imports in py files - "PLC0414", # Too many arguments to function call "PLR0913", # Too many returns