-
-
Notifications
You must be signed in to change notification settings - Fork 978
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
Fix empty str(exception) when initialized with kwargs #2181
Fix empty str(exception) when initialized with kwargs #2181
Conversation
This makes sense to me after reading the upstream issue, thanks for contributing! It might be nice to just add a new test case for this instead of changing the existing |
tests/test_exceptions.py
Outdated
detail = "Not Found: foo" | ||
exception = HTTPException(status_code=404, detail=detail) | ||
assert str(exception) == detail | ||
assert repr(exception) == (f"HTTPException(status_code=404, detail='{detail}')") |
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.
assert repr(exception) == (f"HTTPException(status_code=404, detail='{detail}')") | |
assert repr(exception) == f"HTTPException(status_code=404, detail='{detail}')" |
tests/test_exceptions.py
Outdated
reason = "Policy Violation" | ||
exception = WebSocketException(code=1008, reason=reason) | ||
assert str(exception) == reason | ||
assert repr(exception) == (f"WebSocketException(code=1008, reason='{reason}')") |
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.
assert repr(exception) == (f"WebSocketException(code=1008, reason='{reason}')") | |
assert repr(exception) == f"WebSocketException(code=1008, reason='{reason}')" |
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.
Please implement the __str__
instead of super().__init__(self.detail)
.
Also, on the __str__
, I think we can have the same implementation as the __repr__
.
On DRF, they use only the detail: https://github.com/encode/django-rest-framework/blob/376a5cbbba3f8df9c9db8c03a7c8fa2a6e6c05f4/rest_framework/exceptions.py#L99C1-L117
On flask, they use all the information: https://github.com/pallets/werkzeug/blob/82b5ac433c54b8d03cb0de9fdc16a4c58b935860/src/werkzeug/exceptions.py#L65-L85
@Kludex for what it's worth, I don't think that
Using the The linked Flask code omits the type from the |
Yeah, I pasted the wrong part of the implementation. By "they use all the information" I mean that the
Fair enough. Is there any literature that has a recommendation on how the |
For sub class of Python >>> str(HTTPException(404, "Not found"))
"(404, 'Not found')"
Yes, |
I have re-implemented it referring Flask, https://github.com/pallets/werkzeug/blob/main/src/werkzeug/exceptions.py#L164. And I think it is somehow backward compatible in this way, because |
Why do we need |
I think it's desired practice in Python to always call Is it necessary to call super().init() explicitly in python?
Is calling the superclass constructor in a subclass really important?
|
Doesn't the |
Thanks @BoWuGit 👍 |
Happy to be merged, thanks again. |
Co-authored-by: Marcelo Trylesinski <[email protected]>
This commit upgrades FastAPI to the newest version, adding an odd piece of code to our exception handling to enable the upgrade. The odd piece of code is needed because of FastAPI 0.108.0, which upgrades Starlette to a newer version than 0.29.0, which introduces a custom `__str__` method to `HTTPException`, which breaks our exception interface. See: encode/starlette#2181 for details. The odd piece of code simply circumvents the `__str__` implementation, and uses the old `Exception.__str__` behavior instead, ensuring backwards compatibility and thus ensuring that the upgrade is not a breaking change.
This commit upgrades FastAPI to the newest version, adding an odd piece of code to our exception handling to enable the upgrade. The odd piece of code is needed because of FastAPI 0.108.0, which upgrades Starlette to a newer version than 0.29.0, which introduces a custom `__str__` method to `HTTPException`, which breaks our exception interface. See: encode/starlette#2181 for details. The odd piece of code simply circumvents the `__str__` implementation, and uses the old `Exception.__str__` behavior instead, ensuring backwards compatibility and thus ensuring that the upgrade is not a breaking change. The newer version of FastAPI also required an bugfix upgrade of RAMQP, to version 9.1.1 as prior versions of RAMQP does not work with newer versions of FastAPI.
This commit upgrades FastAPI to the newest version, adding an odd piece of code to our exception handling to enable the upgrade. The odd piece of code is needed because of FastAPI 0.108.0, which upgrades Starlette to a newer version than 0.29.0, which introduces a custom `__str__` method to `HTTPException`, which breaks our exception interface. See: encode/starlette#2181 for details. The odd piece of code simply circumvents the `__str__` implementation, and uses the old `Exception.__str__` behavior instead, ensuring backwards compatibility and thus ensuring that the upgrade is not a breaking change. The newer version of FastAPI also required an bugfix upgrade of RAMQP, to version 9.1.1 as prior versions of RAMQP does not work with newer versions of FastAPI.
Add a
super().__init__
call to sub class of Python standardException
, for two reasons:HTTPException
is initialized with kwargs,str(HTTPException)
is empty, which is just the case I met in this pytest error case.Thanks for your attention.