Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions 20 Lib/test/test_unittest/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
8 changes: 6 additions & 2 deletions 8 Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.