Skip to content
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

Improve re-raises of Exceptions #4142

Merged
merged 2 commits into from
Jul 24, 2022
Merged

Conversation

zundertj
Copy link
Collaborator

@zundertj zundertj commented Jul 24, 2022

When we use try-except in our code base, often we raise a new exception to replace the original exception. It is good practice to either wrap around the original exception (from exc), or bin the original exception (from None) if the new exception is a self-contained description of what happened. The latter is for instance the case when we wrap around a dict, catch the KeyError, and provide more meaningful Exception with message, as the KeyError is not what the user is after, it wants to know that dtype1 cannot be converted to dtype2, etc.

Without re-raise, you will see During handling of the above exception, another exception occurred, which means the two exceptions, original and new, are treated unrelated. With from exc, you will see The above exception was the direct cause of the following exception:. With from None, you will get a clean traceback without the original exception.

Found all cases using pylint (pylint polars --disable=all --enable=raise-missing-from). Discussion on the potential addition of pylint in CI takes place in #4044.

Background reading: https://stefan.sofa-rockers.org/2020/10/28/raise-from/

zundertj added 2 commits July 24, 2022 14:44
When we use try-except in our code base, often we raise a new exception to replace the original exception. It is good practice to either wrap around the original exception (`from exc`), or bin the original exception (`from None`) if the new exception is a self-contained description of what happened. The latter is for instance the case when we wrap around a dict, catch the KeyError, and provide more meaningful Exception with message, as the `KeyError` is not what the user is after, it wants to know that dtype1 cannot be converted to dtype2, etc.

Without re-raise, you will see `During handling of the above exception, another exception occurred`, which means the two exceptions, original and new, are treated unrelated. With `from exc`, you will see `The above exception was the direct cause of the following exception:`. With `from None`, you will get a clean traceback without the original exception.

Found all cases using pylint (`pylint polars --disable=all --enable=raise-missing-from`). Discussion on the rollout of pylint in CI takes place in pola-rs#4044.

Background reading: https://stefan.sofa-rockers.org/2020/10/28/raise-from/
@github-actions github-actions bot added the python Related to Python Polars label Jul 24, 2022
@zundertj zundertj marked this pull request as ready for review July 24, 2022 12:57
@codecov-commenter
Copy link

codecov-commenter commented Jul 24, 2022

Codecov Report

Merging #4142 (3df0cc4) into master (5f8603b) will increase coverage by 0.01%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master    #4142      +/-   ##
==========================================
+ Coverage   58.42%   58.43%   +0.01%     
==========================================
  Files         436      436              
  Lines       66888    66888              
==========================================
+ Hits        39077    39086       +9     
+ Misses      27811    27802       -9     
Impacted Files Coverage Δ
py-polars/polars/datatypes.py 60.50% <ø> (ø)
py-polars/polars/datatypes_constructor.py 53.33% <ø> (ø)
py-polars/polars/internals/frame.py 27.00% <0.00%> (ø)
py-polars/polars/internals/lazy_frame.py 26.23% <0.00%> (ø)
py-polars/polars/io.py 16.48% <ø> (ø)
polars/polars-core/src/utils/mod.rs 53.03% <0.00%> (+0.17%) ⬆️
polars/polars-io/src/csv/utils.rs 54.69% <0.00%> (+0.23%) ⬆️
polars/polars-io/src/csv/buffer.rs 44.72% <0.00%> (+1.75%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5f8603b...3df0cc4. Read the comment docs.

Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement!

@ritchie46 ritchie46 merged commit 2ee2d01 into pola-rs:master Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants