Skip to content

Commit 6e6a4cd

Browse files
authored
gh-106300: Improve assertRaises(Exception) usages in tests (GH-106302)
1 parent 80b9b3a commit 6e6a4cd

File tree

7 files changed

+20
-12
lines changed

7 files changed

+20
-12
lines changed

Lib/test/test_abc.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -448,15 +448,16 @@ class S(metaclass=abc_ABCMeta):
448448

449449
# Also check that issubclass() propagates exceptions raised by
450450
# __subclasses__.
451+
class CustomError(Exception): ...
451452
exc_msg = "exception from __subclasses__"
452453

453454
def raise_exc():
454-
raise Exception(exc_msg)
455+
raise CustomError(exc_msg)
455456

456457
class S(metaclass=abc_ABCMeta):
457458
__subclasses__ = raise_exc
458459

459-
with self.assertRaisesRegex(Exception, exc_msg):
460+
with self.assertRaisesRegex(CustomError, exc_msg):
460461
issubclass(int, S)
461462

462463
def test_subclasshook(self):

Lib/test/test_codecs.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2822,14 +2822,15 @@ def test_binary_to_text_denylists_text_transforms(self):
28222822
def test_custom_zlib_error_is_noted(self):
28232823
# Check zlib codec gives a good error for malformed input
28242824
msg = "decoding with 'zlib_codec' codec failed"
2825-
with self.assertRaises(Exception) as failure:
2825+
with self.assertRaises(zlib.error) as failure:
28262826
codecs.decode(b"hello", "zlib_codec")
28272827
self.assertEqual(msg, failure.exception.__notes__[0])
28282828

28292829
def test_custom_hex_error_is_noted(self):
28302830
# Check hex codec gives a good error for malformed input
2831+
import binascii
28312832
msg = "decoding with 'hex_codec' codec failed"
2832-
with self.assertRaises(Exception) as failure:
2833+
with self.assertRaises(binascii.Error) as failure:
28332834
codecs.decode(b"hello", "hex_codec")
28342835
self.assertEqual(msg, failure.exception.__notes__[0])
28352836

Lib/test/test_email/test_message.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -696,14 +696,16 @@ def subtype_as_add(self, method, subtype, outcome):
696696
self.assertIsNone(part['Content-Disposition'])
697697

698698
class _TestSetRaisingContentManager:
699+
class CustomError(Exception):
700+
pass
699701
def set_content(self, msg, content, *args, **kw):
700-
raise Exception('test')
702+
raise self.CustomError('test')
701703

702704
def test_default_content_manager_for_add_comes_from_policy(self):
703705
cm = self._TestSetRaisingContentManager()
704706
m = self.message(policy=self.policy.clone(content_manager=cm))
705707
for method in ('add_related', 'add_alternative', 'add_attachment'):
706-
with self.assertRaises(Exception) as ar:
708+
with self.assertRaises(self._TestSetRaisingContentManager.CustomError) as ar:
707709
getattr(m, method)('')
708710
self.assertEqual(str(ar.exception), 'test')
709711

Lib/test/test_importlib/test_main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_abc_enforced(self):
6868
dict(name=''),
6969
)
7070
def test_invalid_inputs_to_from_name(self, name):
71-
with self.assertRaises(Exception):
71+
with self.assertRaises(ValueError):
7272
Distribution.from_name(name)
7373

7474

Lib/test/test_mailbox.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ def test_add_nonascii_string_header_raises(self):
116116
self.assertMailboxEmpty()
117117

118118
def test_add_that_raises_leaves_mailbox_empty(self):
119+
class CustomError(Exception): ...
120+
exc_msg = "a fake error"
121+
119122
def raiser(*args, **kw):
120-
raise Exception("a fake error")
123+
raise CustomError(exc_msg)
121124
support.patch(self, email.generator.BytesGenerator, 'flatten', raiser)
122-
with self.assertRaises(Exception):
125+
with self.assertRaisesRegex(CustomError, exc_msg):
123126
self._box.add(email.message_from_string("From: Alphöso"))
124127
self.assertEqual(len(self._box), 0)
125128
self._box.close()

Lib/test/test_shutil.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ def test_regular_copy(self):
27382738
def test_same_file(self):
27392739
self.addCleanup(self.reset)
27402740
with self.get_files() as (src, dst):
2741-
with self.assertRaises(Exception):
2741+
with self.assertRaises((OSError, _GiveupOnFastCopy)):
27422742
self.zerocopy_fun(src, src)
27432743
# Make sure src file is not corrupted.
27442744
self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA)

Lib/test/test_unittest/testmock/testasync.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,10 @@ async def addition(self, var): pass
459459
self.assertEqual(output, 10)
460460

461461
async def test_add_side_effect_exception(self):
462+
class CustomError(Exception): pass
462463
async def addition(var): pass
463-
mock = AsyncMock(addition, side_effect=Exception('err'))
464-
with self.assertRaises(Exception):
464+
mock = AsyncMock(addition, side_effect=CustomError('side-effect'))
465+
with self.assertRaisesRegex(CustomError, 'side-effect'):
465466
await mock(5)
466467

467468
async def test_add_side_effect_coroutine(self):

0 commit comments

Comments
 (0)