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

Fix exception causes all over the codebase #3681

Merged
merged 1 commit into from Jun 14, 2020

Conversation

cool-RR
Copy link
Contributor

@cool-RR cool-RR commented Jun 12, 2020

I recently went over Matplotlib, Pandas and NumPy, fixing a small mistake in the way that Python 3's exception chaining is used. If you're interested, I can do it here too. I've done it on just one file right now.

The mistake is this: In some parts of the code, an exception is being caught and replaced with a more user-friendly error. In these cases the syntax raise new_error from old_error needs to be used.

Python 3's exception chaining means it shows not only the traceback of the current exception, but that of the original exception (and possibly more.) This is regardless of raise from. The usage of raise from tells Python to put a more accurate message between the tracebacks. Instead of this:

During handling of the above exception, another exception occurred:

You'll get this:

The above exception was the direct cause of the following exception:

The first is inaccurate, because it signifies a bug in the exception-handling code itself, which is a separate situation than wrapping an exception.

Let me know what you think!

@coveralls
Copy link

coveralls commented Jun 12, 2020

Coverage Status

Coverage remained the same at 90.69% when pulling 1072cf8 on cool-RR:patch-1 into 201daa6 on PyCQA:master.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

I agree with the changes, but are those really the only occurrence of this in the whole pylint's codebase?

@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 13, 2020

I'm happy to hear that :)

Below is a list of places where this mistake might be happening. After we're done with this PR, I'll try to find time to do the rest. Note that there might be false positives here:

pylint/checkers/base.py: 400
pylint/checkers/spelling.py: 48
pylint/checkers/typecheck.py: 541
pylint/checkers/utils.py: 537
pylint/checkers/utils.py: 569
pylint/checkers/utils.py: 595
pylint/config/option.py: 90
pylint/lint/pylinter.py: 513
pylint/pyreverse/vcgutils.py: 202
tests/functional/i/invalid_exceptions_raised.py: 91
tests/functional/i/invalid_exceptions_raised.py: 105
tests/functional/m/misplaced_bare_raise.py: 83
tests/functional/n/no_else_raise.py: 110
tests/functional/r/regression_3416_unused_argument_raise.py: 12
tests/functional/s/stop_iteration_inside_generator.py: 72
tests/functional/t/try_except_raise.py: 11
tests/functional/t/try_except_raise.py: 43
tests/functional/t/try_except_raise.py: 52

Also: Would you be open to adding a PyLint rule that catches this mistake in people's code?

@Pierre-Sassoulas
Copy link
Member

Let's do all the similar fixes directly in this PR. No need to change the functional tests, everything in tests/functional/ is not supposed to be clean code that we maintain. Quite the opposite it's supposed to trigger problems :)

Also, yes, I think that could become a pylint's convention message.

@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 13, 2020

Changed this PR to include all instances of this problem in the codebase.

@cool-RR cool-RR changed the title Fix exception causes in checkers/utils.py Fix exception causes all over the codebase Jun 13, 2020
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Thank you for taking the time to change this :)

@Pierre-Sassoulas Pierre-Sassoulas merged commit 7bf1176 into pylint-dev:master Jun 14, 2020
@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 14, 2020

My pleasure :)

@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 24, 2020

I just made a blog post about this change here: https://blog.ram.rachum.com/post/621791438475296768/improving-python-exception-chaining-with

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants