forked from pandas-dev/pandas-stubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
136 lines (117 loc) · 4.19 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from __future__ import annotations
from contextlib import (
AbstractContextManager,
nullcontext,
suppress,
)
import os
import platform
from typing import (
TYPE_CHECKING,
Final,
)
import pandas as pd
from pandas.core.groupby.groupby import BaseGroupBy
from pandas.util.version import Version
import pytest
from pandas._typing import T
TYPE_CHECKING_INVALID_USAGE: Final = TYPE_CHECKING
WINDOWS = os.name == "nt" or "cygwin" in platform.system().lower()
PD_LTE_22 = Version(pd.__version__) < Version("2.2.999")
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:
return actual # type: ignore[return-value]
if isinstance(actual, pd.Series):
value = actual.iloc[0]
elif isinstance(actual, pd.Index):
value = actual[0] # type: ignore[assignment]
elif isinstance(actual, BaseGroupBy):
value = actual.obj
elif hasattr(actual, "__iter__"):
value = next(
iter(actual) # pyright: ignore[reportArgumentType,reportCallIssue]
)
else:
assert hasattr(actual, attr)
value = getattr(actual, attr)
if not isinstance(value, dtype):
raise RuntimeError(f"Expected type '{dtype}' but got '{type(value)}'")
return actual # type: ignore[return-value]
def pytest_warns_bounded(
warning: type[Warning],
match: str,
lower: str | None = None,
upper: str | None = None,
version_str: str | None = None,
upper_exception: type[Exception] | None = None,
) -> AbstractContextManager:
"""
Version conditional pytest.warns context manager
Returns a context manager that will raise an error if
the warning is not issued when pandas version is
between the lower and upper version given.
Parameters
----------
warning : type[Warning]
The warning class to check for.
match : str
The string to match in the warning message.
lower : str, optional
The lower bound of the version to check for the warning.
upper : str, optional
The upper bound of the version to check for the warning.
version_str: str, optional
The version string to use. If None, then uses the pandas version.
Can be used to check a python version as well
upper_exception: Exception, optional
Exception to catch if the pandas version is greater than or equal to
the upper bound
Notes
-----
The lower and upper bounds are exclusive so that a pytest.warns context
manager is returned if lower < version_str < upper.
Examples
--------
with pytest_warns_bounded(UserWarning, match="foo", lower="1.2.99"):
# Versions 1.3.0 and above will raise an error
# if the warning is not issued
pass
with pytest_warns_bounded(UserWarning, match="foo", upper="1.5.99"):
# Versions 1.6.0 and below will raise an error
# if the warning is not issued
pass
with pytest_warns_bounded(
UserWarning, match="foo", lower="1.2.99", upper="1.5.99"
):
# Versions between 1.3.x and 1.5.x will raise an error
pass
with pytest_warns_bounded(
UserWarning, match="foo", lower="3.10",
version_str = platform.python_version()
):
# Python version 3.11 and above will raise an error
# if the warning is not issued
pass
with pytest_warns_bounded(
UserWarning, match="foo", lower="1.2.99", upper="1.5.99",
upper_exception=AttributeError
):
# Versions between 1.3.x and 1.5.x will raise an error
# Above 1.5.x, we expect an `AttributeError` to be raised
pass
"""
lb = Version("0.0.0") if lower is None else Version(lower)
ub = Version("9999.0.0") if upper is None else Version(upper)
if version_str is None:
current = Version(pd.__version__)
else:
current = Version(version_str)
if lb < current < ub:
return pytest.warns(warning, match=match)
else:
if upper_exception is None:
return nullcontext()
else:
return suppress(upper_exception)