Skip to content

Commit da122b5

Browse files
gh-71339: Improve error report for types in assertHasAttr() and assertNotHasAttr() (GH-128818)
1 parent 89c401f commit da122b5

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Lib/test/test_unittest/test_case.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,15 @@ def testAssertHasAttr(self):
795795
with self.assertRaises(self.failureException) as cm:
796796
self.assertHasAttr(a, 'y')
797797
self.assertEqual(str(cm.exception),
798-
"List instance has no attribute 'y'")
798+
"'List' object has no attribute 'y'")
799+
with self.assertRaises(self.failureException) as cm:
800+
self.assertHasAttr(List, 'spam')
801+
self.assertEqual(str(cm.exception),
802+
"type object 'List' has no attribute 'spam'")
803+
with self.assertRaises(self.failureException) as cm:
804+
self.assertHasAttr(sys, 'spam')
805+
self.assertEqual(str(cm.exception),
806+
"module 'sys' has no attribute 'spam'")
799807

800808
with self.assertRaises(self.failureException) as cm:
801809
self.assertHasAttr(a, 'y', 'ababahalamaha')
@@ -811,7 +819,15 @@ def testAssertNotHasAttr(self):
811819
with self.assertRaises(self.failureException) as cm:
812820
self.assertNotHasAttr(a, 'x')
813821
self.assertEqual(str(cm.exception),
814-
"List instance has unexpected attribute 'x'")
822+
"'List' object has unexpected attribute 'x'")
823+
with self.assertRaises(self.failureException) as cm:
824+
self.assertNotHasAttr(List, 'append')
825+
self.assertEqual(str(cm.exception),
826+
"type object 'List' has unexpected attribute 'append'")
827+
with self.assertRaises(self.failureException) as cm:
828+
self.assertNotHasAttr(sys, 'modules')
829+
self.assertEqual(str(cm.exception),
830+
"module 'sys' has unexpected attribute 'modules'")
815831

816832
with self.assertRaises(self.failureException) as cm:
817833
self.assertNotHasAttr(a, 'x', 'ababahalamaha')

Lib/unittest/case.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,16 +1372,20 @@ def assertHasAttr(self, obj, name, msg=None):
13721372
if not hasattr(obj, name):
13731373
if isinstance(obj, types.ModuleType):
13741374
standardMsg = f'module {obj.__name__!r} has no attribute {name!r}'
1375+
elif isinstance(obj, type):
1376+
standardMsg = f'type object {obj.__name__!r} has no attribute {name!r}'
13751377
else:
1376-
standardMsg = f'{type(obj).__name__} instance has no attribute {name!r}'
1378+
standardMsg = f'{type(obj).__name__!r} object has no attribute {name!r}'
13771379
self.fail(self._formatMessage(msg, standardMsg))
13781380

13791381
def assertNotHasAttr(self, obj, name, msg=None):
13801382
if hasattr(obj, name):
13811383
if isinstance(obj, types.ModuleType):
13821384
standardMsg = f'module {obj.__name__!r} has unexpected attribute {name!r}'
1385+
elif isinstance(obj, type):
1386+
standardMsg = f'type object {obj.__name__!r} has unexpected attribute {name!r}'
13831387
else:
1384-
standardMsg = f'{type(obj).__name__} instance has unexpected attribute {name!r}'
1388+
standardMsg = f'{type(obj).__name__!r} object has unexpected attribute {name!r}'
13851389
self.fail(self._formatMessage(msg, standardMsg))
13861390

13871391
def assertRaisesRegex(self, expected_exception, expected_regex,

0 commit comments

Comments
 (0)