Skip to content

Commit

Permalink
bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mo…
Browse files Browse the repository at this point in the history
…de (#12991)

* bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode

* Make the requested changes.
  • Loading branch information
ZackerySpytz authored and kushaldas committed May 8, 2019
1 parent 6bd8173 commit b9b08cd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Lib/unittest/mock.py
Expand Up @@ -572,7 +572,8 @@ def __getattr__(self, name):
raise AttributeError(name)
if not self._mock_unsafe:
if name.startswith(('assert', 'assret')):
raise AttributeError(name)
raise AttributeError("Attributes cannot start with 'assert' "
"or 'assret'")

result = self._mock_children.get(name)
if result is _deleted:
Expand Down
5 changes: 3 additions & 2 deletions Lib/unittest/test/testmock/testmock.py
Expand Up @@ -1453,9 +1453,10 @@ def static_method(): pass
#Issue21238
def test_mock_unsafe(self):
m = Mock()
with self.assertRaises(AttributeError):
msg = "Attributes cannot start with 'assert' or 'assret'"
with self.assertRaisesRegex(AttributeError, msg):
m.assert_foo_call()
with self.assertRaises(AttributeError):
with self.assertRaisesRegex(AttributeError, msg):
m.assret_foo_call()
m = Mock(unsafe=True)
m.assert_foo_call()
Expand Down

0 comments on commit b9b08cd

Please sign in to comment.