Fix empty str(exception) when initialized with kwargs#2181
Fix empty str(exception) when initialized with kwargs#2181Kludex merged 5 commits intoKludex:masterfrom
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.
| 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.
| assert repr(exception) == (f"WebSocketException(code=1008, reason='{reason}')") | |
| assert repr(exception) == f"WebSocketException(code=1008, reason='{reason}')" |
Kludex
left a comment
There was a problem hiding this comment.
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: Kludex/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: Kludex/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: Kludex/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:HTTPExceptionis initialized with kwargs,str(HTTPException)is empty, which is just the case I met in this pytest error case.Thanks for your attention.