31
31
W = NewType ("W" , str )
32
32
33
33
34
+ @dataclass
35
+ class WarningInfo :
36
+ """Properties and assertion methods for warnings."""
37
+
38
+ regexp : str
39
+ type : str
40
+
41
+ def assert_regexp (self , message : str ) -> None :
42
+ regexp = self .regexp
43
+ msg = f"Regex pattern did not match.\n Regex: { regexp !r} \n Input: { message !r} "
44
+ assert re .search (regexp , message ), msg
45
+
46
+ def assert_type (self , message : str ) -> None :
47
+ expected = f"[{ self .type } ]"
48
+ msg = f"Warning did not contain type and subtype.\n Expected: { expected } \n Input: { message } "
49
+ assert expected in message , msg
50
+
51
+ def assert_warning (self , message : str ) -> None :
52
+ self .assert_regexp (message )
53
+ self .assert_type (message )
54
+
55
+
34
56
def expected (expected : str , ** options : dict [str , Any ]) -> Callable [[T ], T ]:
35
57
def dec (val : T ) -> T :
36
58
val .EXPECTED = expected
@@ -40,9 +62,9 @@ def dec(val: T) -> T:
40
62
return dec
41
63
42
64
43
- def warns (pattern : str ) -> Callable [[T ], T ]:
65
+ def warns (info : WarningInfo ) -> Callable [[T ], T ]:
44
66
def dec (val : T ) -> T :
45
- val .WARNING = pattern
67
+ val .WARNING = info
46
68
return val
47
69
48
70
return dec
@@ -58,7 +80,7 @@ def wrapper(self) -> str: # noqa: ANN001
58
80
return wrapper
59
81
60
82
61
- @warns ("Cannot handle as a local function" )
83
+ @warns (WarningInfo ( regexp = "Cannot handle as a local function" , type = "sphinx_autodoc_typehints.local_function" ) )
62
84
@expected (
63
85
"""\
64
86
class mod.Class(x, y, z=None)
@@ -330,7 +352,11 @@ def function_with_escaped_default(x: str = "\b"): # noqa: ANN201
330
352
"""
331
353
332
354
333
- @warns ("Cannot resolve forward reference in type annotations" )
355
+ @warns (
356
+ WarningInfo (
357
+ regexp = "Cannot resolve forward reference in type annotations" , type = "sphinx_autodoc_typehints.forward_reference"
358
+ )
359
+ )
334
360
@expected (
335
361
"""\
336
362
mod.function_with_unresolvable_annotation(x)
@@ -1196,7 +1222,7 @@ def docstring_with_enum_list_after_params(param: int) -> None:
1196
1222
"""
1197
1223
1198
1224
1199
- @warns ("Definition list ends without a blank line" )
1225
+ @warns (WarningInfo ( regexp = "Definition list ends without a blank line" , type = "docutils" ) )
1200
1226
@expected (
1201
1227
"""
1202
1228
mod.docstring_with_definition_list_after_params_no_blank_line(param)
@@ -1457,7 +1483,7 @@ def has_doctest1() -> None:
1457
1483
Unformatted = TypeVar ("Unformatted" )
1458
1484
1459
1485
1460
- @warns ("cannot cache unpickleable configuration value: 'typehints_formatter'" )
1486
+ @warns (WarningInfo ( regexp = "cannot cache unpickleable configuration value: 'typehints_formatter'" , type = "config.cache" ) )
1461
1487
@expected (
1462
1488
"""
1463
1489
mod.typehints_formatter_applied_to_signature(param: Formatted) -> Formatted
@@ -1525,11 +1551,10 @@ def test_integration(
1525
1551
app .build ()
1526
1552
assert "build succeeded" in status .getvalue () # Build succeeded
1527
1553
1528
- regexp = getattr (val , "WARNING" , None )
1554
+ warning_info : Union [ WarningInfo , None ] = getattr (val , "WARNING" , None )
1529
1555
value = warning .getvalue ().strip ()
1530
- if regexp :
1531
- msg = f"Regex pattern did not match.\n Regex: { regexp !r} \n Input: { value !r} "
1532
- assert re .search (regexp , value ), msg
1556
+ if warning_info :
1557
+ warning_info .assert_warning (value )
1533
1558
else :
1534
1559
assert not value
1535
1560
0 commit comments