Explicitly chain exceptions with raise ... from ...
#261
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.
Before submitting
What does this PR do?
I'd like to suggest a fix in the way that Python 3's exception chaining is used. I recently submitted a PR and got it merged into PyTorchLightning at Lightning-AI/pytorch-lightning#3750, and I'd like to apply the fix here as well.
As described in detail in this article, exception chaining (PEP 3134) can be used to make exceptions more user-friendly, and in that case, the syntax
raise ... from ...
needs to be used.When
raise ... from ...
is used, there will be a line sayingThe above exception was the direct cause of the following exception:
between tracebacks. However, when implicitly chaining exceptions (meaning whenraise ... from ...
is not used), the message will beDuring handling of the above exception, another exception occurred:
which can confuse users.Specifically, the following should be used in order to explicitly chain exceptions:
instead of:
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃