diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 505da9551..cc0e9aaa0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,29 +10,17 @@ repos: rev: 5.12.0 hooks: - id: isort -- repo: https://github.com/asottile/pyupgrade - rev: v3.9.0 +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.0.278 hooks: - - id: pyupgrade - types_or: [python, pyi] - types: [text] # overwrite types: [python] - args: [--py38-plus] -- repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 - hooks: - - id: flake8 - name: flake8 (py) - types: [python] - args: [--ignore=E501 F841] - - id: flake8 - name: flake8 (pyi) - additional_dependencies: - - flake8-pyi==23.6.0 - types: [pyi] + - id: ruff args: [ - --ignore=E301 E302 E305 E402 E501 E701 E704 F401 F811 F821 W503 Y042, - # TypeVars in private files are already private - --per-file-ignores=_*.pyi:Y001 + --exit-non-zero-on-fix, + --target-version, py38, + --extend-select, "PYI,UP,RUF100", + --ignore, "E501,E731,F841,PYI042", + --per-file-ignores, "_*.pyi:PYI001", + --fix ] - repo: https://github.com/codespell-project/codespell rev: v2.2.5 diff --git a/pandas-stubs/_libs/interval.pyi b/pandas-stubs/_libs/interval.pyi index 0dd40c964..0ad353291 100644 --- a/pandas-stubs/_libs/interval.pyi +++ b/pandas-stubs/_libs/interval.pyi @@ -79,7 +79,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]): left: _OrderableT, right: _OrderableT, closed: IntervalClosedType = ..., - ): ... + ) -> None: ... def __hash__(self) -> int: ... # for __contains__, it seems that we have to separate out the 4 cases to make # mypy happy @@ -222,7 +222,7 @@ class IntervalTree(IntervalMixin): right: np.ndarray, closed: IntervalClosedType = ..., leaf_size: int = ..., - ): ... + ) -> None: ... def get_indexer(self, target) -> npt.NDArray[np.intp]: ... def get_indexer_non_unique( self, target diff --git a/pandas-stubs/_libs/tslibs/offsets.pyi b/pandas-stubs/_libs/tslibs/offsets.pyi index 8e29af4e3..2bed61cf2 100644 --- a/pandas-stubs/_libs/tslibs/offsets.pyi +++ b/pandas-stubs/_libs/tslibs/offsets.pyi @@ -123,7 +123,7 @@ class RelativeDeltaOffset(BaseOffset): class BusinessMixin(SingleConstructorOffset): def __init__( self, n: int = ..., normalize: bool = ..., offset: timedelta = ... - ): ... + ) -> None: ... class BusinessDay(BusinessMixin): ... @@ -135,15 +135,17 @@ class BusinessHour(BusinessMixin): start: str | time | Collection[str | time] = ..., end: str | time | Collection[str | time] = ..., offset: timedelta = ..., - ): ... + ) -> None: ... class WeekOfMonthMixin(SingleConstructorOffset): - def __init__(self, n: int = ..., weekday: Literal[0, 1, 2, 3, 4, 5, 6] = ...): ... + def __init__( + self, n: int = ..., weekday: Literal[0, 1, 2, 3, 4, 5, 6] = ... + ) -> None: ... class YearOffset(SingleConstructorOffset): def __init__( self, n: int = ..., normalize: bool = ..., month: int | None = ... - ): ... + ) -> None: ... class BYearEnd(YearOffset): ... class BYearBegin(YearOffset): ... @@ -206,7 +208,7 @@ class _CustomBusinessMonth(BusinessMixin): normalize: bool = ..., offset: timedelta = ..., holidays: list | None = ..., - ): ... + ) -> None: ... class CustomBusinessDay(BusinessDay): def __init__( @@ -215,7 +217,7 @@ class CustomBusinessDay(BusinessDay): normalize: bool = ..., holidays: list = ..., calendar: AbstractHolidayCalendar | np.busdaycalendar = ..., - ): ... + ) -> None: ... class CustomBusinessHour(BusinessHour): def __init__( @@ -226,7 +228,7 @@ class CustomBusinessHour(BusinessHour): end: str | time | Collection[str | time] = ..., offset: timedelta = ..., holidays: list | None = ..., - ): ... + ) -> None: ... class CustomBusinessMonthEnd(_CustomBusinessMonth): ... class CustomBusinessMonthBegin(_CustomBusinessMonth): ... @@ -256,7 +258,7 @@ class DateOffset(RelativeDeltaOffset): second: int = ..., microsecond: int = ..., nanosecond: int = ..., - ): ... + ) -> None: ... BDay = BusinessDay BMonthEnd = BusinessMonthEnd diff --git a/pandas-stubs/_libs/tslibs/period.pyi b/pandas-stubs/_libs/tslibs/period.pyi index 315030954..17352b959 100644 --- a/pandas-stubs/_libs/tslibs/period.pyi +++ b/pandas-stubs/_libs/tslibs/period.pyi @@ -20,13 +20,12 @@ from pandas.core.series import ( from typing_extensions import TypeAlias from pandas._libs.tslibs import NaTType +from pandas._libs.tslibs.offsets import BaseOffset from pandas._libs.tslibs.timestamps import Timestamp from pandas._typing import npt class IncompatibleFrequency(ValueError): ... -from pandas._libs.tslibs.offsets import BaseOffset - _PeriodAddSub: TypeAlias = ( Timedelta | datetime.timedelta | np.timedelta64 | np.int64 | int | BaseOffset ) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index ac311799e..27548758f 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -91,7 +91,7 @@ DtypeBackend: TypeAlias = Literal["pyarrow", "numpy_nullable"] BooleanDtypeArg: TypeAlias = ( # Builtin bool type and its string alias - type[bool] # noqa: Y030 + type[bool] # noqa: PYI030 | Literal["bool"] # Pandas nullable boolean type and its string alias | pd.BooleanDtype @@ -105,7 +105,7 @@ BooleanDtypeArg: TypeAlias = ( ) IntDtypeArg: TypeAlias = ( # Builtin integer type and its string alias - type[int] # noqa: Y030 + type[int] # noqa: PYI030 | Literal["int"] # Pandas nullable integer types and their string aliases | pd.Int8Dtype @@ -137,7 +137,7 @@ IntDtypeArg: TypeAlias = ( ) UIntDtypeArg: TypeAlias = ( # Pandas nullable unsigned integer types and their string aliases - pd.UInt8Dtype # noqa: Y030 + pd.UInt8Dtype # noqa: PYI030 | pd.UInt16Dtype | pd.UInt32Dtype | pd.UInt64Dtype @@ -166,7 +166,7 @@ UIntDtypeArg: TypeAlias = ( ) FloatDtypeArg: TypeAlias = ( # Builtin float type and its string alias - type[float] # noqa: Y030 + type[float] # noqa: PYI030 | Literal["float"] # Pandas nullable float types and their string aliases | pd.Float32Dtype @@ -197,7 +197,7 @@ FloatDtypeArg: TypeAlias = ( ) ComplexDtypeArg: TypeAlias = ( # Builtin complex type and its string alias - type[complex] # noqa: Y030 + type[complex] # noqa: PYI030 | Literal["complex"] # Numpy complex types and their aliases # https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.csingle @@ -326,7 +326,7 @@ TimestampDtypeArg: TypeAlias = Literal[ StrDtypeArg: TypeAlias = ( # Builtin str type and its string alias - type[str] # noqa: Y030 + type[str] # noqa: PYI030 | Literal["str"] # Pandas nullable string type and its string alias | pd.StringDtype @@ -340,7 +340,7 @@ StrDtypeArg: TypeAlias = ( ) BytesDtypeArg: TypeAlias = ( # Builtin bytes type and its string alias - type[bytes] # noqa: Y030 + type[bytes] # noqa: PYI030 | Literal["bytes"] # Numpy bytes type and its string alias # https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.bytes_ @@ -353,7 +353,7 @@ CategoryDtypeArg: TypeAlias = CategoricalDtype | Literal["category"] ObjectDtypeArg: TypeAlias = ( # Builtin object type and its string alias - type[object] # noqa: Y030 + type[object] # noqa: PYI030 | Literal["object"] # Numpy object type and its string alias # https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.object_ diff --git a/pandas-stubs/core/computation/expr.pyi b/pandas-stubs/core/computation/expr.pyi index 0eb8c9af8..d3f31c7d1 100644 --- a/pandas-stubs/core/computation/expr.pyi +++ b/pandas-stubs/core/computation/expr.pyi @@ -50,7 +50,7 @@ class PandasExprVisitor(BaseExprVisitor): def __init__(self, env, engine, parser, preparser=...) -> None: ... class PythonExprVisitor(BaseExprVisitor): - def __init__(self, env, engine, parser, preparser=...): ... + def __init__(self, env, engine, parser, preparser=...) -> None: ... class Expr: env: Scope diff --git a/pandas-stubs/core/computation/pytables.pyi b/pandas-stubs/core/computation/pytables.pyi index 6acb53113..85135c934 100644 --- a/pandas-stubs/core/computation/pytables.pyi +++ b/pandas-stubs/core/computation/pytables.pyi @@ -81,7 +81,7 @@ class UnaryOp(ops.UnaryOp): class PyTablesExprVisitor(BaseExprVisitor): const_type = ... term_type = ... - def __init__(self, env, engine, parser, **kwargs): ... + def __init__(self, env, engine, parser, **kwargs) -> None: ... def visit_UnaryOp(self, node, **kwargs): ... def visit_Index(self, node, **kwargs): ... def visit_Assign(self, node, **kwargs): ... diff --git a/pandas-stubs/core/dtypes/dtypes.pyi b/pandas-stubs/core/dtypes/dtypes.pyi index 48e42f496..7c075d8f5 100644 --- a/pandas-stubs/core/dtypes/dtypes.pyi +++ b/pandas-stubs/core/dtypes/dtypes.pyi @@ -48,13 +48,13 @@ class DatetimeTZDtype(PandasExtensionDtype): def na_value(self) -> NaTType: ... class PeriodDtype(PandasExtensionDtype): - def __init__(self, freq: str | BaseOffset = ...): ... + def __init__(self, freq: str | BaseOffset = ...) -> None: ... @property def freq(self) -> BaseOffset: ... @property def na_value(self) -> NaTType: ... class IntervalDtype(PandasExtensionDtype): - def __init__(self, subtype: str | npt.DTypeLike | None = ...): ... + def __init__(self, subtype: str | npt.DTypeLike | None = ...) -> None: ... @property def subtype(self) -> np.dtype | None: ... diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 906dab8e6..f9c195b18 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -569,7 +569,7 @@ class DataFrame(NDFrame, OpsMixin): def isetitem( self, loc: int | Sequence[int], value: Scalar | ArrayLike | list[Any] ) -> None: ... - def __setitem__(self, key, value): ... + def __setitem__(self, key, value) -> None: ... @overload def query(self, expr: _str, *, inplace: Literal[True], **kwargs) -> None: ... @overload diff --git a/pandas-stubs/core/generic.pyi b/pandas-stubs/core/generic.pyi index 58e080b68..034328e1a 100644 --- a/pandas-stubs/core/generic.pyi +++ b/pandas-stubs/core/generic.pyi @@ -18,6 +18,7 @@ import numpy as np from pandas import Index from pandas.core.base import PandasObject import pandas.core.indexing as indexing +from pandas.core.series import Series import sqlalchemy.engine from pandas._typing import ( @@ -286,7 +287,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin): def take( self, indices, axis=..., is_copy: _bool | None = ..., **kwargs ) -> NDFrame: ... - def __delitem__(self, idx: Hashable): ... + def __delitem__(self, idx: Hashable) -> None: ... def get(self, key: object, default: Dtype | None = ...) -> Dtype: ... def reindex_like( self, @@ -479,5 +480,3 @@ class NDFrame(PandasObject, indexing.IndexingMixin): ) -> NDFrame: ... def first_valid_index(self): ... def last_valid_index(self): ... - -from pandas.core.series import Series diff --git a/pandas-stubs/core/indexers.pyi b/pandas-stubs/core/indexers.pyi index a36a5bc31..98830f534 100644 --- a/pandas-stubs/core/indexers.pyi +++ b/pandas-stubs/core/indexers.pyi @@ -16,7 +16,7 @@ class BaseIndexer: index_array: np.ndarray | None = ..., window_size: int = ..., **kwargs, - ): ... + ) -> None: ... def get_window_bounds( self, num_values: int = ..., @@ -33,7 +33,7 @@ class VariableOffsetWindowIndexer(BaseIndexer): index=..., offset=..., **kwargs, - ): ... + ) -> None: ... def get_window_bounds( self, num_values: int = ..., diff --git a/pandas-stubs/core/indexes/accessors.pyi b/pandas-stubs/core/indexes/accessors.pyi index fc249ca51..699a74a30 100644 --- a/pandas-stubs/core/indexes/accessors.pyi +++ b/pandas-stubs/core/indexes/accessors.pyi @@ -45,14 +45,6 @@ class Properties(PandasDelegate, PandasObject, NoNewAttributesMixin): _DTFieldOpsReturnType = TypeVar("_DTFieldOpsReturnType", Series[int], _IntIndexType) -class _DatetimeFieldOps( - _DayLikeFieldOps[_DTFieldOpsReturnType], _MiniSeconds[_DTFieldOpsReturnType] -): ... -class PeriodIndexFieldOps( - _DayLikeFieldOps[_IntIndexType], - _PeriodProperties[DatetimeIndex, _IntIndexType, Index, DatetimeIndex, PeriodIndex], -): ... - class _DayLikeFieldOps(Generic[_DTFieldOpsReturnType]): @property def year(self) -> _DTFieldOpsReturnType: ... @@ -89,6 +81,10 @@ class _MiniSeconds(Generic[_DTFieldOpsReturnType]): @property def nanosecond(self) -> _DTFieldOpsReturnType: ... +class _DatetimeFieldOps( + _DayLikeFieldOps[_DTFieldOpsReturnType], _MiniSeconds[_DTFieldOpsReturnType] +): ... + _DTBoolOpsReturnType = TypeVar("_DTBoolOpsReturnType", Series[bool], np_ndarray_bool) class _IsLeapYearProperty(Generic[_DTBoolOpsReturnType]): @@ -338,6 +334,10 @@ class _PeriodProperties( how: Literal["E", "END", "FINISH", "S", "START", "BEGIN"] = ..., ) -> _PeriodPAReturnTypes: ... +class PeriodIndexFieldOps( + _DayLikeFieldOps[_IntIndexType], + _PeriodProperties[DatetimeIndex, _IntIndexType, Index, DatetimeIndex, PeriodIndex], +): ... class PeriodProperties( Properties, _PeriodProperties[ diff --git a/pandas-stubs/core/indexes/datetimes.pyi b/pandas-stubs/core/indexes/datetimes.pyi index 615cb098e..baa06efdf 100644 --- a/pandas-stubs/core/indexes/datetimes.pyi +++ b/pandas-stubs/core/indexes/datetimes.pyi @@ -59,7 +59,7 @@ class DatetimeIndex( # type: ignore[misc] dtype=..., copy: bool = ..., name=..., - ): ... + ) -> None: ... def __array__(self, dtype=...) -> np.ndarray: ... def __reduce__(self): ... # various ignores needed for mypy, as we do want to restrict what can be used in diff --git a/pandas-stubs/core/indexes/range.pyi b/pandas-stubs/core/indexes/range.pyi index 79bd4471e..3166fc7d2 100644 --- a/pandas-stubs/core/indexes/range.pyi +++ b/pandas-stubs/core/indexes/range.pyi @@ -33,7 +33,7 @@ class RangeIndex(_IntIndexType): dtype=..., copy: bool = ..., name=..., - ): ... + ) -> None: ... @classmethod def from_range(cls, data, name=..., dtype=...): ... def __reduce__(self): ... diff --git a/pandas-stubs/core/indexes/timedeltas.pyi b/pandas-stubs/core/indexes/timedeltas.pyi index 9a58bdc0d..e4b7fd02b 100644 --- a/pandas-stubs/core/indexes/timedeltas.pyi +++ b/pandas-stubs/core/indexes/timedeltas.pyi @@ -46,7 +46,7 @@ class TimedeltaIndex( # type: ignore[misc] dtype: Literal[" None: ... # various ignores needed for mypy, as we do want to restrict what can be used in # arithmetic for these types @overload diff --git a/pandas-stubs/io/formats/style.pyi b/pandas-stubs/io/formats/style.pyi index 770df865e..95dfcaf6f 100644 --- a/pandas-stubs/io/formats/style.pyi +++ b/pandas-stubs/io/formats/style.pyi @@ -40,7 +40,7 @@ from pandas.io.formats.style_render import ( Subset, ) -class Styler(StylerRenderer[Styler]): +class Styler(StylerRenderer): def __init__( self, data: DataFrame | Series, diff --git a/pandas-stubs/io/formats/style_render.pyi b/pandas-stubs/io/formats/style_render.pyi index dd33670be..762eea0da 100644 --- a/pandas-stubs/io/formats/style_render.pyi +++ b/pandas-stubs/io/formats/style_render.pyi @@ -4,16 +4,17 @@ from collections.abc import ( ) from typing import ( Any, - Generic, Literal, TypedDict, - TypeVar, ) import jinja2 from pandas import Index from pandas.core.indexing import _IndexSlice -from typing_extensions import TypeAlias +from typing_extensions import ( + Self, + TypeAlias, +) from pandas._typing import ( Axis, @@ -44,9 +45,7 @@ class StyleExportDict(TypedDict, total=False): CSSStyles: TypeAlias = list[CSSDict] Subset: TypeAlias = _IndexSlice | slice | tuple[slice, ...] | list[HashableT] | Index -_StylerT = TypeVar("_StylerT", bound=StylerRenderer) - -class StylerRenderer(Generic[_StylerT]): +class StylerRenderer: loader: jinja2.loaders.PackageLoader env: jinja2.environment.Environment template_html: jinja2.environment.Template @@ -63,7 +62,7 @@ class StylerRenderer(Generic[_StylerT]): thousands: str | None = ..., escape: str | None = ..., hyperlinks: Literal["html", "latex"] | None = ..., - ) -> _StylerT: ... + ) -> Self: ... def format_index( self, formatter: ExtFormatter | None = ..., @@ -75,10 +74,10 @@ class StylerRenderer(Generic[_StylerT]): thousands: str | None = ..., escape: str | None = ..., hyperlinks: Literal["html", "latex"] | None = ..., - ) -> _StylerT: ... + ) -> Self: ... def relabel_index( self, labels: Sequence[str] | Index, axis: Axis = ..., level: Level | list[Level] | None = ..., - ) -> _StylerT: ... + ) -> Self: ... diff --git a/tests/extension/decimal/array.py b/tests/extension/decimal/array.py index 8fac43ac5..f29e73e58 100644 --- a/tests/extension/decimal/array.py +++ b/tests/extension/decimal/array.py @@ -200,7 +200,7 @@ def astype(self, dtype, copy=True): return super().astype(dtype, copy=copy) - def __setitem__(self, key, value): + def __setitem__(self, key, value) -> None: if is_list_like(value): if is_scalar(key): raise ValueError("setting an array element with a sequence.") diff --git a/tests/test_series.py b/tests/test_series.py index 055d16c0d..88c9f4854 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -2568,17 +2568,17 @@ def test_all_astype_args_tested() -> None: } TESTED_ASTYPE_ARGS: list[tuple[Any, type]] = ( ASTYPE_BOOL_ARGS - + ASTYPE_INT_ARGS # noqa: W503 - + ASTYPE_UINT_ARGS # noqa: W503 - + ASTYPE_FLOAT_ARGS # noqa: W503 - + ASTYPE_COMPLEX_ARGS # noqa: W503 - + ASTYPE_TIMEDELTA_ARGS # noqa: W503 - + ASTYPE_TIMESTAMP_ARGS # noqa: W503 - + ASTYPE_BYTES_ARGS # noqa: W503 - + ASTYPE_STRING_ARGS # noqa: W503 - + ASTYPE_CATEGORICAL_ARGS # noqa: W503 - + ASTYPE_OBJECT_ARGS # noqa: W503 - + ASTYPE_VOID_ARGS # noqa: W503 + + ASTYPE_INT_ARGS + + ASTYPE_UINT_ARGS + + ASTYPE_FLOAT_ARGS + + ASTYPE_COMPLEX_ARGS + + ASTYPE_TIMEDELTA_ARGS + + ASTYPE_TIMESTAMP_ARGS + + ASTYPE_BYTES_ARGS + + ASTYPE_STRING_ARGS + + ASTYPE_CATEGORICAL_ARGS + + ASTYPE_OBJECT_ARGS + + ASTYPE_VOID_ARGS ) TESTED_ALIASES: set[str] = { diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index e5f2a283c..ed1a0eab8 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -1,4 +1,3 @@ -# flake8: noqa: F841 from __future__ import annotations import datetime as dt @@ -6,7 +5,6 @@ TYPE_CHECKING, Any, Optional, - Union, ) import numpy as np @@ -43,11 +41,9 @@ ) if TYPE_CHECKING: - from pandas.core.series import ( - PeriodSeries, - TimedeltaSeries, - TimestampSeries, - ) + from pandas.core.series import PeriodSeries # noqa: F401 + from pandas.core.series import TimedeltaSeries # noqa: F401 + from pandas.core.series import TimestampSeries # noqa: F401 if TYPE_CHECKING: from pandas.core.indexes.base import _IntIndexType @@ -310,7 +306,7 @@ def test_to_datetime_nat() -> None: check( assert_type( pd.to_datetime("2021-03-01", errors="coerce"), - "Union[pd.Timestamp, NaTType]", + "pd.Timestamp | NaTType", ), pd.Timestamp, ) @@ -318,7 +314,7 @@ def test_to_datetime_nat() -> None: check( assert_type( pd.to_datetime("not a date", errors="coerce"), - "Union[pd.Timestamp, NaTType]", + "pd.Timestamp | NaTType", ), NaTType, )