Added protections to AttributeErrors raised within properties #9455
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of Changes
Creates a
safe_property
decorator inrequest.py
that utilizes thewrap_attributeerrors
context manager to ensure all properties in the Request class properly handle anyAttributeError
s raised internally.Updates the
__getattr__
function to explicitly raise anAttributeError
rather than calling__getattribute__
again, as calling__getattribute__
can have unexpected side effects (see #9433).Adds a unit test to cover the case where a Parser raises an attribute error when the
Request.data
property is accessed.Benefits
AttributeError
s raised in properties such as from parsing will now produce sane error messages and will no longer result in errors being suppressed.Possible drawbacks
None that I am currently aware of.
Applicable issues
Additional information
There appears to already be a precedent for this type of handling in request.py with the
wrap_attributeerrors
context manager that is currently only used for authentication. This extends that behavior to other properties via a commonsafe_property
decorator that can be used instead of the builtinproperty
decorator.While I think adding a decorator such as
safe_property
that utilizeswrape_attributeerrors
is an elegant way of handling this, I'm also open to just adding thewrap_attributeerrors
context manager wherever needed.