Skip to content

CI: flake8-pyi #47595

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

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

@twoertwein twoertwein Jul 4, 2022

Choose a reason for hiding this comment

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

flake8 doesn't like black's formatting in pyi files

types: [pyi]
language: python
additional_dependencies:
Copy link
Member

Choose a reason for hiding this comment

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

Could these be added to flake8_dependencies in the flake8 block above and then this line changed to additional_dependencies: *flake8_dependencies?

Copy link
Member Author

Choose a reason for hiding this comment

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

I first tried to add flake8-pyi to all the other flake8 plugins but the issue is that we need different ignores for pyi files than for normal py files.

I think flake8 supports file-specific ignores. If they support something along the lines *.pyi: E301,E302,E305,E701,E704 that would be the best approach. I'll look into that later today.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks. That can be a followup PR if it works.

- flake8==4.0.1
- flake8-pyi==22.5.1
2 changes: 1 addition & 1 deletion pandas/_libs/index.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand Down
25 changes: 9 additions & 16 deletions pandas/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ from typing import (
Any,
Generic,
TypeVar,
Union,
overload,
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/sparse.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/conversion.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ from datetime import (

import numpy as np

from pandas._typing import npt

DT64NS_DTYPE: np.dtype
TD64NS_DTYPE: np.dtype

Expand Down
10 changes: 3 additions & 7 deletions pandas/_libs/tslibs/nattype.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ from datetime import (
timedelta,
tzinfo as _tzinfo,
)
from typing import (
Any,
Union,
)

import numpy as np

Expand All @@ -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: ...
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/offsets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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: ...
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ from datetime import timedelta
from typing import (
ClassVar,
Literal,
Type,
TypeVar,
overload,
)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/writers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
14 changes: 12 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

@twoertwein twoertwein Jul 4, 2022

Choose a reason for hiding this comment

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

Y002: we have one if TYPE_CHECKING, without it mypy cannot infer some types (too many cyclic dependencies or does it hide actual errors?)
Y019: pandas does not (explicitly) depend on typeshed/typing_extension
Y021: some files have notes on how the cython and python types relate to each other
Y026: pandas does not (explicitly) depend on typing_extension
Y027: let's wait until we also do that change for the in-line annotations

exclude =
doc/sphinxext/*.py,
doc/build/*.py,
Expand Down