diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 13e0ecd33359f..8d1e13c1e2212 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -229,3 +229,11 @@ repos: entry: python scripts/validate_min_versions_in_sync.py language: python files: ^(ci/deps/actions-.*-minimum_versions\.yaml|pandas/compat/_optional\.py)$ + - id: flake8-pyi + name: flake8-pyi + entry: flake8 --extend-ignore=E301,E302,E305,E701,E704 + types: [pyi] + language: python + additional_dependencies: + - flake8==4.0.1 + - flake8-pyi==22.5.1 diff --git a/pandas/_libs/index.pyi b/pandas/_libs/index.pyi index 68ecf201285c7..575f83847b1b6 100644 --- a/pandas/_libs/index.pyi +++ b/pandas/_libs/index.pyi @@ -69,7 +69,7 @@ class BaseMultiIndexCodesEngine: ) -> npt.NDArray[np.intp]: ... class ExtensionEngine: - def __init__(self, values: "ExtensionArray"): ... + def __init__(self, values: ExtensionArray): ... def __contains__(self, val: object) -> bool: ... def get_loc(self, val: object) -> int | slice | np.ndarray: ... def get_indexer(self, values: np.ndarray) -> npt.NDArray[np.intp]: ... diff --git a/pandas/_libs/interval.pyi b/pandas/_libs/interval.pyi index d177e597478d9..a40a6a1a3e323 100644 --- a/pandas/_libs/interval.pyi +++ b/pandas/_libs/interval.pyi @@ -4,7 +4,6 @@ from typing import ( Any, Generic, TypeVar, - Union, overload, ) @@ -81,11 +80,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]): self: Interval[_OrderableTimesT], key: _OrderableTimesT ) -> bool: ... @overload - def __contains__( - self: Interval[_OrderableScalarT], key: Union[int, float] - ) -> bool: ... - def __repr__(self) -> str: ... - def __str__(self) -> str: ... + def __contains__(self: Interval[_OrderableScalarT], key: int | float) -> bool: ... @overload def __add__( self: Interval[_OrderableTimesT], y: Timedelta @@ -95,7 +90,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]): self: Interval[int], y: _OrderableScalarT ) -> Interval[_OrderableScalarT]: ... @overload - def __add__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ... + def __add__(self: Interval[float], y: int | float) -> Interval[float]: ... @overload def __radd__( self: Interval[_OrderableTimesT], y: Timedelta @@ -105,7 +100,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]): self: Interval[int], y: _OrderableScalarT ) -> Interval[_OrderableScalarT]: ... @overload - def __radd__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ... + def __radd__(self: Interval[float], y: int | float) -> Interval[float]: ... @overload def __sub__( self: Interval[_OrderableTimesT], y: Timedelta @@ -115,7 +110,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]): self: Interval[int], y: _OrderableScalarT ) -> Interval[_OrderableScalarT]: ... @overload - def __sub__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ... + def __sub__(self: Interval[float], y: int | float) -> Interval[float]: ... @overload def __rsub__( self: Interval[_OrderableTimesT], y: Timedelta @@ -125,33 +120,31 @@ class Interval(IntervalMixin, Generic[_OrderableT]): self: Interval[int], y: _OrderableScalarT ) -> Interval[_OrderableScalarT]: ... @overload - def __rsub__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ... + def __rsub__(self: Interval[float], y: int | float) -> Interval[float]: ... @overload def __mul__( self: Interval[int], y: _OrderableScalarT ) -> Interval[_OrderableScalarT]: ... @overload - def __mul__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ... + def __mul__(self: Interval[float], y: int | float) -> Interval[float]: ... @overload def __rmul__( self: Interval[int], y: _OrderableScalarT ) -> Interval[_OrderableScalarT]: ... @overload - def __rmul__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ... + def __rmul__(self: Interval[float], y: int | float) -> Interval[float]: ... @overload def __truediv__( self: Interval[int], y: _OrderableScalarT ) -> Interval[_OrderableScalarT]: ... @overload - def __truediv__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ... + def __truediv__(self: Interval[float], y: int | float) -> Interval[float]: ... @overload def __floordiv__( self: Interval[int], y: _OrderableScalarT ) -> Interval[_OrderableScalarT]: ... @overload - def __floordiv__( - self: Interval[float], y: Union[int, float] - ) -> Interval[float]: ... + def __floordiv__(self: Interval[float], y: int | float) -> Interval[float]: ... def overlaps(self: Interval[_OrderableT], other: Interval[_OrderableT]) -> bool: ... def intervals_to_interval_bounds( diff --git a/pandas/_libs/lib.pyi b/pandas/_libs/lib.pyi index 02f021128cbed..77d3cbe92bef9 100644 --- a/pandas/_libs/lib.pyi +++ b/pandas/_libs/lib.pyi @@ -213,7 +213,7 @@ def count_level_2d( def get_level_sorter( label: np.ndarray, # const int64_t[:] starts: np.ndarray, # const intp_t[:] -) -> np.ndarray: ... # np.ndarray[np.intp, ndim=1] +) -> np.ndarray: ... # np.ndarray[np.intp, ndim=1] def generate_bins_dt64( values: npt.NDArray[np.int64], binner: np.ndarray, # const int64_t[:] diff --git a/pandas/_libs/sparse.pyi b/pandas/_libs/sparse.pyi index aa5388025f6f2..be5d251b2aea6 100644 --- a/pandas/_libs/sparse.pyi +++ b/pandas/_libs/sparse.pyi @@ -7,7 +7,7 @@ import numpy as np from pandas._typing import npt -SparseIndexT = TypeVar("SparseIndexT", bound="SparseIndex") +_SparseIndexT = TypeVar("_SparseIndexT", bound=SparseIndex) class SparseIndex: length: int @@ -24,8 +24,8 @@ class SparseIndex: def lookup_array(self, indexer: npt.NDArray[np.int32]) -> npt.NDArray[np.int32]: ... def to_int_index(self) -> IntIndex: ... def to_block_index(self) -> BlockIndex: ... - def intersect(self: SparseIndexT, y_: SparseIndex) -> SparseIndexT: ... - def make_union(self: SparseIndexT, y_: SparseIndex) -> SparseIndexT: ... + def intersect(self: _SparseIndexT, y_: SparseIndex) -> _SparseIndexT: ... + def make_union(self: _SparseIndexT, y_: SparseIndex) -> _SparseIndexT: ... class IntIndex(SparseIndex): indices: npt.NDArray[np.int32] diff --git a/pandas/_libs/tslibs/conversion.pyi b/pandas/_libs/tslibs/conversion.pyi index 7e2ebb1b3bad3..d564d767f7f05 100644 --- a/pandas/_libs/tslibs/conversion.pyi +++ b/pandas/_libs/tslibs/conversion.pyi @@ -5,8 +5,6 @@ from datetime import ( import numpy as np -from pandas._typing import npt - DT64NS_DTYPE: np.dtype TD64NS_DTYPE: np.dtype diff --git a/pandas/_libs/tslibs/nattype.pyi b/pandas/_libs/tslibs/nattype.pyi index efadd8f0220b3..e5a7e0223e534 100644 --- a/pandas/_libs/tslibs/nattype.pyi +++ b/pandas/_libs/tslibs/nattype.pyi @@ -3,10 +3,6 @@ from datetime import ( timedelta, tzinfo as _tzinfo, ) -from typing import ( - Any, - Union, -) import numpy as np @@ -18,7 +14,7 @@ nat_strings: set[str] def is_null_datetimelike(val: object, inat_is_null: bool = ...) -> bool: ... -_NaTComparisonTypes = Union[datetime, timedelta, Period, np.datetime64, np.timedelta64] +_NaTComparisonTypes = datetime | timedelta | Period | np.datetime64 | np.timedelta64 class _NatComparison: def __call__(self, other: _NaTComparisonTypes) -> bool: ... @@ -117,8 +113,8 @@ class NaTType: # inject Period properties @property def qyear(self) -> float: ... - def __eq__(self, other: Any) -> bool: ... - def __ne__(self, other: Any) -> bool: ... + def __eq__(self, other: object) -> bool: ... + def __ne__(self, other: object) -> bool: ... __lt__: _NatComparison __le__: _NatComparison __gt__: _NatComparison diff --git a/pandas/_libs/tslibs/offsets.pyi b/pandas/_libs/tslibs/offsets.pyi index 058bbcdd346e0..12b113f0b73b1 100644 --- a/pandas/_libs/tslibs/offsets.pyi +++ b/pandas/_libs/tslibs/offsets.pyi @@ -21,7 +21,7 @@ from .timedeltas import Timedelta if TYPE_CHECKING: from pandas.core.indexes.datetimes import DatetimeIndex -_BaseOffsetT = TypeVar("_BaseOffsetT", bound="BaseOffset") +_BaseOffsetT = TypeVar("_BaseOffsetT", bound=BaseOffset) _DatetimeT = TypeVar("_DatetimeT", bound=datetime) _TimedeltaT = TypeVar("_TimedeltaT", bound=timedelta) @@ -76,13 +76,12 @@ class BaseOffset: def __rmul__(self: _BaseOffsetT, other: int) -> _BaseOffsetT: ... def __neg__(self: _BaseOffsetT) -> _BaseOffsetT: ... def copy(self: _BaseOffsetT) -> _BaseOffsetT: ... - def __repr__(self) -> str: ... @property def name(self) -> str: ... @property def rule_code(self) -> str: ... def freqstr(self) -> str: ... - def apply_index(self, dtindex: "DatetimeIndex") -> "DatetimeIndex": ... + def apply_index(self, dtindex: DatetimeIndex) -> DatetimeIndex: ... def _apply_array(self, dtarr) -> None: ... def rollback(self, dt: datetime) -> datetime: ... def rollforward(self, dt: datetime) -> datetime: ... diff --git a/pandas/_libs/tslibs/timedeltas.pyi b/pandas/_libs/tslibs/timedeltas.pyi index cc649e5a62660..1921329da9e24 100644 --- a/pandas/_libs/tslibs/timedeltas.pyi +++ b/pandas/_libs/tslibs/timedeltas.pyi @@ -2,7 +2,6 @@ from datetime import timedelta from typing import ( ClassVar, Literal, - Type, TypeVar, overload, ) @@ -84,7 +83,7 @@ class Timedelta(timedelta): resolution: ClassVar[Timedelta] value: int # np.int64 def __new__( - cls: Type[_S], + cls: type[_S], value=..., unit: str = ..., **kwargs: int | float | np.integer | np.floating, diff --git a/pandas/_libs/tslibs/timestamps.pyi b/pandas/_libs/tslibs/timestamps.pyi index 4de51d4dc7dd8..f6a62688fc72d 100644 --- a/pandas/_libs/tslibs/timestamps.pyi +++ b/pandas/_libs/tslibs/timestamps.pyi @@ -104,7 +104,9 @@ class Timestamp(datetime): def utcnow(cls: type[_DatetimeT]) -> _DatetimeT: ... # error: Signature of "combine" incompatible with supertype "datetime" @classmethod - def combine(cls, date: _date, time: _time) -> datetime: ... # type: ignore[override] + def combine( # type: ignore[override] + cls, date: _date, time: _time + ) -> datetime: ... @classmethod def fromisoformat(cls: type[_DatetimeT], date_string: str) -> _DatetimeT: ... def strftime(self, format: str) -> str: ... diff --git a/pandas/_libs/writers.pyi b/pandas/_libs/writers.pyi index 930322fcbeb77..0d2096eee3573 100644 --- a/pandas/_libs/writers.pyi +++ b/pandas/_libs/writers.pyi @@ -17,7 +17,7 @@ def max_len_string_array( ) -> int: ... def word_len(val: object) -> int: ... def string_array_replace_from_nan_rep( - arr: np.ndarray, # np.ndarray[object, ndim=1] + arr: np.ndarray, # np.ndarray[object, ndim=1] nan_rep: object, replace: object = ..., ) -> None: ... diff --git a/setup.cfg b/setup.cfg index ded05cbb4e413..b191930acf4c5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -107,9 +107,19 @@ ignore = # false positives B301, # single-letter variables - PDF023 + PDF023, # "use 'pandas._testing' instead" in non-test code - PDF025 + PDF025, + # If test must be a simple comparison against sys.platform or sys.version_info + Y002, + # Use "_typeshed.Self" instead of class-bound TypeVar + Y019, + # Docstrings should not be included in stubs + Y021, + # Use typing_extensions.TypeAlias for type aliases + Y026, + # Use "collections.abc.*" instead of "typing.*" (PEP 585 syntax) + Y027 exclude = doc/sphinxext/*.py, doc/build/*.py,