]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-71339: Improve error report for types in assertHasAttr() and assertNotHasAttr...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Jan 2025 09:17:49 +0000 (11:17 +0200)
committerGitHub <noreply@github.com>
Mon, 20 Jan 2025 09:17:49 +0000 (11:17 +0200)
Lib/test/test_unittest/test_case.py
Lib/unittest/case.py

index cd366496eedca307ed01976224978cb95b5b18ad..df1381451b7ebc3d93cd0c514a42dce981c46bbb 100644 (file)
@@ -795,7 +795,15 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
         with self.assertRaises(self.failureException) as cm:
             self.assertHasAttr(a, 'y')
         self.assertEqual(str(cm.exception),
-                "List instance has no attribute 'y'")
+                "'List' object has no attribute 'y'")
+        with self.assertRaises(self.failureException) as cm:
+            self.assertHasAttr(List, 'spam')
+        self.assertEqual(str(cm.exception),
+                "type object 'List' has no attribute 'spam'")
+        with self.assertRaises(self.failureException) as cm:
+            self.assertHasAttr(sys, 'spam')
+        self.assertEqual(str(cm.exception),
+                "module 'sys' has no attribute 'spam'")
 
         with self.assertRaises(self.failureException) as cm:
             self.assertHasAttr(a, 'y', 'ababahalamaha')
@@ -811,7 +819,15 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
         with self.assertRaises(self.failureException) as cm:
             self.assertNotHasAttr(a, 'x')
         self.assertEqual(str(cm.exception),
-                "List instance has unexpected attribute 'x'")
+                "'List' object has unexpected attribute 'x'")
+        with self.assertRaises(self.failureException) as cm:
+            self.assertNotHasAttr(List, 'append')
+        self.assertEqual(str(cm.exception),
+                "type object 'List' has unexpected attribute 'append'")
+        with self.assertRaises(self.failureException) as cm:
+            self.assertNotHasAttr(sys, 'modules')
+        self.assertEqual(str(cm.exception),
+                "module 'sys' has unexpected attribute 'modules'")
 
         with self.assertRaises(self.failureException) as cm:
             self.assertNotHasAttr(a, 'x', 'ababahalamaha')
index e9ef551d0b3dedcbc2bb4eeaa7dd2613fe23ae73..10c3b7e122371ef55869eed0d093415e51a99803 100644 (file)
@@ -1372,16 +1372,20 @@ class TestCase(object):
         if not hasattr(obj, name):
             if isinstance(obj, types.ModuleType):
                 standardMsg = f'module {obj.__name__!r} has no attribute {name!r}'
+            elif isinstance(obj, type):
+                standardMsg = f'type object {obj.__name__!r} has no attribute {name!r}'
             else:
-                standardMsg = f'{type(obj).__name__} instance has no attribute {name!r}'
+                standardMsg = f'{type(obj).__name__!r} object has no attribute {name!r}'
             self.fail(self._formatMessage(msg, standardMsg))
 
     def assertNotHasAttr(self, obj, name, msg=None):
         if hasattr(obj, name):
             if isinstance(obj, types.ModuleType):
                 standardMsg = f'module {obj.__name__!r} has unexpected attribute {name!r}'
+            elif isinstance(obj, type):
+                standardMsg = f'type object {obj.__name__!r} has unexpected attribute {name!r}'
             else:
-                standardMsg = f'{type(obj).__name__} instance has unexpected attribute {name!r}'
+                standardMsg = f'{type(obj).__name__!r} object has unexpected attribute {name!r}'
             self.fail(self._formatMessage(msg, standardMsg))
 
     def assertRaisesRegex(self, expected_exception, expected_regex,