Skip to content

Commit c6ce53c

Browse files
committed
Merge branch 'future-fix' of github.com:bashtage/pandas-stubs into future-fix
2 parents 8a28c23 + 8b5466b commit c6ce53c

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

tests/__init__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from __future__ import annotations
22

3+
from contextlib import (
4+
AbstractContextManager,
5+
nullcontext,
6+
)
37
import os
48
import platform
59
from typing import (
@@ -9,6 +13,7 @@
913

1014
from packaging.version import parse
1115
import pandas as pd
16+
import pytest
1217

1318
from pandas._typing import T
1419

@@ -32,3 +37,59 @@ def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left")
3237
if not isinstance(value, dtype):
3338
raise RuntimeError(f"Expected type '{dtype}' but got '{type(value)}'")
3439
return actual # type: ignore[return-value]
40+
41+
42+
def pytest_warns_bounded(
43+
warning: type[Warning],
44+
match: str,
45+
lower: str | None = None,
46+
upper: str | None = None,
47+
) -> AbstractContextManager:
48+
"""
49+
Version conditional pytet.warns context manager
50+
51+
Returns a context manager that will raise an error if
52+
the warning is not issued when pandas version is
53+
between the lower and upper version given.
54+
55+
Parameters
56+
----------
57+
warning : type[Warning]
58+
The warning class to check for.
59+
match : str
60+
The string to match in the warning message.
61+
lower : str, optional
62+
The lower bound of the version to check for the warning.
63+
upper : str, optional
64+
The upper bound of the version to check for the warning.
65+
66+
Notes
67+
-----
68+
The lower and upper bounds are exclusive so that a pytest.warns context
69+
manager is returned if lower < pd.__version__ < upper.
70+
71+
Examples
72+
--------
73+
with pytest_warns_bounded(UserWarning, match="foo", lower="1.2.99"):
74+
# Versions 1.3.0 and above will raise an error
75+
# if the warning is not issued
76+
pass
77+
78+
with pytest_warns_bounded(UserWarning, match="foo", upper="1.5.99"):
79+
# Versions 1.6.0 and below will raise an error
80+
# if the warning is not issued
81+
pass
82+
83+
with pytest_warns_bounded(
84+
UserWarning, match="foo", lower="1.2.99", upper="1.5.99"
85+
):
86+
# Versions between 1.3.x and 1.5.x will raise an error
87+
pass
88+
"""
89+
lb = parse("0.0.0") if lower is None else parse(lower)
90+
ub = parse("9999.0.0") if upper is None else parse(upper)
91+
current = parse(pd.__version__)
92+
if lb < current < ub:
93+
return pytest.warns(warning, match=match)
94+
else:
95+
return nullcontext()

tests/test_frame.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
PD_LTE_15,
4040
TYPE_CHECKING_INVALID_USAGE,
4141
check,
42+
pytest_warns_bounded,
4243
)
4344

4445
from pandas.io.formats.style import Styler
@@ -934,16 +935,11 @@ def test_types_describe() -> None:
934935
}
935936
)
936937
df.describe()
937-
if PD_LTE_15:
938-
with pytest.warns(FutureWarning, match="Treating datetime data as categorical"):
939-
df.describe(percentiles=[0.5], include="all")
940-
else:
941-
df.describe(percentiles=[0.5], include="all")
942-
if PD_LTE_15:
943-
with pytest.warns(FutureWarning, match="Treating datetime data as categorical"):
944-
df.describe(exclude=[np.number])
945-
else:
938+
with pytest_warns_bounded(
939+
FutureWarning, match="Treating datetime data as categorical", upper="1.5.999"
940+
):
946941
df.describe(percentiles=[0.5], include="all")
942+
df.describe(exclude=[np.number])
947943
if PD_LTE_15:
948944
# datetime_is_numeric param added in 1.1.0
949945
# https://pandas.pydata.org/docs/whatsnew/v1.1.0.html

tests/test_series.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,6 @@ def test_types_describe() -> None:
649649
s.describe(percentiles=[0.5], include="all")
650650
with pytest.warns(DeprecationWarning, match="elementwise comparison failed"):
651651
s.describe(exclude=np.number)
652-
# datetime_is_numeric param added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
653652
if PD_LTE_15:
654653
# datetime_is_numeric param added in 1.1.0
655654
# https://pandas.pydata.org/docs/whatsnew/v1.1.0.html

0 commit comments

Comments
 (0)