Skip to content

Commit

Permalink
Prevent prefix "called_" for attributes of safe mock objects
Browse files Browse the repository at this point in the history
  • Loading branch information
cklein committed Jan 2, 2023
1 parent 9dee973 commit b84bc75
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Lib/test/test_unittest/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,12 +1645,15 @@ def test_mock_unsafe(self):
m.aseert_foo_call()
with self.assertRaisesRegex(AttributeError, msg):
m.assrt_foo_call()
with self.assertRaisesRegex(AttributeError, msg):
m.called_once_with()
m = Mock(unsafe=True)
m.assert_foo_call()
m.assret_foo_call()
m.asert_foo_call()
m.aseert_foo_call()
m.assrt_foo_call()
m.called_once_with()

#Issue21262
def test_assert_not_called(self):
Expand Down
4 changes: 2 additions & 2 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def __getattr__(self, name):
elif _is_magic(name):
raise AttributeError(name)
if not self._mock_unsafe:
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt', 'called_')):
raise AttributeError(
f"{name!r} is not a valid assertion. Use a spec "
f"for the mock if {name!r} is meant to be an attribute.")
Expand Down Expand Up @@ -1231,7 +1231,7 @@ class or instance) that acts as the specification for the mock object. If
`return_value` attribute.
* `unsafe`: By default, accessing any attribute whose name starts with
*assert*, *assret*, *asert*, *aseert* or *assrt* will raise an
*assert*, *assret*, *asert*, *aseert*, *assrt*, or *called_* will raise an
AttributeError. Passing `unsafe=True` will allow access to
these attributes.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Mock objects which are not unsafe will now raise an AttributeError if an
attribute with the prefix ``called_`` is accessed, in addition to this already
happening for the prefixes assert, assret, asert, aseert, assrt.

0 comments on commit b84bc75

Please sign in to comment.