diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e08722d4d..1f45d60ed 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,14 +2,6 @@ minimum_pre_commit_version: 2.15.0 ci: autofix_prs: false repos: -- repo: https://github.com/psf/black - rev: 25.9.0 - hooks: - - id: black -- repo: https://github.com/PyCQA/isort - rev: 6.0.1 - hooks: - - id: isort - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.13.3 hooks: @@ -21,3 +13,11 @@ repos: - id: codespell additional_dependencies: [ tomli ] args: [-L, "THIRDPARTY"] +- repo: https://github.com/PyCQA/isort + rev: 6.0.1 + hooks: + - id: isort +- repo: https://github.com/psf/black + rev: 25.9.0 + hooks: + - id: black diff --git a/conftest.py b/conftest.py index 9dc46f43b..ce5187a65 100644 --- a/conftest.py +++ b/conftest.py @@ -1,10 +1,11 @@ +from collections.abc import Generator import gc import pytest @pytest.fixture -def mpl_cleanup(): +def mpl_cleanup() -> Generator[None, None, None]: """ Ensure Matplotlib is cleaned up around a test. diff --git a/pandas-stubs/_libs/tslibs/__init__.pyi b/pandas-stubs/_libs/tslibs/__init__.pyi index e760e5285..a090f12cd 100644 --- a/pandas-stubs/_libs/tslibs/__init__.pyi +++ b/pandas-stubs/_libs/tslibs/__init__.pyi @@ -1,14 +1,14 @@ __all__ = [ - "Period", - "Timestamp", - "Timedelta", + "BaseOffset", "NaT", "NaTType", + "OutOfBoundsDatetime", + "Period", + "Tick", + "Timedelta", + "Timestamp", "iNaT", "nat_strings", - "BaseOffset", - "Tick", - "OutOfBoundsDatetime", ] from pandas._libs.tslibs.nattype import ( NaT, diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 887d2a240..0d9bc9627 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -1080,6 +1080,7 @@ Incomplete: TypeAlias = Any class Just(Protocol, Generic[T]): @property # type: ignore[override] @override + # pyrefly: ignore # bad-override def __class__(self, /) -> type[T]: ... @__class__.setter @override diff --git a/pandas-stubs/core/groupby/__init__.pyi b/pandas-stubs/core/groupby/__init__.pyi index 27ed3b004..73131e337 100644 --- a/pandas-stubs/core/groupby/__init__.pyi +++ b/pandas-stubs/core/groupby/__init__.pyi @@ -8,8 +8,8 @@ from pandas.core.groupby.grouper import Grouper as Grouper __all__ = [ "DataFrameGroupBy", - "NamedAgg", - "SeriesGroupBy", "GroupBy", "Grouper", + "NamedAgg", + "SeriesGroupBy", ] diff --git a/pandas-stubs/core/groupby/generic.pyi b/pandas-stubs/core/groupby/generic.pyi index 40670c59d..76e543ae8 100644 --- a/pandas-stubs/core/groupby/generic.pyi +++ b/pandas-stubs/core/groupby/generic.pyi @@ -204,6 +204,7 @@ class SeriesGroupBy(GroupBy[Series[S2]], Generic[S2, ByT]): def unique(self) -> Series: ... # Overrides that provide more precise return types over the GroupBy class @final # type: ignore[misc] + # pyrefly: ignore # bad-override def __iter__( # pyright: ignore[reportIncompatibleMethodOverride] self, ) -> Iterator[tuple[ByT, Series[S2]]]: ... @@ -451,6 +452,7 @@ class DataFrameGroupBy(GroupBy[DataFrame], Generic[ByT, _TT]): def __getattr__(self, name: str) -> SeriesGroupBy[Any, ByT]: ... # Overrides that provide more precise return types over the GroupBy class @final # type: ignore[misc] + # pyrefly: ignore # bad-override def __iter__( # pyright: ignore[reportIncompatibleMethodOverride] self, ) -> Iterator[tuple[ByT, DataFrame]]: ... diff --git a/pandas-stubs/core/indexes/datetimes.pyi b/pandas-stubs/core/indexes/datetimes.pyi index 16e1a8c22..94710415a 100644 --- a/pandas-stubs/core/indexes/datetimes.pyi +++ b/pandas-stubs/core/indexes/datetimes.pyi @@ -68,6 +68,7 @@ class DatetimeIndex( self, other: timedelta | BaseOffset ) -> Self: ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __sub__( self, other: datetime | np.datetime64 | np_ndarray_dt | Self ) -> TimedeltaIndex: ... diff --git a/pandas-stubs/core/indexes/interval.pyi b/pandas-stubs/core/indexes/interval.pyi index 43c3595cd..d53d5bba2 100644 --- a/pandas-stubs/core/indexes/interval.pyi +++ b/pandas-stubs/core/indexes/interval.pyi @@ -239,6 +239,7 @@ class IntervalIndex(ExtensionIndex[IntervalT, np.object_], IntervalMixin): @property def length(self) -> Index: ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __getitem__( self, idx: ( diff --git a/pandas-stubs/core/indexes/multi.pyi b/pandas-stubs/core/indexes/multi.pyi index b64cd1f2d..e66c845ec 100644 --- a/pandas-stubs/core/indexes/multi.pyi +++ b/pandas-stubs/core/indexes/multi.pyi @@ -123,6 +123,7 @@ class MultiIndex(Index): def levshape(self): ... def __reduce__(self): ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __getitem__( self, idx: slice | np_ndarray_anyint | Sequence[int] | Index | MaskType, diff --git a/pandas-stubs/core/indexes/period.pyi b/pandas-stubs/core/indexes/period.pyi index 158f38698..84abc62ea 100644 --- a/pandas-stubs/core/indexes/period.pyi +++ b/pandas-stubs/core/indexes/period.pyi @@ -43,6 +43,7 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexField self, other: datetime.timedelta ) -> Self: ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __sub__(self, other: Period) -> Index: ... @overload def __sub__(self, other: Self) -> Index: ... @@ -55,6 +56,7 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexField self, other: TimedeltaIndex | pd.Timedelta ) -> Self: ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __rsub__(self, other: Period) -> Index: ... @overload def __rsub__(self, other: Self) -> Index: ... diff --git a/pandas-stubs/core/indexes/range.pyi b/pandas-stubs/core/indexes/range.pyi index 5e4055fb8..5f5e13013 100644 --- a/pandas-stubs/core/indexes/range.pyi +++ b/pandas-stubs/core/indexes/range.pyi @@ -85,6 +85,7 @@ class RangeIndex(_IndexSubclassBase[int, np.int64]): self, other: list[HashableT] | Index, sort: bool | None = None ) -> Index | Index[int] | RangeIndex: ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __getitem__( self, idx: slice | np_ndarray_anyint | Sequence[int] | Index | MaskType, diff --git a/pandas-stubs/core/indexes/timedeltas.pyi b/pandas-stubs/core/indexes/timedeltas.pyi index 5b0915c6b..de2bee2bd 100644 --- a/pandas-stubs/core/indexes/timedeltas.pyi +++ b/pandas-stubs/core/indexes/timedeltas.pyi @@ -56,6 +56,7 @@ class TimedeltaIndex( # various ignores needed for mypy, as we do want to restrict what can be used in # arithmetic for these types @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __add__(self, other: Period) -> PeriodIndex: ... @overload def __add__(self, other: dt.datetime | DatetimeIndex) -> DatetimeIndex: ... @@ -64,6 +65,7 @@ class TimedeltaIndex( self, other: dt.timedelta | Self ) -> Self: ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __radd__(self, other: Period) -> PeriodIndex: ... @overload def __radd__(self, other: dt.datetime | DatetimeIndex) -> DatetimeIndex: ... @@ -75,6 +77,7 @@ class TimedeltaIndex( self, other: dt.timedelta | np.timedelta64 | np_ndarray_td | BaseOffset | Self ) -> Self: ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __rsub__( self, other: dt.timedelta | np.timedelta64 | np_ndarray_td | BaseOffset | Self ) -> Self: ... @@ -115,6 +118,7 @@ class TimedeltaIndex( ), ) -> Self: ... @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __truediv__(self, other: float | Sequence[float]) -> Self: ... @overload def __truediv__( # pyright: ignore[reportIncompatibleMethodOverride] @@ -122,6 +126,7 @@ class TimedeltaIndex( ) -> Index[float]: ... def __rtruediv__(self, other: dt.timedelta | Sequence[dt.timedelta]) -> Index[float]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __floordiv__(self, other: num | Sequence[float]) -> Self: ... @overload def __floordiv__( # pyright: ignore[reportIncompatibleMethodOverride] diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index a0513eaf4..9b41a239c 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -2208,6 +2208,7 @@ class Series(IndexOpsMixin[S1], NDFrame): ) -> Series[_str]: ... # ignore needed for mypy as we want different results based on the arguments @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __and__( # pyright: ignore[reportOverlappingOverload] self, other: bool | list[int] | MaskType ) -> Series[bool]: ... @@ -3060,6 +3061,7 @@ class Series(IndexOpsMixin[S1], NDFrame): def __pow__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ... # ignore needed for mypy as we want different results based on the arguments @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __or__( # pyright: ignore[reportOverlappingOverload] self, other: bool | list[int] | MaskType ) -> Series[bool]: ... @@ -3067,6 +3069,7 @@ class Series(IndexOpsMixin[S1], NDFrame): def __or__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # ignore needed for mypy as we want different results based on the arguments @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __rand__( # pyright: ignore[reportOverlappingOverload] self, other: bool | MaskType | list[int] ) -> Series[bool]: ... @@ -3077,6 +3080,7 @@ class Series(IndexOpsMixin[S1], NDFrame): def __rpow__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ... # ignore needed for mypy as we want different results based on the arguments @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __ror__( # pyright: ignore[reportOverlappingOverload] self, other: bool | MaskType | list[int] ) -> Series[bool]: ... @@ -3084,6 +3088,7 @@ class Series(IndexOpsMixin[S1], NDFrame): def __ror__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # ignore needed for mypy as we want different results based on the arguments @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __rxor__( # pyright: ignore[reportOverlappingOverload] self, other: bool | MaskType | list[int] ) -> Series[bool]: ... @@ -4244,6 +4249,7 @@ class Series(IndexOpsMixin[S1], NDFrame): rdiv = rtruediv # ignore needed for mypy as we want different results based on the arguments @overload # type: ignore[override] + # pyrefly: ignore # bad-override def __xor__( # pyright: ignore[reportOverlappingOverload] self, other: bool | MaskType | list[int] ) -> Series[bool]: ... diff --git a/pandas-stubs/io/orc.pyi b/pandas-stubs/io/orc.pyi index 7039598e7..489ef433e 100644 --- a/pandas-stubs/io/orc.pyi +++ b/pandas-stubs/io/orc.pyi @@ -14,7 +14,7 @@ def read_orc( path: FilePath | ReadBuffer[bytes], columns: list[HashableT] | None = None, dtype_backend: DtypeBackend | _NoDefaultDoNotUse = "numpy_nullable", - # TODO type with the correct pyarrow types + # TODO: type with the correct pyarrow types # filesystem: pyarrow.fs.FileSystem | fsspec.spec.AbstractFileSystem filesystem: Any | None = None, **kwargs: Any, diff --git a/pandas-stubs/testing.pyi b/pandas-stubs/testing.pyi index 312d8629d..c0a2b17b8 100644 --- a/pandas-stubs/testing.pyi +++ b/pandas-stubs/testing.pyi @@ -8,6 +8,6 @@ from pandas._testing import ( __all__ = [ "assert_extension_array_equal", "assert_frame_equal", - "assert_series_equal", "assert_index_equal", + "assert_series_equal", ] diff --git a/pyproject.toml b/pyproject.toml index 48d2c42f0..78270bfa0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,13 +36,13 @@ types-pytz = ">= 2022.1.1" numpy = ">= 1.23.5" [tool.poetry.group.dev.dependencies] -mypy = "1.18.2" +mypy = ">=1.18.2" pandas = "2.3.2" pyarrow = ">=10.0.1" pytest = ">=8.4.2" pyright = ">=1.1.406" ty = ">=0.0.1a21" -pyrefly = ">=0.35.0" +pyrefly = ">=0.36.1" poethepoet = ">=0.16.5" loguru = ">=0.6.0" typing-extensions = ">=4.4.0" @@ -176,16 +176,59 @@ fix = true [tool.ruff.lint] -extend-select = ["B007", "B018", "PYI", "UP", "RUF100", "RUF059"] +extend-select = ["ALL"] ignore = [ - "E501", # https://docs.astral.sh/ruff/rules/line-too-long/ - "E731", # https://docs.astral.sh/ruff/rules/lambda-assignment/ - "PYI042", # https://docs.astral.sh/ruff/rules/snake-case-type-alias/ + # The following rules are ignored permanently for good reasons + "COM", # https://docs.astral.sh/ruff/rules/#flake8-commas-com + "D", # https://docs.astral.sh/ruff/rules/#pydocstyle-d + "E501", # handled by black https://docs.astral.sh/ruff/rules/line-too-long/ + "FBT", # https://docs.astral.sh/ruff/rules/#flake8-boolean-trap-fbt + "I", # handled by isort ] [tool.ruff.lint.per-file-ignores] -"_*.pyi" = ["PYI001"] +"*.pyi" = [ + # The following rules are ignored permanently for good reasons + "A002", # https://docs.astral.sh/ruff/rules/builtin-argument-shadowing/ + "FIX002", # https://docs.astral.sh/ruff/rules/line-contains-todo/ + "N", # https://docs.astral.sh/ruff/rules/#pep8-naming-n + "TD002", # https://docs.astral.sh/ruff/rules/missing-todo-author/ + # The following rules are ignored temporarily. Either fix them or move to the permanent list above. + "A001", # https://docs.astral.sh/ruff/rules/builtin-variable-shadowing/ + "A004", # https://docs.astral.sh/ruff/rules/builtin-import-shadowing/ + "PYI001", # https://docs.astral.sh/ruff/rules/unprefixed-type-param/ + "PYI042", # https://docs.astral.sh/ruff/rules/snake-case-type-alias/ + "TD003", # https://docs.astral.sh/ruff/rules/missing-todo-link/ + "ERA001", "ANN", "PLR0402", "PLC0105" + ] +"scripts/*" = [ + # The following rules are ignored permanently for good reasons + "EM", # https://docs.astral.sh/ruff/rules/#flake8-errmsg-em + "S603", # https://docs.astral.sh/ruff/rules/subprocess-without-shell-equals-true/ + # The following rules are ignored temporarily. Either fix them or move to the permanent list above. + "TRY", "PT", "BLE" +] +"tests/*" = [ + # The following rules are ignored permanently for good reasons + "B905", # https://docs.astral.sh/ruff/rules/zip-without-explicit-strict/ + "E731", # https://docs.astral.sh/ruff/rules/lambda-assignment/ + "EM", # https://docs.astral.sh/ruff/rules/#flake8-errmsg-em + "FIX002", # https://docs.astral.sh/ruff/rules/line-contains-todo/ + "PD", # Pandas is here + "PLC0415", # https://docs.astral.sh/ruff/rules/import-outside-top-level/ + "S101", # https://docs.astral.sh/ruff/rules/assert/ + "TD002", # https://docs.astral.sh/ruff/rules/missing-todo-author/ + # The following rules are ignored temporarily. Either fix them or move to the permanent list above. + "A001", # https://docs.astral.sh/ruff/rules/builtin-variable-shadowing/ + "PYI042", # https://docs.astral.sh/ruff/rules/snake-case-type-alias/ + "SLF001", # https://docs.astral.sh/ruff/rules/private-member-access/ + "ANN001", "ANN002", "ANN201", "ANN202", "ANN204", "ANN206", "ANN401", "ARG", "ERA", "RUF", "SIM", "TRY", "PT", "NPY", "N", "DTZ", "PLR", "TC", "PGH", "PTH", "S311", "C901" +] +"tests/test_io.py" = [ + # The following rules are ignored permanently for good reasons + "S301", # https://docs.astral.sh/ruff/rules/suspicious-pickle-usage/ +] [tool.mypy] @@ -252,6 +295,9 @@ reportPrivateUsage = false reportMissingModuleSource = true useLibraryCodeForTypes = false +[tool.pyrefly.errors] +bad-param-name-override = false # TODO: report to pyrefly https://github.com/pandas-dev/pandas-stubs/pull/1412#pullrequestreview-3310645279 + [tool.codespell] ignore-words-list = "indext, mose, sav, ser" diff --git a/scripts/test/__init__.py b/scripts/test/__init__.py index 57366bdba..eb7412b9a 100644 --- a/scripts/test/__init__.py +++ b/scripts/test/__init__.py @@ -51,7 +51,7 @@ def stubtest(allowlist: str, check_missing: bool, nightly: bool) -> None: steps = _DIST_STEPS[:2] if nightly: steps.append(_step.nightly) - run_job(steps + [stubtest]) + run_job([*steps, stubtest]) def pytest(nightly: bool) -> None: @@ -59,9 +59,9 @@ def pytest(nightly: bool) -> None: pytest_step = _step.pytest if nightly: setup_steps = [_step.nightly] - run_job(setup_steps + [pytest_step]) + run_job([*setup_steps, pytest_step]) def mypy_src(mypy_nightly: bool) -> None: steps = [_step.mypy_nightly] if mypy_nightly else [] - run_job(steps + [_step.mypy_src]) + run_job([*steps, _step.mypy_src]) diff --git a/tests/__init__.py b/tests/__init__.py index 89dd623e2..0e10b6b24 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -87,7 +87,7 @@ def check( and get_origin(shape_type) is tuple and (tuple_args := get_args(shape_type)) and ... not in tuple_args # fixed-length tuple - and (arr_ndim := getattr(actual, "ndim")) + and (arr_ndim := getattr(actual, "ndim")) # noqa: B009 != (expected_ndim := len(tuple_args)) ): raise RuntimeError( @@ -100,7 +100,7 @@ def check( and (dtype_args := get_args(dtype_type)) and isinstance((expected_dtype := dtype_args[0]), type) and issubclass(expected_dtype, np.generic) - and (arr_dtype := getattr(actual, "dtype")) != expected_dtype + and (arr_dtype := getattr(actual, "dtype")) != expected_dtype # noqa: B009 ): raise RuntimeError( f"Array has wrong dtype {arr_dtype}, expected {expected_dtype.__name__}" @@ -208,8 +208,6 @@ def pytest_warns_bounded( current = Version(version_str) if lb < current < ub: return pytest.warns(warning, match=match) - else: - if upper_exception is None: - return nullcontext() - else: - return suppress(upper_exception) + if upper_exception is None: + return nullcontext() + return suppress(upper_exception) diff --git a/tests/extension/decimal/array.py b/tests/extension/decimal/array.py index b043996f1..ad3a5ae17 100644 --- a/tests/extension/decimal/array.py +++ b/tests/extension/decimal/array.py @@ -150,21 +150,18 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs: Any): def reconstruct(x): if isinstance(x, (decimal.Decimal, numbers.Number)): return x - else: - return DecimalArray._from_sequence(x) + return DecimalArray._from_sequence(x) if ufunc.nout > 1: return tuple(reconstruct(x) for x in result) - else: - return reconstruct(result) + return reconstruct(result) def __getitem__(self, item): if isinstance(item, numbers.Integral): return self._data[item] - else: - # array, slice. - item = check_array_indexer(self, item) - return type(self)(self._data[item]) + # array, slice. + item = check_array_indexer(self, item) + return type(self)(self._data[item]) def take( self, indexer: TakeIndexer, *, allow_fill: bool = False, fill_value=None @@ -208,10 +205,9 @@ def __len__(self) -> int: def __contains__(self, item) -> bool | np.bool_: if not isinstance(item, decimal.Decimal): return False - elif item.is_nan(): + if item.is_nan(): return self.isna().any() - else: - return super().__contains__(item) + return super().__contains__(item) @property def nbytes(self) -> int: diff --git a/tests/indexes/test_indexes.py b/tests/indexes/test_indexes.py index 2ba3c0bc4..892cea004 100644 --- a/tests/indexes/test_indexes.py +++ b/tests/indexes/test_indexes.py @@ -152,10 +152,10 @@ def test_column_contains() -> None: # https://github.com/microsoft/python-type-stubs/issues/199 df = pd.DataFrame({"A": [1, 2], "B": ["c", "d"], "E": [3, 4]}) - collist = [column for column in df.columns] + collist = list(df.columns) check(assert_type(collist, list[str]), list, str) - collist2 = [column for column in df.columns[df.columns.str.contains("A|B")]] + collist2 = list(df.columns[df.columns.str.contains("A|B")]) check(assert_type(collist2, list[str]), list, str) length = len(df.columns[df.columns.str.contains("A|B")]) diff --git a/tests/series/test_series.py b/tests/series/test_series.py index 0a9aa85ba..0c9aad503 100644 --- a/tests/series/test_series.py +++ b/tests/series/test_series.py @@ -1910,7 +1910,7 @@ def test_resample() -> None: # GH 181 N = 10 index = pd.date_range("1/1/2000", periods=N, freq="min") - x = [x for x in range(N)] + x = list(range(N)) s = pd.Series(x, index=index, dtype=float) check(assert_type(s.resample("2min").std(), "pd.Series[float]"), pd.Series, float) check(assert_type(s.resample("2min").var(), "pd.Series[float]"), pd.Series, float) @@ -2976,7 +2976,7 @@ def test_astype_other() -> None: def test_all_astype_args_tested() -> None: """Check that all relevant numpy type aliases are tested.""" - NUMPY_ALIASES: set[str] = {k for k in np.sctypeDict} + NUMPY_ALIASES: set[str] = set(np.sctypeDict) EXCLUDED_ALIASES = { "datetime64", "m", @@ -3631,7 +3631,7 @@ def test_series_unique_timedelta() -> None: def test_slice_timestamp() -> None: dti = pd.date_range("1/1/2025", "2/28/2025") - s = pd.Series([i for i in range(len(dti))], index=dti) + s = pd.Series(list(range(len(dti))), index=dti) # For `s1`, see discussion in GH 397. Needs mypy fix. # s1 = s.loc["2025-01-15":"2025-01-20"] @@ -3708,7 +3708,7 @@ def test_series_bool_fails() -> None: if s == "foo": # pyright: ignore[reportGeneralTypeIssues] # Next line is unreachable. _a = s[0] - assert False + raise AssertionError except ValueError: pass diff --git a/tests/test_errors.py b/tests/test_errors.py index 673eea08d..23be71fd8 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -19,7 +19,7 @@ class Foo: def test_dtype_warning() -> None: with pytest.warns(errors.DtypeWarning): - warnings.warn("", errors.DtypeWarning) + warnings.warn("", errors.DtypeWarning, stacklevel=2) def test_duplicate_label_error() -> None: @@ -29,7 +29,7 @@ def test_duplicate_label_error() -> None: def test_empry_data_error() -> None: with pytest.raises(errors.EmptyDataError): - raise errors.EmptyDataError() + raise errors.EmptyDataError def test_in_casting_nan_error() -> None: @@ -59,69 +59,69 @@ def test_numba_util_error() -> None: def test_option_error() -> None: with pytest.raises(errors.OptionError): - raise errors.OptionError() + raise errors.OptionError def test_out_of_bounds_datetime() -> None: with pytest.raises(errors.OutOfBoundsDatetime): - raise errors.OutOfBoundsDatetime() + raise errors.OutOfBoundsDatetime def test_out_of_bounds_timedelta() -> None: with pytest.raises(errors.OutOfBoundsTimedelta): - raise errors.OutOfBoundsTimedelta() + raise errors.OutOfBoundsTimedelta def test_parser_error() -> None: with pytest.raises(errors.ParserError): - raise errors.ParserError() + raise errors.ParserError def test_parser_warning() -> None: with pytest.warns(errors.ParserWarning): - warnings.warn("", errors.ParserWarning) + warnings.warn("", errors.ParserWarning, stacklevel=2) def test_performance_warning() -> None: with pytest.warns(errors.PerformanceWarning): - warnings.warn("", errors.PerformanceWarning) + warnings.warn("", errors.PerformanceWarning, stacklevel=2) def test_unsorted_index_error() -> None: with pytest.raises(errors.UnsortedIndexError): - raise errors.UnsortedIndexError() + raise errors.UnsortedIndexError def test_unsupported_function_call() -> None: with pytest.raises(errors.UnsupportedFunctionCall): - raise errors.UnsupportedFunctionCall() + raise errors.UnsupportedFunctionCall def test_data_error() -> None: with pytest.raises(errors.DataError): - raise errors.DataError() + raise errors.DataError def test_specification_error() -> None: with pytest.raises(errors.SpecificationError): - raise errors.SpecificationError() + raise errors.SpecificationError def test_setting_with_copy_error() -> None: if PD_LTE_23: with pytest.raises(errors.SettingWithCopyError): - raise errors.SettingWithCopyError() + raise errors.SettingWithCopyError def test_setting_with_copy_warning() -> None: if PD_LTE_23: with pytest.warns(errors.SettingWithCopyWarning): - warnings.warn("", errors.SettingWithCopyWarning) + warnings.warn("", errors.SettingWithCopyWarning, stacklevel=2) def test_numexpr_clobbering_error() -> None: with pytest.raises(errors.NumExprClobberingError): - raise errors.NumExprClobberingError() + raise errors.NumExprClobberingError def test_undefined_variable_error() -> None: @@ -131,12 +131,12 @@ def test_undefined_variable_error() -> None: def test_indexing_error() -> None: with pytest.raises(errors.IndexingError): - raise errors.IndexingError() + raise errors.IndexingError def test_pyperclip_exception() -> None: with pytest.raises(errors.PyperclipException): - raise errors.PyperclipException() + raise errors.PyperclipException @pytest.mark.skipif(not WINDOWS, reason="Windows only") @@ -147,59 +147,59 @@ def test_pyperclip_windows_exception() -> None: def test_css_warning() -> None: with pytest.warns(errors.CSSWarning): - warnings.warn("", errors.CSSWarning) + warnings.warn("", errors.CSSWarning, stacklevel=2) def test_possible_data_loss_error() -> None: with pytest.raises(errors.PossibleDataLossError): - raise errors.PossibleDataLossError() + raise errors.PossibleDataLossError def test_closed_file_error() -> None: with pytest.raises(errors.ClosedFileError): - raise errors.ClosedFileError() + raise errors.ClosedFileError def test_incompatibility_warning() -> None: with pytest.warns(errors.IncompatibilityWarning): - warnings.warn("", errors.IncompatibilityWarning) + warnings.warn("", errors.IncompatibilityWarning, stacklevel=2) def test_attribute_conflict_warning() -> None: with pytest.warns(errors.AttributeConflictWarning): - warnings.warn("", errors.AttributeConflictWarning) + warnings.warn("", errors.AttributeConflictWarning, stacklevel=2) def test_database_error() -> None: with pytest.raises(errors.DatabaseError): - raise errors.DatabaseError() + raise errors.DatabaseError def test_possible_precision_loss() -> None: with pytest.warns(errors.PossiblePrecisionLoss): - warnings.warn("", errors.PossiblePrecisionLoss) + warnings.warn("", errors.PossiblePrecisionLoss, stacklevel=2) def test_value_label_type_mismatch() -> None: with pytest.warns(errors.ValueLabelTypeMismatch): - warnings.warn("", errors.ValueLabelTypeMismatch) + warnings.warn("", errors.ValueLabelTypeMismatch, stacklevel=2) def test_invalid_column_name() -> None: with pytest.warns(errors.InvalidColumnName): - warnings.warn("", errors.InvalidColumnName) + warnings.warn("", errors.InvalidColumnName, stacklevel=2) def test_categorical_conversion_warning() -> None: with pytest.warns(errors.CategoricalConversionWarning): - warnings.warn("", errors.CategoricalConversionWarning) + warnings.warn("", errors.CategoricalConversionWarning, stacklevel=2) def test_invalid_version() -> None: with pytest.raises(errors.InvalidVersion): - raise errors.InvalidVersion() + raise errors.InvalidVersion def test_no_buffer_present() -> None: with pytest.raises(errors.NoBufferPresent): - raise errors.NoBufferPresent() + raise errors.NoBufferPresent diff --git a/tests/test_frame.py b/tests/test_frame.py index e504be7d4..612371842 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -437,7 +437,7 @@ def test_types_filter() -> None: check(assert_type(df.filter(like="1"), pd.DataFrame), pd.DataFrame) # GH964 Docs state `items` is `list-like` check( - assert_type(df.filter(items=("col2", "col2", 1, tuple([4]))), pd.DataFrame), + assert_type(df.filter(items=("col2", "col2", 1, (4,))), pd.DataFrame), pd.DataFrame, ) @@ -3637,9 +3637,9 @@ def sample_to_df(x: pd.DataFrame) -> pd.DataFrame: def test_resample() -> None: # GH 181 N = 10 - x = [x for x in range(N)] + x = list(range(N)) index = pd.date_range("1/1/2000", periods=N, freq="min") - x = [x for x in range(N)] + x = list(range(N)) df = pd.DataFrame({"a": x, "b": x, "c": x}, index=index) check(assert_type(df.resample("2min").std(), pd.DataFrame), pd.DataFrame) check(assert_type(df.resample("2min").var(), pd.DataFrame), pd.DataFrame) @@ -3894,7 +3894,7 @@ def cond1(x: int) -> bool: def test_setitem_loc() -> None: # GH 254 df = pd.DataFrame.from_dict( - {view: (True, True, True) for view in ["A", "B", "C"]}, orient="index" + dict.fromkeys(["A", "B", "C"], (True, True, True)), orient="index" ) df.loc[["A", "C"]] = False my_arr = ["A", "C"] @@ -4129,11 +4129,10 @@ def select3(_: pd.DataFrame) -> int: def test_npint_loc_indexer() -> None: # GH 508 - df = pd.DataFrame(dict(x=[1, 2, 3]), index=np.array([10, 20, 30], dtype="uint64")) + df = pd.DataFrame({"x": [1, 2, 3]}, index=np.array([10, 20, 30], dtype="uint64")) def get_NDArray(df: pd.DataFrame, key: npt.NDArray[np.uint64]) -> pd.DataFrame: - df2 = df.loc[key] - return df2 + return df.loc[key] a: npt.NDArray[np.uint64] = np.array([10, 30], dtype="uint64") check(assert_type(get_NDArray(df, a), pd.DataFrame), pd.DataFrame) diff --git a/tests/test_pandas.py b/tests/test_pandas.py index 0d9917271..ff4289c2e 100644 --- a/tests/test_pandas.py +++ b/tests/test_pandas.py @@ -18,7 +18,7 @@ ) import pandas.util as pdutil -# TODO: github.com/pandas-dev/pandas/issues/55023 +# TODO: pandas-dev/pandas#55023 import pytest from typing_extensions import ( Never, @@ -242,7 +242,8 @@ def test_types_concat() -> None: check( assert_type( - pd.concat(map(lambda _: s2, ["some_value", 3]), axis=1), pd.DataFrame + pd.concat(map(lambda _: s2, ["some_value", 3]), axis=1), # noqa: C417 + pd.DataFrame, ), pd.DataFrame, ) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index a27725059..46d5b37a4 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -594,7 +594,7 @@ def test_plot_subplot_changes_150() -> None: def test_grouped_dataframe_boxplot(close_figures) -> None: - tuples = [t for t in itertools.product(range(10), range(2))] + tuples = list(itertools.product(range(10), range(2))) index = pd.MultiIndex.from_tuples(tuples, names=["lvl0", "lvl1"]) df = pd.DataFrame( data=np.random.randn(len(index), 2), columns=["A", "B"], index=index @@ -630,7 +630,7 @@ def test_grouped_dataframe_boxplot_single(close_figures) -> None: is put separately to make sure that we have no Axes already created. It will fail with `orientation="horizontal"`. """ - tuples = [t for t in itertools.product(range(10), range(2))] + tuples = list(itertools.product(range(10), range(2))) index = pd.MultiIndex.from_tuples(tuples, names=["lvl0", "lvl1"]) df = pd.DataFrame( data=np.random.randn(len(index), 2), columns=["A", "B"], index=index diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 03115f88a..881863afc 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -1186,7 +1186,7 @@ def test_to_timedelta_index() -> None: ] arg2 = tuple(arg0) arg3 = tuple(arg1) - arg4 = range(0, 10) + arg4 = range(10) arg5 = np.arange(10) arg6 = pd.Index(arg5) check(