1
1
from __future__ import annotations
2
2
3
+ from contextlib import (
4
+ AbstractContextManager ,
5
+ nullcontext ,
6
+ )
3
7
import os
4
8
import platform
5
9
from typing import (
9
13
10
14
from packaging .version import parse
11
15
import pandas as pd
16
+ import pytest
12
17
13
18
from pandas ._typing import T
14
19
@@ -32,3 +37,59 @@ def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left")
32
37
if not isinstance (value , dtype ):
33
38
raise RuntimeError (f"Expected type '{ dtype } ' but got '{ type (value )} '" )
34
39
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 ()
0 commit comments