@@ -145,15 +145,15 @@ def warns(
145
145
You may also set the keyword argument ``keep_ignores`` to avoid catching warnings
146
146
which were filtered out, in pytest configuration or otherwise::
147
147
148
- >>> warnings.simplefilter("ignore", category=FutureWarning )
148
+ >>> warnings.simplefilter("ignore", category=UserWarning )
149
149
>>> with pytest.warns(UserWarning, keep_ignores=True):
150
150
... warnings.warn("ignore this warning", UserWarning)
151
151
Traceback (most recent call last):
152
152
...
153
153
Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted...
154
154
155
155
>>> with pytest.warns(RuntimeWarning):
156
- >>> warnings.simplefilter("ignore", category=FutureWarning )
156
+ >>> warnings.simplefilter("ignore", category=UserWarning )
157
157
>>> with pytest.warns(UserWarning, keep_ignores=True):
158
158
... warnings.warn("ignore this warning", UserWarning)
159
159
warnings.warn("keep this warning", RuntimeWarning)
@@ -260,13 +260,15 @@ def __enter__(self) -> Self:
260
260
261
261
if self ._keep_ignores :
262
262
for action , message , category , module , lineno in reversed (warnings .filters ):
263
+ if isinstance (message , re .Pattern ):
264
+ module = getattr (module , "pattern" , None ) # type: ignore[unreachable]
263
265
if isinstance (module , re .Pattern ):
264
266
module = getattr (module , "pattern" , None ) # type: ignore[unreachable]
265
267
warnings .filterwarnings (
266
268
action = "always" if action != "ignore" else "ignore" ,
267
- message = message if isinstance ( message , str ) else "" ,
269
+ message = message or "" ,
268
270
category = category ,
269
- module = module if isinstance ( module , str ) else "" ,
271
+ module = module or "" ,
270
272
lineno = lineno ,
271
273
)
272
274
else :
0 commit comments