Skip to content

Commit 154012f

Browse files
committed
Prevent calling assert functions without the prefix "assert_" in safe mode
Raise an AttributeError when calling e.g. `mock_obj.called_once` instead of `mock_obj.assert_called_once`.
1 parent d7e7f79 commit 154012f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Lib/test/test_unittest/testmock/testmock.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,12 +1645,18 @@ 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()
1650+
with self.assertRaisesRegex(AttributeError, msg):
1651+
m.has_calls()
16481652
m = Mock(unsafe=True)
16491653
m.assert_foo_call()
16501654
m.assret_foo_call()
16511655
m.asert_foo_call()
16521656
m.aseert_foo_call()
16531657
m.assrt_foo_call()
1658+
m.called_once_with()
1659+
m.has_calls()
16541660

16551661
#Issue21262
16561662
def test_assert_not_called(self):

Lib/unittest/mock.py

Lines changed: 6 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')) or name in ATTRIB_DENY_LIST:
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.")
@@ -1062,6 +1062,10 @@ def _calls_repr(self, prefix="Calls"):
10621062
return f"\n{prefix}: {safe_repr(self.mock_calls)}."
10631063

10641064

1065+
# gh-100690 Denylist for forbidden method names in safe mode
1066+
ATTRIB_DENY_LIST = {name.removeprefix("assert_") for name in dir(NonCallableMock) if name.startswith("assert_")}
1067+
1068+
10651069
class _AnyComparer(list):
10661070
"""A list which checks if it contains a call which may have an
10671071
argument of ANY, flipping the components of item and self from
@@ -1231,7 +1235,7 @@ class or instance) that acts as the specification for the mock object. If
12311235
`return_value` attribute.
12321236
12331237
* `unsafe`: By default, accessing any attribute whose name starts with
1234-
*assert*, *assret*, *asert*, *aseert* or *assrt* will raise an
1238+
*assert*, *assret*, *asert*, *aseert*, *assrt*, or *called_* will raise an
12351239
AttributeError. Passing `unsafe=True` will allow access to
12361240
these attributes.
12371241
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)