Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added tests to show that names from denylist still can be used with a…
… spec
  • Loading branch information
cklein committed Jan 5, 2023
commit 2bd97a17b535de645c873c0f2553aad91a28767f
18 changes: 18 additions & 0 deletions Lib/test/test_unittest/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1647,14 +1647,32 @@ def test_mock_unsafe(self):
m.assrt_foo_call()
with self.assertRaisesRegex(AttributeError, msg):
m.called_once_with()
with self.assertRaisesRegex(AttributeError, msg):
m.called_once()
with self.assertRaisesRegex(AttributeError, msg):
m.has_calls()

class Foo(object):
def called_once(self):
pass

def has_calls(self):
pass

m = Mock(spec=Foo)
m.called_once()
m.has_calls()

m.called_once.assert_called_once()
m.has_calls.assert_called_once()

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()
m.called_once_with()
m.has_calls()

Expand Down
6 changes: 3 additions & 3 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,9 +1235,9 @@ 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*, *assrt*, or *called_* will raise an
AttributeError. Passing `unsafe=True` will allow access to
these attributes.
*assert*, *assret*, *asert*, *aseert*, *assrt*, or an attribute name that matches
an assertion method without the prefix *assert_* will raise an
AttributeError. Passing `unsafe=True` will allow access to these attributes.

* `wraps`: Item for the mock object to wrap. If `wraps` is not None then
calling the Mock will pass the call through to the wrapped object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +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.
attribute that matches the name of an existing assertion is accessed, e.g. ``called_once`` instead of ``assert_called_once``.
This is in addition to this already happening for accessing attributes with prefixes assert, assret, asert, aseert, and assrt.
Comment thread
merwok marked this conversation as resolved.
Outdated