From 64c64ec1fdc70755f5bcf7bd38289297d9e63327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 30 Aug 2023 09:42:13 -0400 Subject: [PATCH 1/2] Pandas 2.1 requires python>3.9 --- .github/workflows/test.yml | 2 +- .pre-commit-config.yaml | 4 +- pandas-stubs/_libs/tslibs/timestamps.pyi | 7 +- pandas-stubs/core/base.pyi | 2 +- pyproject.toml | 10 +-- scripts/_job.py | 9 ++- tests/test_api_types.py | 4 +- tests/test_frame.py | 85 ++++++++++++------------ tests/test_indexes.py | 11 ++- tests/test_io.py | 27 ++++---- tests/test_pandas.py | 2 +- tests/test_plotting.py | 7 +- tests/test_resampler.py | 11 ++- tests/test_series.py | 59 ++++++++-------- tests/test_styler.py | 7 +- 15 files changed, 110 insertions(+), 137 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4a3b7ee64..740540c08 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] - python-version: ['3.8', '3.9', '3.10', '3.11'] + python-version: ['3.9', '3.10', '3.11'] steps: - uses: actions/checkout@v3 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0b831caa7..095d31a13 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,12 +11,12 @@ repos: hooks: - id: isort - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.0.285 + rev: v0.0.286 hooks: - id: ruff args: [ --exit-non-zero-on-fix, - --target-version, py38, + --target-version, py39, --extend-select, "PYI,UP,RUF100", --ignore, "E501,E731,F841,PYI042", --per-file-ignores, "_*.pyi:PYI001", diff --git a/pandas-stubs/_libs/tslibs/timestamps.pyi b/pandas-stubs/_libs/tslibs/timestamps.pyi index 01811eeed..8679c8163 100644 --- a/pandas-stubs/_libs/tslibs/timestamps.pyi +++ b/pandas-stubs/_libs/tslibs/timestamps.pyi @@ -1,11 +1,11 @@ from datetime import ( + _IsoCalendarDate, date as _date, datetime, time as _time, timedelta, tzinfo as _tzinfo, ) -import sys from time import struct_time from typing import ( ClassVar, @@ -46,11 +46,6 @@ _Nonexistent: TypeAlias = ( Literal["raise", "NaT", "shift_backward", "shift_forward"] | Timedelta | timedelta ) -if sys.version_info >= (3, 9): - from datetime import _IsoCalendarDate -else: - _IsoCalendarDate: TypeAlias = tuple[int, int, int] - class Timestamp(datetime): min: ClassVar[Timestamp] max: ClassVar[Timestamp] diff --git a/pandas-stubs/core/base.pyi b/pandas-stubs/core/base.pyi index f32c934c4..fb31f13f6 100644 --- a/pandas-stubs/core/base.pyi +++ b/pandas-stubs/core/base.pyi @@ -1,6 +1,6 @@ +from collections.abc import Iterator from typing import ( Generic, - Iterator, Literal, ) diff --git a/pyproject.toml b/pyproject.toml index a0cb7d541..e4eca4719 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,6 @@ classifiers = [ "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", @@ -31,12 +30,9 @@ packages = [ "Documentation" = "https://pandas.pydata.org/pandas-docs/stable" [tool.poetry.dependencies] -python = ">=3.8" +python = ">=3.9" types-pytz = ">= 2022.1.1" -numpy = [ - {version = "<=1.24.3", python = "<=3.8"}, - {version = ">=1.25.0", python = ">=3.9"} -] +numpy = ">=1.25.0" [tool.poetry.dev-dependencies] mypy = "1.5.1" @@ -116,7 +112,7 @@ args = [{ name = "allowlist", positional = true, default = "", required = false, [tool.black] -target-version = ['py38'] +target-version = ['py39'] [tool.isort] known_pre_libs = "pandas._config" diff --git a/scripts/_job.py b/scripts/_job.py index 6aa51f743..c3d22dbf8 100644 --- a/scripts/_job.py +++ b/scripts/_job.py @@ -1,10 +1,9 @@ +from collections import deque from dataclasses import dataclass import sys import time from typing import ( Callable, - Deque, - List, Optional, ) @@ -18,7 +17,7 @@ class Step: rollback: Optional[Callable[[], None]] = None -def __rollback_job(steps: Deque[Step]): +def __rollback_job(steps: deque[Step]): """ Responsible to run rollback of steps. """ @@ -35,12 +34,12 @@ def __rollback_job(steps: Deque[Step]): ) -def run_job(steps: List[Step]) -> None: +def run_job(steps: list[Step]) -> None: """ Responsible to run steps with logs. """ - rollback_steps = Deque[Step]() + rollback_steps = deque[Step]() failed = False for step in steps: diff --git a/tests/test_api_types.py b/tests/test_api_types.py index e06fe69d9..92ba58693 100644 --- a/tests/test_api_types.py +++ b/tests/test_api_types.py @@ -1,5 +1,3 @@ -from typing import Type - import numpy as np import pandas as pd from pandas.api.extensions import ExtensionDtype @@ -468,7 +466,7 @@ def test_union_categoricals() -> None: def test_check_extension_dtypes() -> None: # GH 315 - def check_ext_dtype(etype: Type[ExtensionDtype]): + def check_ext_dtype(etype: type[ExtensionDtype]): assert issubclass(etype, ExtensionDtype) check_ext_dtype(pd.Int64Dtype) diff --git a/tests/test_frame.py b/tests/test_frame.py index 3f9bca6cf..748a646bd 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -1,6 +1,12 @@ from __future__ import annotations from collections import defaultdict +from collections.abc import ( + Hashable, + Iterable, + Iterator, + Mapping, +) import csv import datetime from enum import Enum @@ -11,14 +17,7 @@ TYPE_CHECKING, Any, Callable, - Dict, Generic, - Hashable, - Iterable, - Iterator, - List, - Mapping, - Tuple, TypedDict, TypeVar, Union, @@ -2037,17 +2036,17 @@ def test_groupby_result() -> None: # GH 142 df = pd.DataFrame({"a": [0, 1, 2], "b": [4, 5, 6], "c": [7, 8, 9]}) iterator = df.groupby(["a", "b"]).__iter__() - assert_type(iterator, Iterator[Tuple[Tuple, pd.DataFrame]]) + assert_type(iterator, Iterator[tuple[tuple, pd.DataFrame]]) index, value = next(iterator) - assert_type((index, value), Tuple[Tuple, pd.DataFrame]) + assert_type((index, value), tuple[tuple, pd.DataFrame]) - check(assert_type(index, Tuple), tuple, np.integer) + check(assert_type(index, tuple), tuple, np.integer) check(assert_type(value, pd.DataFrame), pd.DataFrame) iterator2 = df.groupby("a").__iter__() - assert_type(iterator2, Iterator[Tuple[Scalar, pd.DataFrame]]) + assert_type(iterator2, Iterator[tuple[Scalar, pd.DataFrame]]) index2, value2 = next(iterator2) - assert_type((index2, value2), Tuple[Scalar, pd.DataFrame]) + assert_type((index2, value2), tuple[Scalar, pd.DataFrame]) check(assert_type(index2, Scalar), int) check(assert_type(value2, pd.DataFrame), pd.DataFrame) @@ -2056,11 +2055,11 @@ def test_groupby_result() -> None: # grouping by pd.MultiIndex should always resolve to a tuple as well multi_index = pd.MultiIndex.from_frame(df[["a", "b"]]) iterator3 = df.groupby(multi_index).__iter__() - assert_type(iterator3, Iterator[Tuple[Tuple, pd.DataFrame]]) + assert_type(iterator3, Iterator[tuple[tuple, pd.DataFrame]]) index3, value3 = next(iterator3) - assert_type((index3, value3), Tuple[Tuple, pd.DataFrame]) + assert_type((index3, value3), tuple[tuple, pd.DataFrame]) - check(assert_type(index3, Tuple), tuple, int) + check(assert_type(index3, tuple), tuple, int) check(assert_type(value3, pd.DataFrame), pd.DataFrame) # Want to make sure these cases are differentiated @@ -2080,27 +2079,27 @@ def test_groupby_result_for_scalar_indexes() -> None: df = pd.DataFrame({"date": dates, "days": 1}) period_index = pd.PeriodIndex(df.date, freq="M") iterator = df.groupby(period_index).__iter__() - assert_type(iterator, Iterator[Tuple[pd.Period, pd.DataFrame]]) + assert_type(iterator, Iterator[tuple[pd.Period, pd.DataFrame]]) index, value = next(iterator) - assert_type((index, value), Tuple[pd.Period, pd.DataFrame]) + assert_type((index, value), tuple[pd.Period, pd.DataFrame]) check(assert_type(index, pd.Period), pd.Period) check(assert_type(value, pd.DataFrame), pd.DataFrame) dt_index = pd.DatetimeIndex(dates) iterator2 = df.groupby(dt_index).__iter__() - assert_type(iterator2, Iterator[Tuple[pd.Timestamp, pd.DataFrame]]) + assert_type(iterator2, Iterator[tuple[pd.Timestamp, pd.DataFrame]]) index2, value2 = next(iterator2) - assert_type((index2, value2), Tuple[pd.Timestamp, pd.DataFrame]) + assert_type((index2, value2), tuple[pd.Timestamp, pd.DataFrame]) check(assert_type(index2, pd.Timestamp), pd.Timestamp) check(assert_type(value2, pd.DataFrame), pd.DataFrame) tdelta_index = pd.TimedeltaIndex(dates - pd.Timestamp("2020-01-01")) iterator3 = df.groupby(tdelta_index).__iter__() - assert_type(iterator3, Iterator[Tuple[pd.Timedelta, pd.DataFrame]]) + assert_type(iterator3, Iterator[tuple[pd.Timedelta, pd.DataFrame]]) index3, value3 = next(iterator3) - assert_type((index3, value3), Tuple[pd.Timedelta, pd.DataFrame]) + assert_type((index3, value3), tuple[pd.Timedelta, pd.DataFrame]) check(assert_type(index3, pd.Timedelta), pd.Timedelta) check(assert_type(value3, pd.DataFrame), pd.DataFrame) @@ -2111,9 +2110,9 @@ def test_groupby_result_for_scalar_indexes() -> None: interval_index = pd.IntervalIndex(intervals) assert_type(interval_index, "pd.IntervalIndex[pd.Interval[pd.Timestamp]]") iterator4 = df.groupby(interval_index).__iter__() - assert_type(iterator4, Iterator[Tuple["pd.Interval[pd.Timestamp]", pd.DataFrame]]) + assert_type(iterator4, Iterator[tuple["pd.Interval[pd.Timestamp]", pd.DataFrame]]) index4, value4 = next(iterator4) - assert_type((index4, value4), Tuple["pd.Interval[pd.Timestamp]", pd.DataFrame]) + assert_type((index4, value4), tuple["pd.Interval[pd.Timestamp]", pd.DataFrame]) check(assert_type(index4, "pd.Interval[pd.Timestamp]"), pd.Interval) check(assert_type(value4, pd.DataFrame), pd.DataFrame) @@ -2136,9 +2135,9 @@ def test_groupby_result_for_ambiguous_indexes() -> None: df = pd.DataFrame({"a": [0, 1, 2], "b": [4, 5, 6], "c": [7, 8, 9]}) # this will use pd.Index which is ambiguous iterator = df.groupby(df.index).__iter__() - assert_type(iterator, Iterator[Tuple[Any, pd.DataFrame]]) + assert_type(iterator, Iterator[tuple[Any, pd.DataFrame]]) index, value = next(iterator) - assert_type((index, value), Tuple[Any, pd.DataFrame]) + assert_type((index, value), tuple[Any, pd.DataFrame]) check(assert_type(index, Any), int) check(assert_type(value, pd.DataFrame), pd.DataFrame) @@ -2153,9 +2152,9 @@ def test_groupby_result_for_ambiguous_indexes() -> None: ): categorical_index = pd.CategoricalIndex(df.a) iterator2 = df.groupby(categorical_index).__iter__() - assert_type(iterator2, Iterator[Tuple[Any, pd.DataFrame]]) + assert_type(iterator2, Iterator[tuple[Any, pd.DataFrame]]) index2, value2 = next(iterator2) - assert_type((index2, value2), Tuple[Any, pd.DataFrame]) + assert_type((index2, value2), tuple[Any, pd.DataFrame]) check(assert_type(index2, Any), int) check(assert_type(value2, pd.DataFrame), pd.DataFrame) @@ -2308,8 +2307,8 @@ def test_to_records(): def test_to_dict(): - check(assert_type(DF.to_dict(), Dict[Hashable, Any]), dict) - check(assert_type(DF.to_dict("split"), Dict[Hashable, Any]), dict) + check(assert_type(DF.to_dict(), dict[Hashable, Any]), dict) + check(assert_type(DF.to_dict("split"), dict[Hashable, Any]), dict) target: Mapping = defaultdict(list) check(assert_type(DF.to_dict(into=target), Mapping[Hashable, Any]), defaultdict) @@ -2319,9 +2318,9 @@ def test_to_dict(): defaultdict, ) target = defaultdict(list) - check(assert_type(DF.to_dict("records"), List[Dict[Hashable, Any]]), list) + check(assert_type(DF.to_dict("records"), list[dict[Hashable, Any]]), list) check( - assert_type(DF.to_dict("records", into=target), List[Mapping[Hashable, Any]]), + assert_type(DF.to_dict("records", into=target), list[Mapping[Hashable, Any]]), list, ) @@ -2725,34 +2724,34 @@ def test_to_dict_index() -> None: df = pd.DataFrame({"a": [1, 2], "b": [9, 10]}) check( assert_type( - df.to_dict(orient="records", index=True), List[Dict[Hashable, Any]] + df.to_dict(orient="records", index=True), list[dict[Hashable, Any]] ), list, ) - check(assert_type(df.to_dict(orient="dict", index=True), Dict[Hashable, Any]), dict) + check(assert_type(df.to_dict(orient="dict", index=True), dict[Hashable, Any]), dict) check( - assert_type(df.to_dict(orient="series", index=True), Dict[Hashable, Any]), dict + assert_type(df.to_dict(orient="series", index=True), dict[Hashable, Any]), dict ) check( - assert_type(df.to_dict(orient="index", index=True), Dict[Hashable, Any]), dict + assert_type(df.to_dict(orient="index", index=True), dict[Hashable, Any]), dict ) check( - assert_type(df.to_dict(orient="split", index=True), Dict[Hashable, Any]), dict + assert_type(df.to_dict(orient="split", index=True), dict[Hashable, Any]), dict ) check( - assert_type(df.to_dict(orient="tight", index=True), Dict[Hashable, Any]), dict + assert_type(df.to_dict(orient="tight", index=True), dict[Hashable, Any]), dict ) check( - assert_type(df.to_dict(orient="tight", index=False), Dict[Hashable, Any]), dict + assert_type(df.to_dict(orient="tight", index=False), dict[Hashable, Any]), dict ) check( - assert_type(df.to_dict(orient="split", index=False), Dict[Hashable, Any]), dict + assert_type(df.to_dict(orient="split", index=False), dict[Hashable, Any]), dict ) if TYPE_CHECKING_INVALID_USAGE: - check(assert_type(df.to_dict(orient="records", index=False), List[Dict[Hashable, Any]]), list) # type: ignore[assert-type, call-overload] # pyright: ignore[reportGeneralTypeIssues] - check(assert_type(df.to_dict(orient="dict", index=False), Dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportGeneralTypeIssues] - check(assert_type(df.to_dict(orient="series", index=False), Dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportGeneralTypeIssues] - check(assert_type(df.to_dict(orient="index", index=False), Dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportGeneralTypeIssues] + check(assert_type(df.to_dict(orient="records", index=False), list[dict[Hashable, Any]]), list) # type: ignore[assert-type, call-overload] # pyright: ignore[reportGeneralTypeIssues] + check(assert_type(df.to_dict(orient="dict", index=False), dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportGeneralTypeIssues] + check(assert_type(df.to_dict(orient="series", index=False), dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportGeneralTypeIssues] + check(assert_type(df.to_dict(orient="index", index=False), dict[Hashable, Any]), dict) # type: ignore[assert-type, call-overload] # pyright: ignore[reportGeneralTypeIssues] def test_suffix_prefix_index() -> None: diff --git a/tests/test_indexes.py b/tests/test_indexes.py index 5ae1bba6d..6d89b45cb 100644 --- a/tests/test_indexes.py +++ b/tests/test_indexes.py @@ -1,10 +1,7 @@ from __future__ import annotations import datetime as dt -from typing import ( - Tuple, - Union, -) +from typing import Union import numpy as np from numpy import typing as npt @@ -734,9 +731,9 @@ def test_index_operators() -> None: check(assert_type(i1 % i2, "pd.Index[int]"), pd.Index) check(assert_type(i1 % 10, "pd.Index[int]"), pd.Index) check(assert_type(10 % i1, "pd.Index[int]"), pd.Index) - check(assert_type(divmod(i1, i2), Tuple["pd.Index[int]", "pd.Index[int]"]), tuple) - check(assert_type(divmod(i1, 10), Tuple["pd.Index[int]", "pd.Index[int]"]), tuple) - check(assert_type(divmod(10, i1), Tuple["pd.Index[int]", "pd.Index[int]"]), tuple) + check(assert_type(divmod(i1, i2), tuple["pd.Index[int]", "pd.Index[int]"]), tuple) + check(assert_type(divmod(i1, 10), tuple["pd.Index[int]", "pd.Index[int]"]), tuple) + check(assert_type(divmod(10, i1), tuple["pd.Index[int]", "pd.Index[int]"]), tuple) if TYPE_CHECKING_INVALID_USAGE: assert_type( diff --git a/tests/test_io.py b/tests/test_io.py index e1d9e8a02..ca6274071 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,4 +1,5 @@ from collections import defaultdict +from collections.abc import Generator import csv import io import os.path @@ -8,11 +9,7 @@ from typing import ( TYPE_CHECKING, Any, - Dict, - Generator, - List, Literal, - Tuple, Union, ) @@ -212,7 +209,7 @@ def _true_if_greater_than_0(i: int) -> bool: return i > 0 -def _true_if_first_param_is_head(t: Tuple[str, int]) -> bool: +def _true_if_first_param_is_head(t: tuple[str, int]) -> bool: return t[0] == "head" @@ -366,7 +363,7 @@ def test_hdfstore(): check(assert_type(store, HDFStore), HDFStore) check(assert_type(store.put("df", DF, "table"), None), type(None)) check(assert_type(store.append("df2", DF, "table"), None), type(None)) - check(assert_type(store.keys(), List[str]), list) + check(assert_type(store.keys(), list[str]), list) check(assert_type(store.info(), str), str) check( assert_type(store.select("df", start=0, stop=1), Union[DataFrame, Series]), @@ -866,7 +863,7 @@ def test_read_excel() -> None: check( assert_type( pd.read_excel(path, sheet_name=["Sheet1"]), - Dict[Union[int, str], pd.DataFrame], + dict[Union[int, str], pd.DataFrame], ), dict, ) @@ -876,21 +873,21 @@ def test_read_excel() -> None: ) check( assert_type( - pd.read_excel(path, sheet_name=[0]), Dict[Union[int, str], pd.DataFrame] + pd.read_excel(path, sheet_name=[0]), dict[Union[int, str], pd.DataFrame] ), dict, ) check( assert_type( pd.read_excel(path, sheet_name=[0, "Sheet1"]), - Dict[Union[int, str], pd.DataFrame], + dict[Union[int, str], pd.DataFrame], ), dict, ) check( assert_type( pd.read_excel(path, sheet_name=None), - Dict[Union[int, str], pd.DataFrame], + dict[Union[int, str], pd.DataFrame], ), dict, ) @@ -1011,13 +1008,13 @@ def test_read_excel_list(): check( assert_type( read_excel(path, sheet_name=["Sheet1"]), - Dict[Union[str, int], DataFrame], + dict[Union[str, int], DataFrame], ), dict, ) check( assert_type( - read_excel(path, sheet_name=[0]), Dict[Union[str, int], DataFrame] + read_excel(path, sheet_name=[0]), dict[Union[str, int], DataFrame] ), dict, ) @@ -1045,7 +1042,7 @@ def test_excel_writer(): check(assert_type(read_excel(ef), DataFrame), DataFrame) check(assert_type(ef.parse(sheet_name=0), DataFrame), DataFrame) check( - assert_type(ef.parse(sheet_name=[0]), Dict[Union[str, int], DataFrame]), + assert_type(ef.parse(sheet_name=[0]), dict[Union[str, int], DataFrame]), dict, ) check(assert_type(ef.close(), None), type(None)) @@ -1244,7 +1241,7 @@ def test_read_html(): check(assert_type(DF.to_html(), str), str) with ensure_clean() as path: check(assert_type(DF.to_html(path), None), type(None)) - check(assert_type(read_html(path), List[DataFrame]), list) + check(assert_type(read_html(path), list[DataFrame]), list) def test_csv_quoting(): @@ -1453,7 +1450,7 @@ def test_read_with_lxml_dtype_backend() -> None: check(assert_type(DF.to_html(path), None), type(None)) check( assert_type( - read_html(path, dtype_backend="numpy_nullable"), List[DataFrame] + read_html(path, dtype_backend="numpy_nullable"), list[DataFrame] ), list, ) diff --git a/tests/test_pandas.py b/tests/test_pandas.py index ed67efc86..84b8b2e5a 100644 --- a/tests/test_pandas.py +++ b/tests/test_pandas.py @@ -750,7 +750,7 @@ def test_lreshape() -> None: "year2": [2008, 2008], } ) - from typing import Hashable + from collections.abc import Hashable groups: dict[Hashable, list[Hashable]] = { ("year",): [("year1",), "year2"], diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 42067b558..29dbbf69b 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -1,8 +1,5 @@ import io -from typing import ( - Any, - Dict, -) +from typing import Any import matplotlib.pyplot as plt from matplotlib.table import Table @@ -229,7 +226,7 @@ def test_plot_parallel_coordinates(close_figures) -> None: def test_plot_params(close_figures) -> None: - check(assert_type(pd.plotting.plot_params, Dict[str, Any]), dict) + check(assert_type(pd.plotting.plot_params, dict[str, Any]), dict) def test_radviz(close_figures) -> None: diff --git a/tests/test_resampler.py b/tests/test_resampler.py index 6b192b60a..f87e98942 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -1,9 +1,8 @@ -from typing import ( +from collections.abc import ( Generator, Hashable, - Tuple, - Union, ) +from typing import Union import numpy as np import pandas as pd @@ -40,10 +39,10 @@ def test_props() -> None: def test_iter() -> None: assert_type( - iter(DF.resample("m")), Generator[Tuple[Hashable, DataFrame], None, None] + iter(DF.resample("m")), Generator[tuple[Hashable, DataFrame], None, None] ) for v in DF.resample("m"): - check(assert_type(v, Tuple[Hashable, DataFrame]), tuple) + check(assert_type(v, tuple[Hashable, DataFrame]), tuple) def test_agg_funcs() -> None: @@ -189,7 +188,7 @@ def test_props_series() -> None: def test_iter_series() -> None: for v in S.resample("m"): - check(assert_type(v, Tuple[Hashable, Series]), tuple) + check(assert_type(v, tuple[Hashable, Series]), tuple) def test_agg_funcs_series() -> None: diff --git a/tests/test_series.py b/tests/test_series.py index 1a1140909..e83da57ea 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -1,5 +1,10 @@ from __future__ import annotations +from collections.abc import ( + Iterable, + Iterator, + Sequence, +) import datetime from decimal import Decimal from enum import Enum @@ -9,13 +14,7 @@ from typing import ( TYPE_CHECKING, Any, - Dict, Generic, - Iterable, - Iterator, - List, - Sequence, - Tuple, TypeVar, cast, ) @@ -635,17 +634,17 @@ def test_groupby_result() -> None: multi_index = pd.MultiIndex.from_tuples([(0, 0), (0, 1), (1, 0)], names=["a", "b"]) s = pd.Series([0, 1, 2], index=multi_index, dtype=int) iterator = s.groupby(["a", "b"]).__iter__() - assert_type(iterator, Iterator[Tuple[Tuple, "pd.Series[int]"]]) + assert_type(iterator, Iterator[tuple[tuple, "pd.Series[int]"]]) index, value = next(iterator) - assert_type((index, value), Tuple[Tuple, "pd.Series[int]"]) + assert_type((index, value), tuple[tuple, "pd.Series[int]"]) - check(assert_type(index, Tuple), tuple, np.integer) + check(assert_type(index, tuple), tuple, np.integer) check(assert_type(value, "pd.Series[int]"), pd.Series, np.integer) iterator2 = s.groupby("a").__iter__() - assert_type(iterator2, Iterator[Tuple[Scalar, "pd.Series[int]"]]) + assert_type(iterator2, Iterator[tuple[Scalar, "pd.Series[int]"]]) index2, value2 = next(iterator2) - assert_type((index2, value2), Tuple[Scalar, "pd.Series[int]"]) + assert_type((index2, value2), tuple[Scalar, "pd.Series[int]"]) check(assert_type(index2, Scalar), int) check(assert_type(value2, "pd.Series[int]"), pd.Series, np.integer) @@ -653,11 +652,11 @@ def test_groupby_result() -> None: # GH 674 # grouping by pd.MultiIndex should always resolve to a tuple as well iterator3 = s.groupby(multi_index).__iter__() - assert_type(iterator3, Iterator[Tuple[Tuple, "pd.Series[int]"]]) + assert_type(iterator3, Iterator[tuple[tuple, "pd.Series[int]"]]) index3, value3 = next(iterator3) - assert_type((index3, value3), Tuple[Tuple, "pd.Series[int]"]) + assert_type((index3, value3), tuple[tuple, "pd.Series[int]"]) - check(assert_type(index3, Tuple), tuple, int) + check(assert_type(index3, tuple), tuple, int) check(assert_type(value3, "pd.Series[int]"), pd.Series, np.integer) # Want to make sure these cases are differentiated @@ -685,27 +684,27 @@ def test_groupby_result_for_scalar_indexes() -> None: period_index = pd.PeriodIndex(dates, freq="M") iterator = s.groupby(period_index).__iter__() - assert_type(iterator, Iterator[Tuple[pd.Period, "pd.Series[int]"]]) + assert_type(iterator, Iterator[tuple[pd.Period, "pd.Series[int]"]]) index, value = next(iterator) - assert_type((index, value), Tuple[pd.Period, "pd.Series[int]"]) + assert_type((index, value), tuple[pd.Period, "pd.Series[int]"]) check(assert_type(index, pd.Period), pd.Period) check(assert_type(value, "pd.Series[int]"), pd.Series, np.integer) dt_index = pd.DatetimeIndex(dates) iterator2 = s.groupby(dt_index).__iter__() - assert_type(iterator2, Iterator[Tuple[pd.Timestamp, "pd.Series[int]"]]) + assert_type(iterator2, Iterator[tuple[pd.Timestamp, "pd.Series[int]"]]) index2, value2 = next(iterator2) - assert_type((index2, value2), Tuple[pd.Timestamp, "pd.Series[int]"]) + assert_type((index2, value2), tuple[pd.Timestamp, "pd.Series[int]"]) check(assert_type(index2, pd.Timestamp), pd.Timestamp) check(assert_type(value2, "pd.Series[int]"), pd.Series, np.integer) tdelta_index = pd.TimedeltaIndex(dates - pd.Timestamp("2020-01-01")) iterator3 = s.groupby(tdelta_index).__iter__() - assert_type(iterator3, Iterator[Tuple[pd.Timedelta, "pd.Series[int]"]]) + assert_type(iterator3, Iterator[tuple[pd.Timedelta, "pd.Series[int]"]]) index3, value3 = next(iterator3) - assert_type((index3, value3), Tuple[pd.Timedelta, "pd.Series[int]"]) + assert_type((index3, value3), tuple[pd.Timedelta, "pd.Series[int]"]) check(assert_type(index3, pd.Timedelta), pd.Timedelta) check(assert_type(value3, "pd.Series[int]"), pd.Series, np.integer) @@ -717,10 +716,10 @@ def test_groupby_result_for_scalar_indexes() -> None: assert_type(interval_index, "pd.IntervalIndex[pd.Interval[pd.Timestamp]]") iterator4 = s.groupby(interval_index).__iter__() assert_type( - iterator4, Iterator[Tuple["pd.Interval[pd.Timestamp]", "pd.Series[int]"]] + iterator4, Iterator[tuple["pd.Interval[pd.Timestamp]", "pd.Series[int]"]] ) index4, value4 = next(iterator4) - assert_type((index4, value4), Tuple["pd.Interval[pd.Timestamp]", "pd.Series[int]"]) + assert_type((index4, value4), tuple["pd.Interval[pd.Timestamp]", "pd.Series[int]"]) check(assert_type(index4, "pd.Interval[pd.Timestamp]"), pd.Interval) check(assert_type(value4, "pd.Series[int]"), pd.Series, np.integer) @@ -743,9 +742,9 @@ def test_groupby_result_for_ambiguous_indexes() -> None: s = pd.Series([0, 1, 2], index=["a", "b", "a"], dtype=int) # this will use pd.Index which is ambiguous iterator = s.groupby(s.index).__iter__() - assert_type(iterator, Iterator[Tuple[Any, "pd.Series[int]"]]) + assert_type(iterator, Iterator[tuple[Any, "pd.Series[int]"]]) index, value = next(iterator) - assert_type((index, value), Tuple[Any, "pd.Series[int]"]) + assert_type((index, value), tuple[Any, "pd.Series[int]"]) check(assert_type(index, Any), str) check(assert_type(value, "pd.Series[int]"), pd.Series, np.integer) @@ -759,9 +758,9 @@ def test_groupby_result_for_ambiguous_indexes() -> None: ): categorical_index = pd.CategoricalIndex(s.index) iterator2 = s.groupby(categorical_index).__iter__() - assert_type(iterator2, Iterator[Tuple[Any, "pd.Series[int]"]]) + assert_type(iterator2, Iterator[tuple[Any, "pd.Series[int]"]]) index2, value2 = next(iterator2) - assert_type((index2, value2), Tuple[Any, "pd.Series[int]"]) + assert_type((index2, value2), tuple[Any, "pd.Series[int]"]) check(assert_type(index2, Any), str) check(assert_type(value2, "pd.Series[int]"), pd.Series, np.integer) @@ -1324,13 +1323,13 @@ def test_types_iter() -> None: def test_types_to_list() -> None: s = pd.Series(["a", "b", "c"], dtype=str) - check(assert_type(s.tolist(), List[str]), list, str) - check(assert_type(s.to_list(), List[str]), list, str) + check(assert_type(s.tolist(), list[str]), list, str) + check(assert_type(s.to_list(), list[str]), list, str) def test_types_to_dict() -> None: s = pd.Series(["a", "b", "c"], dtype=str) - assert_type(s.to_dict(), Dict[Any, str]) + assert_type(s.to_dict(), dict[Any, str]) def test_categorical_codes(): @@ -1758,7 +1757,7 @@ def test_change_to_dict_return_type() -> None: value = ["a", "b", "c"] df = pd.DataFrame(zip(id, value), columns=["id", "value"]) fd = df.set_index("id")["value"].to_dict() - check(assert_type(fd, Dict[Any, Any]), dict) + check(assert_type(fd, dict[Any, Any]), dict) ASTYPE_BOOL_ARGS: list[tuple[BooleanDtypeArg, type]] = [ diff --git a/tests/test_styler.py b/tests/test_styler.py index 79a92cb54..67c80d0c6 100644 --- a/tests/test_styler.py +++ b/tests/test_styler.py @@ -2,10 +2,7 @@ import os import pathlib -from typing import ( - TYPE_CHECKING, - Type, -) +from typing import TYPE_CHECKING from jinja2.environment import ( Environment, @@ -121,7 +118,7 @@ def test_from_custom_template() -> None: check( assert_type( Styler.from_custom_template(str(PWD / "data" / "myhtml_table.tpl")), - Type[Styler], + type[Styler], ), type(Styler), ) From 330636d536a76ad4cea7ac6ae35ca7051211d239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 30 Aug 2023 09:54:44 -0400 Subject: [PATCH 2/2] manual changes --- tests/test_dtypes.py | 10 ++++++---- tests/test_frame.py | 10 +++++----- tests/test_indexes.py | 10 +++++----- tests/test_series.py | 2 +- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/tests/test_dtypes.py b/tests/test_dtypes.py index 68c8f98be..065861772 100644 --- a/tests/test_dtypes.py +++ b/tests/test_dtypes.py @@ -14,8 +14,10 @@ import numpy as np import pandas as pd from pandas.api.types import is_any_real_numeric_dtype -from pandas.core.arrays import BooleanArray # noqa: F401 -from pandas.core.arrays import IntegerArray # noqa: F401 +from pandas.core.arrays import ( + BooleanArray, + IntegerArray, +) import pyarrow as pa from typing_extensions import assert_type @@ -96,7 +98,7 @@ def test_int64_dtype() -> None: i64dt = pd.Int64Dtype() check(assert_type(i64dt.itemsize, int), int) check(assert_type(i64dt.na_value, NAType), NAType) - check(assert_type(i64dt.construct_array_type(), "type[IntegerArray]"), type) + check(assert_type(i64dt.construct_array_type(), type[IntegerArray]), type) def test_categorical_dtype() -> None: @@ -138,7 +140,7 @@ def test_boolean_dtype() -> None: b_dt = pd.BooleanDtype() check(assert_type(b_dt, pd.BooleanDtype), pd.BooleanDtype) check(assert_type(b_dt.na_value, NAType), NAType) - check(assert_type(b_dt.construct_array_type(), "type[BooleanArray]"), type) + check(assert_type(b_dt.construct_array_type(), type[BooleanArray]), type) def test_arrow_dtype() -> None: diff --git a/tests/test_frame.py b/tests/test_frame.py index 748a646bd..383f4ecc6 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -773,17 +773,17 @@ def test_types_element_wise_arithmetic() -> None: # divmod operation was added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html check( - assert_type(divmod(df, df2), "tuple[pd.DataFrame, pd.DataFrame]"), + assert_type(divmod(df, df2), tuple[pd.DataFrame, pd.DataFrame]), tuple, pd.DataFrame, ) check( - assert_type(df.__divmod__(df2), "tuple[pd.DataFrame, pd.DataFrame]"), + assert_type(df.__divmod__(df2), tuple[pd.DataFrame, pd.DataFrame]), tuple, pd.DataFrame, ) check( - assert_type(df.__rdivmod__(df2), "tuple[pd.DataFrame, pd.DataFrame]"), + assert_type(df.__rdivmod__(df2), tuple[pd.DataFrame, pd.DataFrame]), tuple, pd.DataFrame, ) @@ -1722,7 +1722,7 @@ def test_indexslice_getitem(): .set_index(["x", "y"]) ) ind = pd.Index([2, 3]) - check(assert_type(pd.IndexSlice[ind, :], "tuple[pd.Index[int], slice]"), tuple) + check(assert_type(pd.IndexSlice[ind, :], tuple["pd.Index[int]", slice]), tuple) check(assert_type(df.loc[pd.IndexSlice[ind, :]], pd.DataFrame), pd.DataFrame) check(assert_type(df.loc[pd.IndexSlice[1:2]], pd.DataFrame), pd.DataFrame) check( @@ -2607,7 +2607,7 @@ def test_in_columns() -> None: # GH 532 (PR) df = pd.DataFrame(np.random.random((3, 4)), columns=["cat", "dog", "rat", "pig"]) cols = [c for c in df.columns if "at" in c] - check(assert_type(cols, "list[str]"), list, str) + check(assert_type(cols, list[str]), list, str) check(assert_type(df.loc[:, cols], pd.DataFrame), pd.DataFrame) check(assert_type(df[cols], pd.DataFrame), pd.DataFrame) check(assert_type(df.groupby(by=cols).sum(), pd.DataFrame), pd.DataFrame) diff --git a/tests/test_indexes.py b/tests/test_indexes.py index 6d89b45cb..1de763227 100644 --- a/tests/test_indexes.py +++ b/tests/test_indexes.py @@ -62,8 +62,8 @@ def test_multiindex_get_level_values() -> None: def test_index_tolist() -> None: i1 = pd.Index([1, 2, 3]) - check(assert_type(i1.tolist(), "list[int]"), list, int) - check(assert_type(i1.to_list(), "list[int]"), list, int) + check(assert_type(i1.tolist(), list[int]), list, int) + check(assert_type(i1.to_list(), list[int]), list, int) def test_column_getitem() -> None: @@ -90,7 +90,7 @@ def test_column_sequence() -> None: df = pd.DataFrame([1, 2, 3]) col_list = list(df.columns) check( - assert_type(col_list, "list[str]"), + assert_type(col_list, list[str]), list, int, ) @@ -701,8 +701,8 @@ def test_interval_index_tuples(): def test_sorted_and_list() -> None: # GH 497 i1 = pd.Index([3, 2, 1]) - check(assert_type(sorted(i1), "list[int]"), list, int) - check(assert_type(list(i1), "list[int]"), list, int) + check(assert_type(sorted(i1), list[int]), list, int) + check(assert_type(list(i1), list[int]), list, int) def test_index_operators() -> None: diff --git a/tests/test_series.py b/tests/test_series.py index e83da57ea..43fdd4afb 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -556,7 +556,7 @@ def test_types_element_wise_arithmetic() -> None: res_pow: pd.Series = s ** s2.abs() res_pow2: pd.Series = s.pow(s2.abs(), fill_value=0) - check(assert_type(divmod(s, s2), "tuple[pd.Series[int], pd.Series[int]]"), tuple) + check(assert_type(divmod(s, s2), tuple["pd.Series[int]", "pd.Series[int]"]), tuple) def test_types_scalar_arithmetic() -> None: