Repository navigation
gh-100690: Prevent prefix "called_" for attributes of safe mock objects #100691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
154012f
26dc793
2bd97a1
01946cb
e9b8f73
674f5bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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_")} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for being late to the party, but anyways :) I think that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is already non-public by not being included in |
||
|
|
||
|
|
||
|
|
@@ -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 | ||
|
|
||
| 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. | ||
|
merwok marked this conversation as resolved.
Outdated
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much prefer this approach! 🎉