Skip to content

Commit 25e26d8

Browse files
ckleinChristian Klein
authored andcommitted
Prevent prefix "called_" for attributes of safe mock objects
1 parent 9dee973 commit 25e26d8

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

Lib/test/test_unittest/testmock/testmock.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,12 +1645,15 @@ def test_mock_unsafe(self):
16451645
m.aseert_foo_call()
16461646
with self.assertRaisesRegex(AttributeError, msg):
16471647
m.assrt_foo_call()
1648+
with self.assertRaisesRegex(AttributeError, msg):
1649+
m.called_once_with()
16481650
m = Mock(unsafe=True)
16491651
m.assert_foo_call()
16501652
m.assret_foo_call()
16511653
m.asert_foo_call()
16521654
m.aseert_foo_call()
16531655
m.assrt_foo_call()
1656+
m.called_once_with()
16541657

16551658
#Issue21262
16561659
def test_assert_not_called(self):

Lib/unittest/mock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def __getattr__(self, name):
653653
elif _is_magic(name):
654654
raise AttributeError(name)
655655
if not self._mock_unsafe:
656-
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
656+
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt', 'called_')):
657657
raise AttributeError(
658658
f"{name!r} is not a valid assertion. Use a spec "
659659
f"for the mock if {name!r} is meant to be an attribute.")
@@ -1231,7 +1231,7 @@ class or instance) that acts as the specification for the mock object. If
12311231
`return_value` attribute.
12321232
12331233
* `unsafe`: By default, accessing any attribute whose name starts with
1234-
*assert*, *assret*, *asert*, *aseert* or *assrt* will raise an
1234+
*assert*, *assret*, *asert*, *aseert*, *assrt*, or *called_* will raise an
12351235
AttributeError. Passing `unsafe=True` will allow access to
12361236
these attributes.
12371237
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Mock objects which are not unsafe will now raise an AttributeError if an
2+
attribute with the prefix called_ is accessed, in addition to this already
3+
happening for the prefixes assert, assret, asert, aseert, assrt.

0 commit comments

Comments
 (0)