1
1
import re
2
2
import warnings
3
+ from typing import Optional
3
4
4
5
import pytest
6
+ from _pytest .outcomes import Failed
5
7
from _pytest .recwarn import WarningsRecorder
6
8
7
9
8
- def test_recwarn_stacklevel (recwarn ) :
10
+ def test_recwarn_stacklevel (recwarn : WarningsRecorder ) -> None :
9
11
warnings .warn ("hello" )
10
12
warn = recwarn .pop ()
11
13
assert warn .filename == __file__
12
14
13
15
14
- def test_recwarn_functional (testdir ):
16
+ def test_recwarn_functional (testdir ) -> None :
15
17
testdir .makepyfile (
16
18
"""
17
19
import warnings
@@ -26,7 +28,7 @@ def test_method(recwarn):
26
28
27
29
28
30
class TestWarningsRecorderChecker :
29
- def test_recording (self ):
31
+ def test_recording (self ) -> None :
30
32
rec = WarningsRecorder ()
31
33
with rec :
32
34
assert not rec .list
@@ -42,23 +44,23 @@ def test_recording(self):
42
44
assert values is rec .list
43
45
pytest .raises (AssertionError , rec .pop )
44
46
45
- def test_warn_stacklevel (self ):
47
+ def test_warn_stacklevel (self ) -> None :
46
48
"""#4243"""
47
49
rec = WarningsRecorder ()
48
50
with rec :
49
51
warnings .warn ("test" , DeprecationWarning , 2 )
50
52
51
- def test_typechecking (self ):
53
+ def test_typechecking (self ) -> None :
52
54
from _pytest .recwarn import WarningsChecker
53
55
54
56
with pytest .raises (TypeError ):
55
- WarningsChecker (5 )
57
+ WarningsChecker (5 ) # type: ignore
56
58
with pytest .raises (TypeError ):
57
- WarningsChecker (("hi" , RuntimeWarning ))
59
+ WarningsChecker (("hi" , RuntimeWarning )) # type: ignore
58
60
with pytest .raises (TypeError ):
59
- WarningsChecker ([DeprecationWarning , RuntimeWarning ])
61
+ WarningsChecker ([DeprecationWarning , RuntimeWarning ]) # type: ignore
60
62
61
- def test_invalid_enter_exit (self ):
63
+ def test_invalid_enter_exit (self ) -> None :
62
64
# wrap this test in WarningsRecorder to ensure warning state gets reset
63
65
with WarningsRecorder ():
64
66
with pytest .raises (RuntimeError ):
@@ -75,50 +77,52 @@ def test_invalid_enter_exit(self):
75
77
class TestDeprecatedCall :
76
78
"""test pytest.deprecated_call()"""
77
79
78
- def dep (self , i , j = None ):
80
+ def dep (self , i : int , j : Optional [ int ] = None ) -> int :
79
81
if i == 0 :
80
82
warnings .warn ("is deprecated" , DeprecationWarning , stacklevel = 1 )
81
83
return 42
82
84
83
- def dep_explicit (self , i ) :
85
+ def dep_explicit (self , i : int ) -> None :
84
86
if i == 0 :
85
87
warnings .warn_explicit (
86
88
"dep_explicit" , category = DeprecationWarning , filename = "hello" , lineno = 3
87
89
)
88
90
89
- def test_deprecated_call_raises (self ):
90
- with pytest .raises (pytest . fail . Exception , match = "No warnings of type" ):
91
+ def test_deprecated_call_raises (self ) -> None :
92
+ with pytest .raises (Failed , match = "No warnings of type" ):
91
93
pytest .deprecated_call (self .dep , 3 , 5 )
92
94
93
- def test_deprecated_call (self ):
95
+ def test_deprecated_call (self ) -> None :
94
96
pytest .deprecated_call (self .dep , 0 , 5 )
95
97
96
- def test_deprecated_call_ret (self ):
98
+ def test_deprecated_call_ret (self ) -> None :
97
99
ret = pytest .deprecated_call (self .dep , 0 )
98
100
assert ret == 42
99
101
100
- def test_deprecated_call_preserves (self ):
101
- onceregistry = warnings .onceregistry .copy ()
102
- filters = warnings .filters [:]
102
+ def test_deprecated_call_preserves (self ) -> None :
103
+ # Type ignored because `onceregistry` and `filters` are not
104
+ # documented API.
105
+ onceregistry = warnings .onceregistry .copy () # type: ignore
106
+ filters = warnings .filters [:] # type: ignore
103
107
warn = warnings .warn
104
108
warn_explicit = warnings .warn_explicit
105
109
self .test_deprecated_call_raises ()
106
110
self .test_deprecated_call ()
107
- assert onceregistry == warnings .onceregistry
108
- assert filters == warnings .filters
111
+ assert onceregistry == warnings .onceregistry # type: ignore
112
+ assert filters == warnings .filters # type: ignore
109
113
assert warn is warnings .warn
110
114
assert warn_explicit is warnings .warn_explicit
111
115
112
- def test_deprecated_explicit_call_raises (self ):
113
- with pytest .raises (pytest . fail . Exception ):
116
+ def test_deprecated_explicit_call_raises (self ) -> None :
117
+ with pytest .raises (Failed ):
114
118
pytest .deprecated_call (self .dep_explicit , 3 )
115
119
116
- def test_deprecated_explicit_call (self ):
120
+ def test_deprecated_explicit_call (self ) -> None :
117
121
pytest .deprecated_call (self .dep_explicit , 0 )
118
122
pytest .deprecated_call (self .dep_explicit , 0 )
119
123
120
124
@pytest .mark .parametrize ("mode" , ["context_manager" , "call" ])
121
- def test_deprecated_call_no_warning (self , mode ):
125
+ def test_deprecated_call_no_warning (self , mode ) -> None :
122
126
"""Ensure deprecated_call() raises the expected failure when its block/function does
123
127
not raise a deprecation warning.
124
128
"""
@@ -127,7 +131,7 @@ def f():
127
131
pass
128
132
129
133
msg = "No warnings of type (.*DeprecationWarning.*, .*PendingDeprecationWarning.*)"
130
- with pytest .raises (pytest . fail . Exception , match = msg ):
134
+ with pytest .raises (Failed , match = msg ):
131
135
if mode == "call" :
132
136
pytest .deprecated_call (f )
133
137
else :
@@ -140,7 +144,7 @@ def f():
140
144
@pytest .mark .parametrize ("mode" , ["context_manager" , "call" ])
141
145
@pytest .mark .parametrize ("call_f_first" , [True , False ])
142
146
@pytest .mark .filterwarnings ("ignore" )
143
- def test_deprecated_call_modes (self , warning_type , mode , call_f_first ):
147
+ def test_deprecated_call_modes (self , warning_type , mode , call_f_first ) -> None :
144
148
"""Ensure deprecated_call() captures a deprecation warning as expected inside its
145
149
block/function.
146
150
"""
@@ -159,7 +163,7 @@ def f():
159
163
assert f () == 10
160
164
161
165
@pytest .mark .parametrize ("mode" , ["context_manager" , "call" ])
162
- def test_deprecated_call_exception_is_raised (self , mode ):
166
+ def test_deprecated_call_exception_is_raised (self , mode ) -> None :
163
167
"""If the block of the code being tested by deprecated_call() raises an exception,
164
168
it must raise the exception undisturbed.
165
169
"""
@@ -174,7 +178,7 @@ def f():
174
178
with pytest .deprecated_call ():
175
179
f ()
176
180
177
- def test_deprecated_call_specificity (self ):
181
+ def test_deprecated_call_specificity (self ) -> None :
178
182
other_warnings = [
179
183
Warning ,
180
184
UserWarning ,
@@ -189,78 +193,78 @@ def test_deprecated_call_specificity(self):
189
193
def f ():
190
194
warnings .warn (warning ("hi" ))
191
195
192
- with pytest .raises (pytest . fail . Exception ):
196
+ with pytest .raises (Failed ):
193
197
pytest .deprecated_call (f )
194
- with pytest .raises (pytest . fail . Exception ):
198
+ with pytest .raises (Failed ):
195
199
with pytest .deprecated_call ():
196
200
f ()
197
201
198
- def test_deprecated_call_supports_match (self ):
202
+ def test_deprecated_call_supports_match (self ) -> None :
199
203
with pytest .deprecated_call (match = r"must be \d+$" ):
200
204
warnings .warn ("value must be 42" , DeprecationWarning )
201
205
202
- with pytest .raises (pytest . fail . Exception ):
206
+ with pytest .raises (Failed ):
203
207
with pytest .deprecated_call (match = r"must be \d+$" ):
204
208
warnings .warn ("this is not here" , DeprecationWarning )
205
209
206
210
207
211
class TestWarns :
208
- def test_check_callable (self ):
212
+ def test_check_callable (self ) -> None :
209
213
source = "warnings.warn('w1', RuntimeWarning)"
210
214
with pytest .raises (TypeError , match = r".* must be callable" ):
211
- pytest .warns (RuntimeWarning , source )
215
+ pytest .warns (RuntimeWarning , source ) # type: ignore
212
216
213
- def test_several_messages (self ):
217
+ def test_several_messages (self ) -> None :
214
218
# different messages, b/c Python suppresses multiple identical warnings
215
219
pytest .warns (RuntimeWarning , lambda : warnings .warn ("w1" , RuntimeWarning ))
216
- with pytest .raises (pytest . fail . Exception ):
220
+ with pytest .raises (Failed ):
217
221
pytest .warns (UserWarning , lambda : warnings .warn ("w2" , RuntimeWarning ))
218
222
pytest .warns (RuntimeWarning , lambda : warnings .warn ("w3" , RuntimeWarning ))
219
223
220
- def test_function (self ):
224
+ def test_function (self ) -> None :
221
225
pytest .warns (
222
226
SyntaxWarning , lambda msg : warnings .warn (msg , SyntaxWarning ), "syntax"
223
227
)
224
228
225
- def test_warning_tuple (self ):
229
+ def test_warning_tuple (self ) -> None :
226
230
pytest .warns (
227
231
(RuntimeWarning , SyntaxWarning ), lambda : warnings .warn ("w1" , RuntimeWarning )
228
232
)
229
233
pytest .warns (
230
234
(RuntimeWarning , SyntaxWarning ), lambda : warnings .warn ("w2" , SyntaxWarning )
231
235
)
232
236
pytest .raises (
233
- pytest . fail . Exception ,
237
+ Failed ,
234
238
lambda : pytest .warns (
235
239
(RuntimeWarning , SyntaxWarning ),
236
240
lambda : warnings .warn ("w3" , UserWarning ),
237
241
),
238
242
)
239
243
240
- def test_as_contextmanager (self ):
244
+ def test_as_contextmanager (self ) -> None :
241
245
with pytest .warns (RuntimeWarning ):
242
246
warnings .warn ("runtime" , RuntimeWarning )
243
247
244
248
with pytest .warns (UserWarning ):
245
249
warnings .warn ("user" , UserWarning )
246
250
247
- with pytest .raises (pytest . fail . Exception ) as excinfo :
251
+ with pytest .raises (Failed ) as excinfo :
248
252
with pytest .warns (RuntimeWarning ):
249
253
warnings .warn ("user" , UserWarning )
250
254
excinfo .match (
251
255
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) was emitted. "
252
256
r"The list of emitted warnings is: \[UserWarning\('user',?\)\]."
253
257
)
254
258
255
- with pytest .raises (pytest . fail . Exception ) as excinfo :
259
+ with pytest .raises (Failed ) as excinfo :
256
260
with pytest .warns (UserWarning ):
257
261
warnings .warn ("runtime" , RuntimeWarning )
258
262
excinfo .match (
259
263
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. "
260
264
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)\]."
261
265
)
262
266
263
- with pytest .raises (pytest . fail . Exception ) as excinfo :
267
+ with pytest .raises (Failed ) as excinfo :
264
268
with pytest .warns (UserWarning ):
265
269
pass
266
270
excinfo .match (
@@ -269,7 +273,7 @@ def test_as_contextmanager(self):
269
273
)
270
274
271
275
warning_classes = (UserWarning , FutureWarning )
272
- with pytest .raises (pytest . fail . Exception ) as excinfo :
276
+ with pytest .raises (Failed ) as excinfo :
273
277
with pytest .warns (warning_classes ) as warninfo :
274
278
warnings .warn ("runtime" , RuntimeWarning )
275
279
warnings .warn ("import" , ImportWarning )
@@ -286,14 +290,14 @@ def test_as_contextmanager(self):
286
290
)
287
291
)
288
292
289
- def test_record (self ):
293
+ def test_record (self ) -> None :
290
294
with pytest .warns (UserWarning ) as record :
291
295
warnings .warn ("user" , UserWarning )
292
296
293
297
assert len (record ) == 1
294
298
assert str (record [0 ].message ) == "user"
295
299
296
- def test_record_only (self ):
300
+ def test_record_only (self ) -> None :
297
301
with pytest .warns (None ) as record :
298
302
warnings .warn ("user" , UserWarning )
299
303
warnings .warn ("runtime" , RuntimeWarning )
@@ -302,7 +306,7 @@ def test_record_only(self):
302
306
assert str (record [0 ].message ) == "user"
303
307
assert str (record [1 ].message ) == "runtime"
304
308
305
- def test_record_by_subclass (self ):
309
+ def test_record_by_subclass (self ) -> None :
306
310
with pytest .warns (Warning ) as record :
307
311
warnings .warn ("user" , UserWarning )
308
312
warnings .warn ("runtime" , RuntimeWarning )
@@ -325,7 +329,7 @@ class MyRuntimeWarning(RuntimeWarning):
325
329
assert str (record [0 ].message ) == "user"
326
330
assert str (record [1 ].message ) == "runtime"
327
331
328
- def test_double_test (self , testdir ):
332
+ def test_double_test (self , testdir ) -> None :
329
333
"""If a test is run again, the warning should still be raised"""
330
334
testdir .makepyfile (
331
335
"""
@@ -341,32 +345,32 @@ def test(run):
341
345
result = testdir .runpytest ()
342
346
result .stdout .fnmatch_lines (["*2 passed in*" ])
343
347
344
- def test_match_regex (self ):
348
+ def test_match_regex (self ) -> None :
345
349
with pytest .warns (UserWarning , match = r"must be \d+$" ):
346
350
warnings .warn ("value must be 42" , UserWarning )
347
351
348
- with pytest .raises (pytest . fail . Exception ):
352
+ with pytest .raises (Failed ):
349
353
with pytest .warns (UserWarning , match = r"must be \d+$" ):
350
354
warnings .warn ("this is not here" , UserWarning )
351
355
352
- with pytest .raises (pytest . fail . Exception ):
356
+ with pytest .raises (Failed ):
353
357
with pytest .warns (FutureWarning , match = r"must be \d+$" ):
354
358
warnings .warn ("value must be 42" , UserWarning )
355
359
356
- def test_one_from_multiple_warns (self ):
360
+ def test_one_from_multiple_warns (self ) -> None :
357
361
with pytest .warns (UserWarning , match = r"aaa" ):
358
362
warnings .warn ("cccccccccc" , UserWarning )
359
363
warnings .warn ("bbbbbbbbbb" , UserWarning )
360
364
warnings .warn ("aaaaaaaaaa" , UserWarning )
361
365
362
- def test_none_of_multiple_warns (self ):
363
- with pytest .raises (pytest . fail . Exception ):
366
+ def test_none_of_multiple_warns (self ) -> None :
367
+ with pytest .raises (Failed ):
364
368
with pytest .warns (UserWarning , match = r"aaa" ):
365
369
warnings .warn ("bbbbbbbbbb" , UserWarning )
366
370
warnings .warn ("cccccccccc" , UserWarning )
367
371
368
372
@pytest .mark .filterwarnings ("ignore" )
369
- def test_can_capture_previously_warned (self ):
373
+ def test_can_capture_previously_warned (self ) -> None :
370
374
def f ():
371
375
warnings .warn (UserWarning ("ohai" ))
372
376
return 10
@@ -375,8 +379,8 @@ def f():
375
379
assert pytest .warns (UserWarning , f ) == 10
376
380
assert pytest .warns (UserWarning , f ) == 10
377
381
378
- def test_warns_context_manager_with_kwargs (self ):
382
+ def test_warns_context_manager_with_kwargs (self ) -> None :
379
383
with pytest .raises (TypeError ) as excinfo :
380
- with pytest .warns (UserWarning , foo = "bar" ):
384
+ with pytest .warns (UserWarning , foo = "bar" ): # type: ignore
381
385
pass
382
386
assert "Unexpected keyword arguments" in str (excinfo .value )
0 commit comments