From b598270d0dbf19cbb248b8e8598d8b57e6f8c2da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 22 Dec 2023 21:40:42 -0500 Subject: [PATCH 1/3] CI: test on 3.12 --- .github/workflows/test.yml | 2 +- pandas-stubs/_libs/tslibs/timestamps.pyi | 13 +- pyproject.toml | 3 +- tests/__init__.py | 14 +- tests/test_frame.py | 43 +- tests/test_io.py | 758 +++++++++++++---------- tests/test_utility.py | 15 +- 7 files changed, 479 insertions(+), 369 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 740540c08..b233de81b 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.9', '3.10', '3.11'] + python-version: ['3.9', '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v3 diff --git a/pandas-stubs/_libs/tslibs/timestamps.pyi b/pandas-stubs/_libs/tslibs/timestamps.pyi index dbf151c7b..a8bf36b87 100644 --- a/pandas-stubs/_libs/tslibs/timestamps.pyi +++ b/pandas-stubs/_libs/tslibs/timestamps.pyi @@ -6,6 +6,7 @@ from datetime import ( timedelta, tzinfo as _tzinfo, ) +import sys from time import struct_time from typing import ( ClassVar, @@ -99,8 +100,16 @@ class Timestamp(datetime): def tz(self) -> _tzinfo | None: ... @property def fold(self) -> int: ... - @classmethod - def fromtimestamp(cls, t: float, tz: _tzinfo | str | None = ...) -> Self: ... + + if sys.version_info < (3, 12): + @classmethod + def fromtimestamp(cls, t: float, tz: _tzinfo | str | None = ...) -> Self: ... + else: + @classmethod + def fromtimestamp( # pyright: ignore[reportIncompatibleMethodOverride] + cls, t: float, tz: _tzinfo | str | None = ... + ) -> Self: ... + @classmethod def utcfromtimestamp(cls, ts: float) -> Self: ... @classmethod diff --git a/pyproject.toml b/pyproject.toml index 0100907db..f6f94145d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ types-pytz = ">= 2022.1.1" numpy = { version = ">=1.26.0", python = "<3.13" } [tool.poetry.group.dev.dependencies] -mypy = "1.7.1" +mypy = "1.8.0" pandas = "2.1.4" pyarrow = ">=10.0.1" pytest = ">=7.1.2" @@ -61,7 +61,6 @@ jinja2 = ">=3.1" scipy = { version = ">=1.9.1", python = "<3.13" } SQLAlchemy = ">=2.0.12" types-python-dateutil = ">=2.8.19" -numexpr = "<2.8.5" # https://github.com/pandas-dev/pandas/issues/54449 beautifulsoup4 = ">=4.12.2" html5lib = ">=1.1" diff --git a/tests/__init__.py b/tests/__init__.py index 847f7f52a..867d7f59e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -7,23 +7,29 @@ ) import os import platform +import sys from typing import ( TYPE_CHECKING, Final, + TypeVar, ) -import pandas as pd -from pandas.util.version import Version import pytest -from pandas._typing import T +if sys.version_info < (3, 12): + import pandas as pd +else: + with pytest.warns(DeprecationWarning, match="datetime.datetime.utcfromtimestamp"): + import pandas as pd +from pandas.util.version import Version +_T = TypeVar("_T") TYPE_CHECKING_INVALID_USAGE: Final = TYPE_CHECKING WINDOWS = os.name == "nt" or "cygwin" in platform.system().lower() PD_LTE_21 = Version(pd.__version__) < Version("2.1.999") -def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left") -> T: +def check(actual: _T, klass: type, dtype: type | None = None, attr: str = "left") -> _T: if not isinstance(actual, klass): raise RuntimeError(f"Expected type '{klass}' but got '{type(actual)}'") if dtype is None: diff --git a/tests/test_frame.py b/tests/test_frame.py index a25e9b683..9a9ea25a0 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -14,6 +14,7 @@ import io import itertools from pathlib import Path +import platform import string from typing import ( TYPE_CHECKING, @@ -1821,24 +1822,30 @@ def test_frame_getitem_isin() -> None: def test_to_excel() -> None: df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl") - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(Path(path), engine="openpyxl") - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl", startrow=1, startcol=1, header=False) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl", sheet_name="sheet", index=False) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl", header=["x", "y"]) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl", columns=["col1"]) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl") + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(Path(path), engine="openpyxl") + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl", startrow=1, startcol=1, header=False) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl", sheet_name="sheet", index=False) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl", header=["x", "y"]) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl", columns=["col1"]) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) def test_join() -> None: diff --git a/tests/test_io.py b/tests/test_io.py index 71c069e29..ab7ccfa9c 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -5,6 +5,7 @@ import os.path import pathlib from pathlib import Path +import platform import sqlite3 from typing import ( TYPE_CHECKING, @@ -897,233 +898,293 @@ def test_to_csv_series(): def test_read_excel() -> None: - with ensure_clean(".xlsx") as path: - # https://github.com/pandas-dev/pandas-stubs/pull/33 - check( - assert_type(pd.DataFrame({"A": [1, 2, 3]}).to_excel(path), None), type(None) - ) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - check( - assert_type(pd.read_excel(path, sheet_name="Sheet1"), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel(path, sheet_name=["Sheet1"]), - dict[str, pd.DataFrame], - ), - dict, - ) - # GH 98 - check( - assert_type(pd.read_excel(path, sheet_name=0), pd.DataFrame), pd.DataFrame - ) - check( - assert_type(pd.read_excel(path, sheet_name=[0]), dict[int, pd.DataFrame]), - dict, - ) - check( - assert_type( - pd.read_excel(path, sheet_name=[0, "Sheet1"]), - dict[Union[int, str], pd.DataFrame], - ), - dict, - ) - # GH 641 - check( - assert_type( - pd.read_excel(path, sheet_name=None), - dict[str, pd.DataFrame], - ), - dict, - ) - check( - assert_type(pd.read_excel(path, names=("test",), header=0), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, names=(1,), header=0), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel(path, names=(("higher", "lower"),), header=0), + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean(".xlsx") as path: + # https://github.com/pandas-dev/pandas-stubs/pull/33 + check( + assert_type(pd.DataFrame({"A": [1, 2, 3]}).to_excel(path), None), + type(None), + ) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + check( + assert_type(pd.read_excel(path, sheet_name="Sheet1"), pd.DataFrame), pd.DataFrame, - ), - pd.DataFrame, - ), - check( - assert_type(pd.read_excel(path, names=range(1), header=0), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=None), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=["A"]), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=(0,)), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=range(1)), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=_true_if_b), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel( - path, - names=[1, 2], - usecols=_true_if_greater_than_0, - header=0, - index_col=0, + ) + check( + assert_type( + pd.read_excel(path, sheet_name=["Sheet1"]), + dict[str, pd.DataFrame], ), + dict, + ) + # GH 98 + check( + assert_type(pd.read_excel(path, sheet_name=0), pd.DataFrame), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel( - path, - names=(("head", 1), ("tail", 2)), - usecols=_true_if_first_param_is_head, - header=0, - index_col=0, + ) + check( + assert_type( + pd.read_excel(path, sheet_name=[0]), dict[int, pd.DataFrame] + ), + dict, + ) + check( + assert_type( + pd.read_excel(path, sheet_name=[0, "Sheet1"]), + dict[Union[int, str], pd.DataFrame], + ), + dict, + ) + # GH 641 + check( + assert_type( + pd.read_excel(path, sheet_name=None), + dict[str, pd.DataFrame], + ), + dict, + ) + check( + assert_type( + pd.read_excel(path, names=("test",), header=0), pd.DataFrame ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel( - path, - usecols="A", + ) + check( + assert_type(pd.read_excel(path, names=(1,), header=0), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel(path, names=(("higher", "lower"),), header=0), + pd.DataFrame, ), pd.DataFrame, ), - pd.DataFrame, - ) - if TYPE_CHECKING_INVALID_USAGE: - pd.read_excel(path, names="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues] + check( + assert_type( + pd.read_excel(path, names=range(1), header=0), pd.DataFrame + ), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=None), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=["A"]), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=(0,)), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=range(1)), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=_true_if_b), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel( + path, + names=[1, 2], + usecols=_true_if_greater_than_0, + header=0, + index_col=0, + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel( + path, + names=(("head", 1), ("tail", 2)), + usecols=_true_if_first_param_is_head, + header=0, + index_col=0, + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel( + path, + usecols="A", + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + if TYPE_CHECKING_INVALID_USAGE: + pd.read_excel(path, names="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues] def test_read_excel_io_types() -> None: # GH 195 df = pd.DataFrame([[1, 2], [8, 9]], columns=["A", "B"]) - with ensure_clean(".xlsx") as path: - as_str: str = path - df.to_excel(path) + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean(".xlsx") as path: + as_str: str = path + df.to_excel(path) - check(assert_type(pd.read_excel(as_str), pd.DataFrame), pd.DataFrame) + check(assert_type(pd.read_excel(as_str), pd.DataFrame), pd.DataFrame) - as_path = Path(as_str) - check(assert_type(pd.read_excel(as_path), pd.DataFrame), pd.DataFrame) + as_path = Path(as_str) + check(assert_type(pd.read_excel(as_path), pd.DataFrame), pd.DataFrame) - with as_path.open("rb") as as_file: - check(assert_type(pd.read_excel(as_file), pd.DataFrame), pd.DataFrame) + with as_path.open("rb") as as_file: + check(assert_type(pd.read_excel(as_file), pd.DataFrame), pd.DataFrame) def test_read_excel_basic(): - with ensure_clean(".xlsx") as path: - check(assert_type(DF.to_excel(path), None), type(None)) - check(assert_type(read_excel(path), DataFrame), DataFrame) - check(assert_type(read_excel(path, sheet_name="Sheet1"), DataFrame), DataFrame) - check(assert_type(read_excel(path, sheet_name=0), DataFrame), DataFrame) + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean(".xlsx") as path: + check(assert_type(DF.to_excel(path), None), type(None)) + check(assert_type(read_excel(path), DataFrame), DataFrame) + check( + assert_type(read_excel(path, sheet_name="Sheet1"), DataFrame), DataFrame + ) + check(assert_type(read_excel(path, sheet_name=0), DataFrame), DataFrame) def test_read_excel_list(): - with ensure_clean(".xlsx") as path: - check(assert_type(DF.to_excel(path), None), type(None)) - check( - assert_type( - read_excel(path, sheet_name=["Sheet1"]), - dict[str, DataFrame], - ), - dict, - ) - check( - assert_type(read_excel(path, sheet_name=[0]), dict[int, DataFrame]), - dict, - ) + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean(".xlsx") as path: + check(assert_type(DF.to_excel(path), None), type(None)) + check( + assert_type( + read_excel(path, sheet_name=["Sheet1"]), + dict[str, DataFrame], + ), + dict, + ) + check( + assert_type(read_excel(path, sheet_name=[0]), dict[int, DataFrame]), + dict, + ) def test_read_excel_dtypes(): # GH 440 df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"], "c": [10.0, 20.0, 30.3]}) - with ensure_clean(".xlsx") as path: - check(assert_type(df.to_excel(path), None), type(None)) - dtypes = {"a": np.int64, "b": str, "c": np.float64} - check(assert_type(read_excel(path, dtype=dtypes), pd.DataFrame), pd.DataFrame) + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean(".xlsx") as path: + check(assert_type(df.to_excel(path), None), type(None)) + dtypes = {"a": np.int64, "b": str, "c": np.float64} + check( + assert_type(read_excel(path, dtype=dtypes), pd.DataFrame), pd.DataFrame + ) def test_excel_writer(): - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path) as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - check(assert_type(read_excel(path, sheet_name="A"), DataFrame), DataFrame) - check(assert_type(read_excel(path), DataFrame), DataFrame) - ef = pd.ExcelFile(path) - check(assert_type(ef, pd.ExcelFile), pd.ExcelFile) - check(assert_type(read_excel(ef, sheet_name="A"), DataFrame), DataFrame) - 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]), - dict, - ) - check(assert_type(ef.close(), None), type(None)) + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path) as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check(assert_type(read_excel(path, sheet_name="A"), DataFrame), DataFrame) + check(assert_type(read_excel(path), DataFrame), DataFrame) + ef = pd.ExcelFile(path) + check(assert_type(ef, pd.ExcelFile), pd.ExcelFile) + check(assert_type(read_excel(ef, sheet_name="A"), DataFrame), DataFrame) + 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]), + dict, + ) + check(assert_type(ef.close(), None), type(None)) def test_excel_writer_engine(): - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path, engine="auto") as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path, engine="openpyxl") as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - check( - assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), - str, - ) + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="auto") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="openpyxl") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) - with ensure_clean(".ods") as path: - with pd.ExcelWriter(path, engine="odf") as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - check( - assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), - str, - ) + with ensure_clean(".ods") as path: + with pd.ExcelWriter(path, engine="odf") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path, engine="xlsxwriter") as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - check( - assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), - str, - ) + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="xlsxwriter") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) def test_excel_writer_append_mode(): - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path, mode="w") as ew: - DF.to_excel(ew, sheet_name="A") - with pd.ExcelWriter(path, mode="a", engine="openpyxl") as ew: - DF.to_excel(ew, sheet_name="B") + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, mode="w") as ew: + DF.to_excel(ew, sheet_name="A") + with pd.ExcelWriter(path, mode="a", engine="openpyxl") as ew: + DF.to_excel(ew, sheet_name="B") def test_to_string(): @@ -1397,100 +1458,111 @@ def test_read_sql_dtype_backend() -> None: def test_all_read_without_lxml_dtype_backend() -> None: - with ensure_clean() as path: - check(assert_type(DF.to_csv(path), None), type(None)) - s1 = read_csv(path, iterator=True, dtype_backend="pyarrow") - check(assert_type(s1, TextFileReader), TextFileReader) - s1.close() + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean() as path: + check(assert_type(DF.to_csv(path), None), type(None)) + s1 = read_csv(path, iterator=True, dtype_backend="pyarrow") + check(assert_type(s1, TextFileReader), TextFileReader) + s1.close() - DF.to_string(path, index=False) - check( - assert_type(read_fwf(path, dtype_backend="pyarrow"), DataFrame), DataFrame - ) + DF.to_string(path, index=False) + check( + assert_type(read_fwf(path, dtype_backend="pyarrow"), DataFrame), + DataFrame, + ) - check(assert_type(DF.to_json(path), None), type(None)) - check( - assert_type(read_json(path, dtype_backend="pyarrow"), DataFrame), DataFrame - ) - check( - assert_type(read_json(path, dtype={"MatchID": str}), DataFrame), DataFrame - ) + check(assert_type(DF.to_json(path), None), type(None)) + check( + assert_type(read_json(path, dtype_backend="pyarrow"), DataFrame), + DataFrame, + ) + check( + assert_type(read_json(path, dtype={"MatchID": str}), DataFrame), + DataFrame, + ) - with ensure_clean() as path: - con = sqlite3.connect(path) - check(assert_type(DF.to_sql("test", con=con), Union[int, None]), int) - check( - assert_type( - read_sql_query( - "select * from test", - con=con, - index_col="index", - dtype_backend="pyarrow", + with ensure_clean() as path: + con = sqlite3.connect(path) + check(assert_type(DF.to_sql("test", con=con), Union[int, None]), int) + check( + assert_type( + read_sql_query( + "select * from test", + con=con, + index_col="index", + dtype_backend="pyarrow", + ), + DataFrame, ), DataFrame, - ), - DataFrame, - ) - con.close() + ) + con.close() - if not WINDOWS: - check(assert_type(DF.to_orc(path), None), type(None)) + if not WINDOWS: + check(assert_type(DF.to_orc(path), None), type(None)) + with pytest_warns_bounded( + DeprecationWarning, + "make_block is deprecated and will be removed", + lower="2.1.99", + ): + check( + assert_type( + read_orc(path, dtype_backend="numpy_nullable"), DataFrame + ), + DataFrame, + ) + check(assert_type(DF.to_feather(path), None), type(None)) with pytest_warns_bounded( DeprecationWarning, "make_block is deprecated and will be removed", lower="2.1.99", + ): + check( + assert_type(read_feather(path, dtype_backend="pyarrow"), DataFrame), + DataFrame, + ) + + with pytest_warns_bounded( + FutureWarning, "errors='ignore' is deprecated", lower="2.1.99" ): check( assert_type( - read_orc(path, dtype_backend="numpy_nullable"), DataFrame + pd.to_numeric( + [1.0, 2.0, "blerg"], + errors="ignore", + dtype_backend="numpy_nullable", + ), + npt.NDArray, ), - DataFrame, + np.ndarray, ) - check(assert_type(DF.to_feather(path), None), type(None)) - with pytest_warns_bounded( - DeprecationWarning, - "make_block is deprecated and will be removed", - lower="2.1.99", - ): - check( - assert_type(read_feather(path, dtype_backend="pyarrow"), DataFrame), - DataFrame, - ) - with pytest_warns_bounded( - FutureWarning, "errors='ignore' is deprecated", lower="2.1.99" - ): + with ensure_clean(".xlsx") as path: + as_str: str = path + DF.to_excel(path) check( assert_type( - pd.to_numeric( - [1.0, 2.0, "blerg"], - errors="ignore", - dtype_backend="numpy_nullable", - ), - npt.NDArray, + pd.read_excel(as_str, dtype_backend="pyarrow"), pd.DataFrame ), - np.ndarray, + pd.DataFrame, ) - with ensure_clean(".xlsx") as path: - as_str: str = path - DF.to_excel(path) + try: + DF.to_clipboard() + except errors.PyperclipException: + pytest.skip("clipboard not available for testing") check( - assert_type(pd.read_excel(as_str, dtype_backend="pyarrow"), pd.DataFrame), - pd.DataFrame, + assert_type( + read_clipboard(iterator=True, dtype_backend="pyarrow"), TextFileReader + ), + TextFileReader, ) - try: - DF.to_clipboard() - except errors.PyperclipException: - pytest.skip("clipboard not available for testing") - check( - assert_type( - read_clipboard(iterator=True, dtype_backend="pyarrow"), TextFileReader - ), - TextFileReader, - ) - if TYPE_CHECKING: # sqlite3 doesn't support read_table, which is required for this function # Could only run in pytest if SQLAlchemy was installed @@ -1541,98 +1613,106 @@ def test_read_sql_dict_str_value_dtype() -> None: def test_added_date_format() -> None: - with ensure_clean() as path: - df_dates = pd.DataFrame( - data={ - "col1": ["2023-03-15", "2023-04-20"], - } - ) - df_dates.to_csv(path) + with pytest_warns_bounded( + DeprecationWarning, + match="datetime.datetime.utcnow", + lower="3.11.99", + version_str=platform.python_version(), + ): + with ensure_clean() as path: + df_dates = pd.DataFrame( + data={ + "col1": ["2023-03-15", "2023-04-20"], + } + ) + df_dates.to_csv(path) - check( - assert_type( - pd.read_table( - path, sep=",", parse_dates=["col1"], date_format="%Y-%m-%d" + check( + assert_type( + pd.read_table( + path, sep=",", parse_dates=["col1"], date_format="%Y-%m-%d" + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.read_table( - path, - sep=",", - parse_dates=["col1"], - date_format={"col1": "%Y-%m-%d"}, + ) + check( + assert_type( + pd.read_table( + path, + sep=",", + parse_dates=["col1"], + date_format={"col1": "%Y-%m-%d"}, + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.read_table( - path, sep=",", parse_dates=["col1"], date_format={0: "%Y-%m-%d"} + ) + check( + assert_type( + pd.read_table( + path, sep=",", parse_dates=["col1"], date_format={0: "%Y-%m-%d"} + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) + ) - check( - assert_type( - pd.read_fwf(path, date_format="%Y-%m-%d"), + check( + assert_type( + pd.read_fwf(path, date_format="%Y-%m-%d"), + pd.DataFrame, + ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.read_fwf(path, date_format={"col1": "%Y-%m-%d"}), + ) + check( + assert_type( + pd.read_fwf(path, date_format={"col1": "%Y-%m-%d"}), + pd.DataFrame, + ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.read_fwf(path, date_format={0: "%Y-%m-%d"}), + ) + check( + assert_type( + pd.read_fwf(path, date_format={0: "%Y-%m-%d"}), + pd.DataFrame, + ), pd.DataFrame, - ), - pd.DataFrame, - ) - with ensure_clean(".xlsx") as path: - check( - assert_type( - pd.DataFrame( - data={ - "col1": ["2023-03-15", "2023-04-20"], - } - ).to_excel(path), - None, - ), - type(None), - ) - check( - assert_type( - pd.read_excel(path, parse_dates=["col1"], date_format={0: "%Y-%m-%d"}), + ) + with ensure_clean(".xlsx") as path: + check( + assert_type( + pd.DataFrame( + data={ + "col1": ["2023-03-15", "2023-04-20"], + } + ).to_excel(path), + None, + ), + type(None), + ) + check( + assert_type( + pd.read_excel( + path, parse_dates=["col1"], date_format={0: "%Y-%m-%d"} + ), + pd.DataFrame, + ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel( - path, parse_dates=["col1"], date_format={"col1": "%Y-%m-%d"} + ) + check( + assert_type( + pd.read_excel( + path, parse_dates=["col1"], date_format={"col1": "%Y-%m-%d"} + ), + pd.DataFrame, ), pd.DataFrame, - ), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel(path, parse_dates=["col1"], date_format="%Y-%m-%d"), + ) + check( + assert_type( + pd.read_excel(path, parse_dates=["col1"], date_format="%Y-%m-%d"), + pd.DataFrame, + ), pd.DataFrame, - ), - pd.DataFrame, - ) + ) diff --git a/tests/test_utility.py b/tests/test_utility.py index 9a27f822c..70ae19034 100644 --- a/tests/test_utility.py +++ b/tests/test_utility.py @@ -1,12 +1,21 @@ +import platform + import pandas as pd -import pytest from typing_extensions import assert_type -from tests import check +from tests import ( + check, + pytest_warns_bounded, +) def test_show_version(): - with pytest.warns(UserWarning, match="Setuptools is replacing distutils"): + with pytest_warns_bounded( + UserWarning, + match="Setuptools is replacing distutils", + upper="3.11.99", + version_str=platform.python_version(), + ): check(assert_type(pd.show_versions(True), None), type(None)) check(assert_type(pd.show_versions(False), None), type(None)) From 29afb64979f8e7595aec297b353d50e74d3b6b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sat, 23 Dec 2023 20:23:35 -0500 Subject: [PATCH 2/3] simpler solution --- pyproject.toml | 8 + scripts/test/run.py | 4 +- tests/__init__.py | 14 +- tests/test_frame.py | 43 ++- tests/test_io.py | 758 ++++++++++++++++++++------------------------ 5 files changed, 371 insertions(+), 456 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f6f94145d..223bdf780 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -197,6 +197,14 @@ useLibraryCodeForTypes = false [tool.codespell] ignore-words-list = "indext, mose, sav, ser" +[tool.pytest.ini_options] +filterwarnings = [ + # treat warnings as errors + "error", + # until there is a new dateutil release: github.com/dateutil/dateutil/pull/1285 + "ignore:datetime.datetime.utc:DeprecationWarning", +] + # Next line needed to avoid poetry complaint [tool.setuptools_scm] diff --git a/scripts/test/run.py b/scripts/test/run.py index e08b41a14..c30395da9 100644 --- a/scripts/test/run.py +++ b/scripts/test/run.py @@ -15,8 +15,8 @@ def pyright_src(): subprocess.run(cmd, check=True) -def pytest(flags: tuple[str, ...] = ("-Werror",)): - cmd = ["pytest", "--cache-clear", *flags] +def pytest(): + cmd = ["pytest", "--cache-clear"] subprocess.run(cmd, check=True) diff --git a/tests/__init__.py b/tests/__init__.py index 867d7f59e..847f7f52a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -7,29 +7,23 @@ ) import os import platform -import sys from typing import ( TYPE_CHECKING, Final, - TypeVar, ) +import pandas as pd +from pandas.util.version import Version import pytest -if sys.version_info < (3, 12): - import pandas as pd -else: - with pytest.warns(DeprecationWarning, match="datetime.datetime.utcfromtimestamp"): - import pandas as pd -from pandas.util.version import Version +from pandas._typing import T -_T = TypeVar("_T") TYPE_CHECKING_INVALID_USAGE: Final = TYPE_CHECKING WINDOWS = os.name == "nt" or "cygwin" in platform.system().lower() PD_LTE_21 = Version(pd.__version__) < Version("2.1.999") -def check(actual: _T, klass: type, dtype: type | None = None, attr: str = "left") -> _T: +def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left") -> T: if not isinstance(actual, klass): raise RuntimeError(f"Expected type '{klass}' but got '{type(actual)}'") if dtype is None: diff --git a/tests/test_frame.py b/tests/test_frame.py index 9a9ea25a0..a25e9b683 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -14,7 +14,6 @@ import io import itertools from pathlib import Path -import platform import string from typing import ( TYPE_CHECKING, @@ -1822,30 +1821,24 @@ def test_frame_getitem_isin() -> None: def test_to_excel() -> None: df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}) - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl") - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(Path(path), engine="openpyxl") - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl", startrow=1, startcol=1, header=False) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl", sheet_name="sheet", index=False) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl", header=["x", "y"]) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - with ensure_clean() as path: - df.to_excel(path, engine="openpyxl", columns=["col1"]) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl") + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(Path(path), engine="openpyxl") + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl", startrow=1, startcol=1, header=False) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl", sheet_name="sheet", index=False) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl", header=["x", "y"]) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + with ensure_clean() as path: + df.to_excel(path, engine="openpyxl", columns=["col1"]) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) def test_join() -> None: diff --git a/tests/test_io.py b/tests/test_io.py index ab7ccfa9c..71c069e29 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -5,7 +5,6 @@ import os.path import pathlib from pathlib import Path -import platform import sqlite3 from typing import ( TYPE_CHECKING, @@ -898,293 +897,233 @@ def test_to_csv_series(): def test_read_excel() -> None: - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean(".xlsx") as path: - # https://github.com/pandas-dev/pandas-stubs/pull/33 - check( - assert_type(pd.DataFrame({"A": [1, 2, 3]}).to_excel(path), None), - type(None), - ) - check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) - check( - assert_type(pd.read_excel(path, sheet_name="Sheet1"), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel(path, sheet_name=["Sheet1"]), - dict[str, pd.DataFrame], - ), - dict, - ) - # GH 98 - check( - assert_type(pd.read_excel(path, sheet_name=0), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel(path, sheet_name=[0]), dict[int, pd.DataFrame] - ), - dict, - ) - check( - assert_type( - pd.read_excel(path, sheet_name=[0, "Sheet1"]), - dict[Union[int, str], pd.DataFrame], - ), - dict, - ) - # GH 641 - check( - assert_type( - pd.read_excel(path, sheet_name=None), - dict[str, pd.DataFrame], - ), - dict, - ) - check( - assert_type( - pd.read_excel(path, names=("test",), header=0), pd.DataFrame - ), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, names=(1,), header=0), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type( - pd.read_excel(path, names=(("higher", "lower"),), header=0), - pd.DataFrame, - ), - pd.DataFrame, + with ensure_clean(".xlsx") as path: + # https://github.com/pandas-dev/pandas-stubs/pull/33 + check( + assert_type(pd.DataFrame({"A": [1, 2, 3]}).to_excel(path), None), type(None) + ) + check(assert_type(pd.read_excel(path), pd.DataFrame), pd.DataFrame) + check( + assert_type(pd.read_excel(path, sheet_name="Sheet1"), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel(path, sheet_name=["Sheet1"]), + dict[str, pd.DataFrame], ), - check( - assert_type( - pd.read_excel(path, names=range(1), header=0), pd.DataFrame - ), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=None), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=["A"]), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=(0,)), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=range(1)), pd.DataFrame), - pd.DataFrame, - ) - check( - assert_type(pd.read_excel(path, usecols=_true_if_b), pd.DataFrame), + dict, + ) + # GH 98 + check( + assert_type(pd.read_excel(path, sheet_name=0), pd.DataFrame), pd.DataFrame + ) + check( + assert_type(pd.read_excel(path, sheet_name=[0]), dict[int, pd.DataFrame]), + dict, + ) + check( + assert_type( + pd.read_excel(path, sheet_name=[0, "Sheet1"]), + dict[Union[int, str], pd.DataFrame], + ), + dict, + ) + # GH 641 + check( + assert_type( + pd.read_excel(path, sheet_name=None), + dict[str, pd.DataFrame], + ), + dict, + ) + check( + assert_type(pd.read_excel(path, names=("test",), header=0), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, names=(1,), header=0), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel(path, names=(("higher", "lower"),), header=0), pd.DataFrame, - ) - check( - assert_type( - pd.read_excel( - path, - names=[1, 2], - usecols=_true_if_greater_than_0, - header=0, - index_col=0, - ), - pd.DataFrame, + ), + pd.DataFrame, + ), + check( + assert_type(pd.read_excel(path, names=range(1), header=0), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=None), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=["A"]), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=(0,)), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=range(1)), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type(pd.read_excel(path, usecols=_true_if_b), pd.DataFrame), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel( + path, + names=[1, 2], + usecols=_true_if_greater_than_0, + header=0, + index_col=0, ), pd.DataFrame, - ) - check( - assert_type( - pd.read_excel( - path, - names=(("head", 1), ("tail", 2)), - usecols=_true_if_first_param_is_head, - header=0, - index_col=0, - ), - pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel( + path, + names=(("head", 1), ("tail", 2)), + usecols=_true_if_first_param_is_head, + header=0, + index_col=0, ), pd.DataFrame, - ) - check( - assert_type( - pd.read_excel( - path, - usecols="A", - ), - pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel( + path, + usecols="A", ), pd.DataFrame, - ) - if TYPE_CHECKING_INVALID_USAGE: - pd.read_excel(path, names="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues] + ), + pd.DataFrame, + ) + if TYPE_CHECKING_INVALID_USAGE: + pd.read_excel(path, names="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues] def test_read_excel_io_types() -> None: # GH 195 df = pd.DataFrame([[1, 2], [8, 9]], columns=["A", "B"]) - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean(".xlsx") as path: - as_str: str = path - df.to_excel(path) + with ensure_clean(".xlsx") as path: + as_str: str = path + df.to_excel(path) - check(assert_type(pd.read_excel(as_str), pd.DataFrame), pd.DataFrame) + check(assert_type(pd.read_excel(as_str), pd.DataFrame), pd.DataFrame) - as_path = Path(as_str) - check(assert_type(pd.read_excel(as_path), pd.DataFrame), pd.DataFrame) + as_path = Path(as_str) + check(assert_type(pd.read_excel(as_path), pd.DataFrame), pd.DataFrame) - with as_path.open("rb") as as_file: - check(assert_type(pd.read_excel(as_file), pd.DataFrame), pd.DataFrame) + with as_path.open("rb") as as_file: + check(assert_type(pd.read_excel(as_file), pd.DataFrame), pd.DataFrame) def test_read_excel_basic(): - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean(".xlsx") as path: - check(assert_type(DF.to_excel(path), None), type(None)) - check(assert_type(read_excel(path), DataFrame), DataFrame) - check( - assert_type(read_excel(path, sheet_name="Sheet1"), DataFrame), DataFrame - ) - check(assert_type(read_excel(path, sheet_name=0), DataFrame), DataFrame) + with ensure_clean(".xlsx") as path: + check(assert_type(DF.to_excel(path), None), type(None)) + check(assert_type(read_excel(path), DataFrame), DataFrame) + check(assert_type(read_excel(path, sheet_name="Sheet1"), DataFrame), DataFrame) + check(assert_type(read_excel(path, sheet_name=0), DataFrame), DataFrame) def test_read_excel_list(): - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean(".xlsx") as path: - check(assert_type(DF.to_excel(path), None), type(None)) - check( - assert_type( - read_excel(path, sheet_name=["Sheet1"]), - dict[str, DataFrame], - ), - dict, - ) - check( - assert_type(read_excel(path, sheet_name=[0]), dict[int, DataFrame]), - dict, - ) + with ensure_clean(".xlsx") as path: + check(assert_type(DF.to_excel(path), None), type(None)) + check( + assert_type( + read_excel(path, sheet_name=["Sheet1"]), + dict[str, DataFrame], + ), + dict, + ) + check( + assert_type(read_excel(path, sheet_name=[0]), dict[int, DataFrame]), + dict, + ) def test_read_excel_dtypes(): # GH 440 df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"], "c": [10.0, 20.0, 30.3]}) - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean(".xlsx") as path: - check(assert_type(df.to_excel(path), None), type(None)) - dtypes = {"a": np.int64, "b": str, "c": np.float64} - check( - assert_type(read_excel(path, dtype=dtypes), pd.DataFrame), pd.DataFrame - ) + with ensure_clean(".xlsx") as path: + check(assert_type(df.to_excel(path), None), type(None)) + dtypes = {"a": np.int64, "b": str, "c": np.float64} + check(assert_type(read_excel(path, dtype=dtypes), pd.DataFrame), pd.DataFrame) def test_excel_writer(): - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path) as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - check(assert_type(read_excel(path, sheet_name="A"), DataFrame), DataFrame) - check(assert_type(read_excel(path), DataFrame), DataFrame) - ef = pd.ExcelFile(path) - check(assert_type(ef, pd.ExcelFile), pd.ExcelFile) - check(assert_type(read_excel(ef, sheet_name="A"), DataFrame), DataFrame) - 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]), - dict, - ) - check(assert_type(ef.close(), None), type(None)) + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path) as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check(assert_type(read_excel(path, sheet_name="A"), DataFrame), DataFrame) + check(assert_type(read_excel(path), DataFrame), DataFrame) + ef = pd.ExcelFile(path) + check(assert_type(ef, pd.ExcelFile), pd.ExcelFile) + check(assert_type(read_excel(ef, sheet_name="A"), DataFrame), DataFrame) + 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]), + dict, + ) + check(assert_type(ef.close(), None), type(None)) def test_excel_writer_engine(): - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path, engine="auto") as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path, engine="openpyxl") as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - check( - assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), - str, - ) + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="auto") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="openpyxl") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) - with ensure_clean(".ods") as path: - with pd.ExcelWriter(path, engine="odf") as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - check( - assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), - str, - ) + with ensure_clean(".ods") as path: + with pd.ExcelWriter(path, engine="odf") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path, engine="xlsxwriter") as ew: - check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) - DF.to_excel(ew, sheet_name="A") - check( - assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), - str, - ) + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="xlsxwriter") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) def test_excel_writer_append_mode(): - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean(".xlsx") as path: - with pd.ExcelWriter(path, mode="w") as ew: - DF.to_excel(ew, sheet_name="A") - with pd.ExcelWriter(path, mode="a", engine="openpyxl") as ew: - DF.to_excel(ew, sheet_name="B") + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, mode="w") as ew: + DF.to_excel(ew, sheet_name="A") + with pd.ExcelWriter(path, mode="a", engine="openpyxl") as ew: + DF.to_excel(ew, sheet_name="B") def test_to_string(): @@ -1458,111 +1397,100 @@ def test_read_sql_dtype_backend() -> None: def test_all_read_without_lxml_dtype_backend() -> None: - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean() as path: - check(assert_type(DF.to_csv(path), None), type(None)) - s1 = read_csv(path, iterator=True, dtype_backend="pyarrow") - check(assert_type(s1, TextFileReader), TextFileReader) - s1.close() + with ensure_clean() as path: + check(assert_type(DF.to_csv(path), None), type(None)) + s1 = read_csv(path, iterator=True, dtype_backend="pyarrow") + check(assert_type(s1, TextFileReader), TextFileReader) + s1.close() - DF.to_string(path, index=False) - check( - assert_type(read_fwf(path, dtype_backend="pyarrow"), DataFrame), - DataFrame, - ) + DF.to_string(path, index=False) + check( + assert_type(read_fwf(path, dtype_backend="pyarrow"), DataFrame), DataFrame + ) - check(assert_type(DF.to_json(path), None), type(None)) - check( - assert_type(read_json(path, dtype_backend="pyarrow"), DataFrame), - DataFrame, - ) - check( - assert_type(read_json(path, dtype={"MatchID": str}), DataFrame), - DataFrame, - ) + check(assert_type(DF.to_json(path), None), type(None)) + check( + assert_type(read_json(path, dtype_backend="pyarrow"), DataFrame), DataFrame + ) + check( + assert_type(read_json(path, dtype={"MatchID": str}), DataFrame), DataFrame + ) - with ensure_clean() as path: - con = sqlite3.connect(path) - check(assert_type(DF.to_sql("test", con=con), Union[int, None]), int) - check( - assert_type( - read_sql_query( - "select * from test", - con=con, - index_col="index", - dtype_backend="pyarrow", - ), - DataFrame, + with ensure_clean() as path: + con = sqlite3.connect(path) + check(assert_type(DF.to_sql("test", con=con), Union[int, None]), int) + check( + assert_type( + read_sql_query( + "select * from test", + con=con, + index_col="index", + dtype_backend="pyarrow", ), DataFrame, - ) - con.close() + ), + DataFrame, + ) + con.close() - if not WINDOWS: - check(assert_type(DF.to_orc(path), None), type(None)) - with pytest_warns_bounded( - DeprecationWarning, - "make_block is deprecated and will be removed", - lower="2.1.99", - ): - check( - assert_type( - read_orc(path, dtype_backend="numpy_nullable"), DataFrame - ), - DataFrame, - ) - check(assert_type(DF.to_feather(path), None), type(None)) + if not WINDOWS: + check(assert_type(DF.to_orc(path), None), type(None)) with pytest_warns_bounded( DeprecationWarning, "make_block is deprecated and will be removed", lower="2.1.99", - ): - check( - assert_type(read_feather(path, dtype_backend="pyarrow"), DataFrame), - DataFrame, - ) - - with pytest_warns_bounded( - FutureWarning, "errors='ignore' is deprecated", lower="2.1.99" ): check( assert_type( - pd.to_numeric( - [1.0, 2.0, "blerg"], - errors="ignore", - dtype_backend="numpy_nullable", - ), - npt.NDArray, + read_orc(path, dtype_backend="numpy_nullable"), DataFrame ), - np.ndarray, + DataFrame, ) + check(assert_type(DF.to_feather(path), None), type(None)) + with pytest_warns_bounded( + DeprecationWarning, + "make_block is deprecated and will be removed", + lower="2.1.99", + ): + check( + assert_type(read_feather(path, dtype_backend="pyarrow"), DataFrame), + DataFrame, + ) - with ensure_clean(".xlsx") as path: - as_str: str = path - DF.to_excel(path) + with pytest_warns_bounded( + FutureWarning, "errors='ignore' is deprecated", lower="2.1.99" + ): check( assert_type( - pd.read_excel(as_str, dtype_backend="pyarrow"), pd.DataFrame + pd.to_numeric( + [1.0, 2.0, "blerg"], + errors="ignore", + dtype_backend="numpy_nullable", + ), + npt.NDArray, ), - pd.DataFrame, + np.ndarray, ) - try: - DF.to_clipboard() - except errors.PyperclipException: - pytest.skip("clipboard not available for testing") + with ensure_clean(".xlsx") as path: + as_str: str = path + DF.to_excel(path) check( - assert_type( - read_clipboard(iterator=True, dtype_backend="pyarrow"), TextFileReader - ), - TextFileReader, + assert_type(pd.read_excel(as_str, dtype_backend="pyarrow"), pd.DataFrame), + pd.DataFrame, ) + try: + DF.to_clipboard() + except errors.PyperclipException: + pytest.skip("clipboard not available for testing") + check( + assert_type( + read_clipboard(iterator=True, dtype_backend="pyarrow"), TextFileReader + ), + TextFileReader, + ) + if TYPE_CHECKING: # sqlite3 doesn't support read_table, which is required for this function # Could only run in pytest if SQLAlchemy was installed @@ -1613,106 +1541,98 @@ def test_read_sql_dict_str_value_dtype() -> None: def test_added_date_format() -> None: - with pytest_warns_bounded( - DeprecationWarning, - match="datetime.datetime.utcnow", - lower="3.11.99", - version_str=platform.python_version(), - ): - with ensure_clean() as path: - df_dates = pd.DataFrame( - data={ - "col1": ["2023-03-15", "2023-04-20"], - } - ) - df_dates.to_csv(path) + with ensure_clean() as path: + df_dates = pd.DataFrame( + data={ + "col1": ["2023-03-15", "2023-04-20"], + } + ) + df_dates.to_csv(path) - check( - assert_type( - pd.read_table( - path, sep=",", parse_dates=["col1"], date_format="%Y-%m-%d" - ), - pd.DataFrame, + check( + assert_type( + pd.read_table( + path, sep=",", parse_dates=["col1"], date_format="%Y-%m-%d" ), pd.DataFrame, - ) - check( - assert_type( - pd.read_table( - path, - sep=",", - parse_dates=["col1"], - date_format={"col1": "%Y-%m-%d"}, - ), - pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_table( + path, + sep=",", + parse_dates=["col1"], + date_format={"col1": "%Y-%m-%d"}, ), pd.DataFrame, - ) - check( - assert_type( - pd.read_table( - path, sep=",", parse_dates=["col1"], date_format={0: "%Y-%m-%d"} - ), - pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_table( + path, sep=",", parse_dates=["col1"], date_format={0: "%Y-%m-%d"} ), pd.DataFrame, - ) + ), + pd.DataFrame, + ) - check( - assert_type( - pd.read_fwf(path, date_format="%Y-%m-%d"), - pd.DataFrame, - ), + check( + assert_type( + pd.read_fwf(path, date_format="%Y-%m-%d"), pd.DataFrame, - ) - check( - assert_type( - pd.read_fwf(path, date_format={"col1": "%Y-%m-%d"}), - pd.DataFrame, - ), + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_fwf(path, date_format={"col1": "%Y-%m-%d"}), pd.DataFrame, - ) - check( - assert_type( - pd.read_fwf(path, date_format={0: "%Y-%m-%d"}), - pd.DataFrame, - ), + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_fwf(path, date_format={0: "%Y-%m-%d"}), pd.DataFrame, - ) - with ensure_clean(".xlsx") as path: - check( - assert_type( - pd.DataFrame( - data={ - "col1": ["2023-03-15", "2023-04-20"], - } - ).to_excel(path), - None, - ), - type(None), - ) - check( - assert_type( - pd.read_excel( - path, parse_dates=["col1"], date_format={0: "%Y-%m-%d"} - ), - pd.DataFrame, - ), + ), + pd.DataFrame, + ) + with ensure_clean(".xlsx") as path: + check( + assert_type( + pd.DataFrame( + data={ + "col1": ["2023-03-15", "2023-04-20"], + } + ).to_excel(path), + None, + ), + type(None), + ) + check( + assert_type( + pd.read_excel(path, parse_dates=["col1"], date_format={0: "%Y-%m-%d"}), pd.DataFrame, - ) - check( - assert_type( - pd.read_excel( - path, parse_dates=["col1"], date_format={"col1": "%Y-%m-%d"} - ), - pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel( + path, parse_dates=["col1"], date_format={"col1": "%Y-%m-%d"} ), pd.DataFrame, - ) - check( - assert_type( - pd.read_excel(path, parse_dates=["col1"], date_format="%Y-%m-%d"), - pd.DataFrame, - ), + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel(path, parse_dates=["col1"], date_format="%Y-%m-%d"), pd.DataFrame, - ) + ), + pd.DataFrame, + ) From a87b51bf237d965554620c6ee93b7a96adfbdd57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sat, 23 Dec 2023 20:55:18 -0500 Subject: [PATCH 3/3] fix nightly --- .pre-commit-config.yaml | 4 ++-- tests/test_frame.py | 44 ++++++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d3559f982..35165128e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ ci: autofix_prs: false repos: - repo: https://github.com/python/black - rev: 23.12.0 + rev: 23.12.1 hooks: - id: black - repo: https://github.com/PyCQA/isort @@ -11,7 +11,7 @@ repos: hooks: - id: isort - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.1.8 + rev: v0.1.9 hooks: - id: ruff args: [ diff --git a/tests/test_frame.py b/tests/test_frame.py index bdd9bf491..e3875432f 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -1699,31 +1699,43 @@ class ReadCsvKwargs(TypedDict): pd.DataFrame, ) parse_dates_2 = {"combined_date": ["Year", "Month", "Day"]} - check( - assert_type(pd.read_csv(path, parse_dates=parse_dates_2), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, "Support for nested sequences", lower="2.1.99" + ): + check( + assert_type(pd.read_csv(path, parse_dates=parse_dates_2), pd.DataFrame), + pd.DataFrame, + ) parse_dates_3 = {"combined_date": [1, 2, 3]} - check( - assert_type(pd.read_csv(path, parse_dates=parse_dates_3), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, "Support for nested sequences", lower="2.1.99" + ): + check( + assert_type(pd.read_csv(path, parse_dates=parse_dates_3), pd.DataFrame), + pd.DataFrame, + ) # MyPy calls this Dict[str, object] by default which necessitates the explicit annotation (Pyright does not) parse_dates_4: dict[str, list[str | int]] = {"combined_date": [1, "Month", 3]} - check( - assert_type(pd.read_csv(path, parse_dates=parse_dates_4), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, "Support for nested sequences", lower="2.1.99" + ): + check( + assert_type(pd.read_csv(path, parse_dates=parse_dates_4), pd.DataFrame), + pd.DataFrame, + ) parse_dates_5 = [0] check( assert_type(pd.read_csv(path, parse_dates=parse_dates_5), pd.DataFrame), pd.DataFrame, ) parse_dates_6 = [[1, 2, 3]] - check( - assert_type(pd.read_csv(path, parse_dates=parse_dates_6), pd.DataFrame), - pd.DataFrame, - ) + with pytest_warns_bounded( + FutureWarning, "Support for nested sequences", lower="2.1.99" + ): + check( + assert_type(pd.read_csv(path, parse_dates=parse_dates_6), pd.DataFrame), + pd.DataFrame, + ) def test_groupby_series_methods() -> None: