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
Changed documentation
  • Loading branch information
cklein committed Jan 6, 2023
commit 01946cb02964f45985c5ec83f52663bfd115aa9b
10 changes: 6 additions & 4 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ def _calls_repr(self, prefix="Calls"):
return f"\n{prefix}: {safe_repr(self.mock_calls)}."


# gh-100690 Denylist for forbidden attribute names in safe mode
# Denylist for forbidden attribute names in safe mode
ATTRIB_DENY_LIST = {name.removeprefix("assert_") for name in dir(NonCallableMock) if name.startswith("assert_")}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much prefer this approach! 🎉

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being late to the party, but anyways :)

I think that ATTRIB_DENY_LIST is an implementation detail, so it should be _ATTRIB_DENY_LIST. And also, it is not suited for mutation, isn't it? So, it should be a frozenset instead :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this PR is already merged, I've opened #100819 with this proposal. Feel free to close if it should be public and mutable!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already non-public by not being included in __all__, and the mutability is not really an issue.



Expand Down Expand Up @@ -1235,9 +1235,11 @@ 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 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.
*assert*, *assret*, *asert*, *aseert*, or *assrt* raises an AttributeError.
Additionally, an AttributeError is raised when accessing
attributes that match the name of an assertion method without the prefix
`assert_`, e.g. accessing `called_once` instead of `assert_called_once`.
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 that matches the name of an existing assertion is accessed, e.g. ``called_once`` instead of ``assert_called_once``.
Mock objects which are not unsafe will now raise an AttributeError when accessing an
attribute that matches the name of an assertion but without the prefix ``assert_``, e.g. accessing ``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