88import textwrap
99from io import UnsupportedOperation
1010from typing import TYPE_CHECKING
11+ from typing import Any
1112from typing import BinaryIO
13+ from typing import cast
1214
1315import pytest
1416
@@ -208,7 +210,8 @@ def test_init_capturing(self):
208210 try :
209211 capman = CaptureManager (CaptureMethod .FD )
210212 capman .start_capturing ()
211- pytest .raises (AssertionError , capman .start_capturing )
213+ with pytest .raises (AssertionError ):
214+ capman .start_capturing ()
212215 capman .stop_capturing ()
213216 finally :
214217 capouter .stop_capturing ()
@@ -364,7 +367,8 @@ def test_text(self):
364367 def test_unicode_and_str_mixture (self ):
365368 f = capture .CaptureIO ()
366369 f .write ("\u00f6 " )
367- pytest .raises (TypeError , f .write , b"hello" )
370+ with pytest .raises (TypeError ):
371+ f .write (cast ("Any" , b"hello" ))
368372
369373 def test_write_bytes_to_buffer (self ):
370374 """In python3, stdout / stderr are text io wrappers (exposing a buffer property
@@ -394,7 +398,8 @@ def test_unicode_and_str_mixture(self):
394398 sio = io .StringIO ()
395399 f = capture .TeeCaptureIO (sio )
396400 f .write ("\u00f6 " )
397- pytest .raises (TypeError , f .write , b"hello" )
401+ with pytest .raises (TypeError ):
402+ f .write (cast ("Any" , b"hello" ))
398403
399404
400405def test_dontreadfrominput ():
@@ -403,11 +408,15 @@ def test_dontreadfrominput():
403408 f = DontReadFromInput ()
404409 assert f .buffer is f
405410 assert not f .isatty ()
406- pytest .raises (OSError , f .read )
407- pytest .raises (OSError , f .readlines )
411+ with pytest .raises (OSError ):
412+ f .read ()
413+ with pytest .raises (OSError ):
414+ f .readlines ()
408415 iter_f = iter (f )
409- pytest .raises (OSError , next , iter_f )
410- pytest .raises (UnsupportedOperation , f .fileno )
416+ with pytest .raises (OSError ):
417+ next (iter_f )
418+ with pytest .raises (UnsupportedOperation ):
419+ f .fileno ()
411420 f .close () # just for completeness
412421
413422
@@ -474,7 +483,8 @@ def test_simple(self, tmpfile):
474483 cap = capture .FDCapture (fd )
475484 data = b"hello"
476485 os .write (fd , data )
477- pytest .raises (AssertionError , cap .snap )
486+ with pytest .raises (AssertionError ):
487+ cap .snap ()
478488 cap .done ()
479489 cap = capture .FDCapture (fd )
480490 cap .start ()
@@ -495,7 +505,8 @@ def test_simple_fail_second_start(self, tmpfile):
495505 fd = tmpfile .fileno ()
496506 cap = capture .FDCapture (fd )
497507 cap .done ()
498- pytest .raises (AssertionError , cap .start )
508+ with pytest .raises (AssertionError ):
509+ cap .start ()
499510
500511 def test_stderr (self ):
501512 cap = capture .FDCapture (2 )
@@ -546,7 +557,8 @@ def test_simple_resume_suspend(self):
546557 assert s == "but now yes\n "
547558 cap .suspend ()
548559 cap .done ()
549- pytest .raises (AssertionError , cap .suspend )
560+ with pytest .raises (AssertionError ):
561+ cap .suspend ()
550562
551563 assert repr (cap ) == (
552564 "<FDCapture 1 oldfd={} _state='done' tmpfile={!r}>" .format ( # noqa: UP032
@@ -631,7 +643,8 @@ def test_reset_twice_error(self):
631643 with self .getcapture () as cap :
632644 print ("hello" )
633645 out , err = cap .readouterr ()
634- pytest .raises (ValueError , cap .stop_capturing )
646+ with pytest .raises (ValueError ):
647+ cap .stop_capturing ()
635648 assert out == "hello\n "
636649 assert not err
637650
@@ -688,8 +701,8 @@ def test_stdin_nulled_by_default(self):
688701 print ("XX this test may well hang instead of crashing" )
689702 print ("XX which indicates an error in the underlying capturing" )
690703 print ("XX mechanisms" )
691- with self .getcapture ():
692- pytest . raises ( OSError , sys .stdin .read )
704+ with self .getcapture (), pytest . raises ( OSError ) :
705+ sys .stdin .read ( )
693706
694707
695708class TestTeeStdCapture (TestStdCapture ):
@@ -829,6 +842,5 @@ def test_fdcapture_invalid_fd_without_fd_reuse(self, tmp_path):
829842
830843def test__get_multicapture () -> None :
831844 assert isinstance (_get_multicapture (CaptureMethod .NO ), MultiCapture )
832- pytest .raises (ValueError , _get_multicapture , "unknown" ).match (
833- r"^unknown capturing method: 'unknown'"
834- )
845+ with pytest .raises (ValueError , match = r"^unknown capturing method: 'unknown'" ):
846+ _get_multicapture (cast ("CaptureMethod" , "unknown" ))
0 commit comments