forked from pandas-dev/pandas-stubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
126 lines (106 loc) · 3.91 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
from __future__ import annotations
from contextlib import (
AbstractContextManager,
nullcontext,
)
import os
import platform
import sys
from typing import (
TYPE_CHECKING,
Final,
)
import pandas as pd
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_15 = Version(pd.__version__) < Version("1.5.999")
arrow_skip = pytest.mark.skipif(
sys.version_info >= (3, 11), reason="pyarrow is not available for 3.11 yet"
)
"""This is only needed temporarily due to no wheels being available for arrow on 3.11"""
lxml_skip = pytest.mark.skipif(
sys.version_info >= (3, 11), reason="lxml is not available for 3.11 yet"
)
"""This is only needed temporarily due to no wheels being available for lxml on 3.11"""
pytables_skip = pytest.mark.skipif(
sys.version_info >= (3, 11), reason="pytables is not available for 3.11 yet"
)
"""This is only needed temporarily due to no wheels being available for pytables on 3.11"""
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 hasattr(actual, "__iter__"):
value = next(iter(actual)) # pyright: ignore[reportGeneralTypeIssues]
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,
) -> 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
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
"""
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:
return nullcontext()