https://github.com/python/cpython/commit/da122b5facbbae9197a108e0a3c4b3f0594c5e92
commit: da122b5facbbae9197a108e0a3c4b3f0594c5e92
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-01-20T11:17:49+02:00
summary:

gh-71339: Improve error report for types in assertHasAttr() and 
assertNotHasAttr() (GH-128818)

files:
M Lib/test/test_unittest/test_case.py
M Lib/unittest/case.py

diff --git a/Lib/test/test_unittest/test_case.py 
b/Lib/test/test_unittest/test_case.py
index cd366496eedca3..df1381451b7ebc 100644
--- a/Lib/test/test_unittest/test_case.py
+++ b/Lib/test/test_unittest/test_case.py
@@ -795,7 +795,15 @@ def testAssertHasAttr(self):
         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 @@ def testAssertNotHasAttr(self):
         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')
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index e9ef551d0b3ded..10c3b7e122371e 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -1372,16 +1372,20 @@ def assertHasAttr(self, obj, name, msg=None):
         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,

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to