bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mo… · python/cpython@b9b08cd

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -572,7 +572,8 @@ def __getattr__(self, name):

572572

raise AttributeError(name)

573573

if not self._mock_unsafe:

574574

if name.startswith(('assert', 'assret')):

575-

raise AttributeError(name)

575+

raise AttributeError("Attributes cannot start with 'assert' "

576+

"or 'assret'")

576577
577578

result = self._mock_children.get(name)

578579

if result is _deleted:

Original file line numberDiff line numberDiff line change

@@ -1453,9 +1453,10 @@ def static_method(): pass

14531453

#Issue21238

14541454

def test_mock_unsafe(self):

14551455

m = Mock()

1456-

with self.assertRaises(AttributeError):

1456+

msg = "Attributes cannot start with 'assert' or 'assret'"

1457+

with self.assertRaisesRegex(AttributeError, msg):

14571458

m.assert_foo_call()

1458-

with self.assertRaises(AttributeError):

1459+

with self.assertRaisesRegex(AttributeError, msg):

14591460

m.assret_foo_call()

14601461

m = Mock(unsafe=True)

14611462

m.assert_foo_call()