Commit b9b08cd9 authored by Zackery Spytz's avatar Zackery Spytz Committed by Kushal Das

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

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

* Make the requested changes.
parent 6bd81734
...@@ -572,7 +572,8 @@ class NonCallableMock(Base): ...@@ -572,7 +572,8 @@ class NonCallableMock(Base):
raise AttributeError(name) raise AttributeError(name)
if not self._mock_unsafe: if not self._mock_unsafe:
if name.startswith(('assert', 'assret')): if name.startswith(('assert', 'assret')):
raise AttributeError(name) raise AttributeError("Attributes cannot start with 'assert' "
"or 'assret'")
result = self._mock_children.get(name) result = self._mock_children.get(name)
if result is _deleted: if result is _deleted:
......
...@@ -1453,9 +1453,10 @@ class MockTest(unittest.TestCase): ...@@ -1453,9 +1453,10 @@ class MockTest(unittest.TestCase):
#Issue21238 #Issue21238
def test_mock_unsafe(self): def test_mock_unsafe(self):
m = Mock() m = Mock()
with self.assertRaises(AttributeError): msg = "Attributes cannot start with 'assert' or 'assret'"
with self.assertRaisesRegex(AttributeError, msg):
m.assert_foo_call() m.assert_foo_call()
with self.assertRaises(AttributeError): with self.assertRaisesRegex(AttributeError, msg):
m.assret_foo_call() m.assret_foo_call()
m = Mock(unsafe=True) m = Mock(unsafe=True)
m.assert_foo_call() m.assert_foo_call()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment