1
1
"""Record warnings during test function execution."""
2
2
import re
3
3
import warnings
4
+ from pprint import pformat
4
5
from types import TracebackType
5
6
from typing import Any
6
7
from typing import Callable
@@ -142,10 +143,11 @@ def warns(
142
143
__tracebackhide__ = True
143
144
if not args :
144
145
if kwargs :
145
- msg = "Unexpected keyword arguments passed to pytest.warns: "
146
- msg += ", " .join (sorted (kwargs ))
147
- msg += "\n Use context-manager form instead?"
148
- raise TypeError (msg )
146
+ argnames = ", " .join (sorted (kwargs ))
147
+ raise TypeError (
148
+ f"Unexpected keyword arguments passed to pytest.warns: { argnames } "
149
+ "\n Use context-manager form instead?"
150
+ )
149
151
return WarningsChecker (expected_warning , match_expr = match , _ispytest = True )
150
152
else :
151
153
func = args [0 ]
@@ -191,7 +193,7 @@ def pop(self, cls: Type[Warning] = Warning) -> "warnings.WarningMessage":
191
193
if issubclass (w .category , cls ):
192
194
return self ._list .pop (i )
193
195
__tracebackhide__ = True
194
- raise AssertionError ("%r not found in warning list" % cls )
196
+ raise AssertionError (f" { cls !r } not found in warning list" )
195
197
196
198
def clear (self ) -> None :
197
199
"""Clear the list of recorded warnings."""
@@ -202,7 +204,7 @@ def clear(self) -> None:
202
204
def __enter__ (self ) -> "WarningsRecorder" : # type: ignore
203
205
if self ._entered :
204
206
__tracebackhide__ = True
205
- raise RuntimeError ("Cannot enter %r twice" % self )
207
+ raise RuntimeError (f "Cannot enter { self !r } twice" )
206
208
_list = super ().__enter__ ()
207
209
# record=True means it's None.
208
210
assert _list is not None
@@ -218,7 +220,7 @@ def __exit__(
218
220
) -> None :
219
221
if not self ._entered :
220
222
__tracebackhide__ = True
221
- raise RuntimeError ("Cannot exit %r without entering first" % self )
223
+ raise RuntimeError (f "Cannot exit { self !r } without entering first" )
222
224
223
225
super ().__exit__ (exc_type , exc_val , exc_tb )
224
226
@@ -268,16 +270,17 @@ def __exit__(
268
270
269
271
__tracebackhide__ = True
270
272
273
+ def found_str ():
274
+ return pformat ([record .message for record in self ], indent = 2 )
275
+
271
276
# only check if we're not currently handling an exception
272
277
if exc_type is None and exc_val is None and exc_tb is None :
273
278
if self .expected_warning is not None :
274
279
if not any (issubclass (r .category , self .expected_warning ) for r in self ):
275
280
__tracebackhide__ = True
276
281
fail (
277
- "DID NOT WARN. No warnings of type {} were emitted. "
278
- "The list of emitted warnings is: {}." .format (
279
- self .expected_warning , [each .message for each in self ]
280
- )
282
+ f"DID NOT WARN. No warnings of type { self .expected_warning } were emitted. \n "
283
+ f"The list of emitted warnings is: { found_str ()} ."
281
284
)
282
285
elif self .match_expr is not None :
283
286
for r in self :
@@ -286,11 +289,8 @@ def __exit__(
286
289
break
287
290
else :
288
291
fail (
289
- "DID NOT WARN. No warnings of type {} matching"
290
- " ('{}') were emitted. The list of emitted warnings"
291
- " is: {}." .format (
292
- self .expected_warning ,
293
- self .match_expr ,
294
- [each .message for each in self ],
295
- )
292
+ f"""\
293
+ DID NOT WARN. No warnings of type { self .expected_warning } matching the regex were emitted.
294
+ The regex is: { self .match_expr !r}
295
+ The list of emitted warnings is: { found_str ()} """
296
296
)
0 commit comments