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 #8998

Merged
merged 1 commit into from
Jun 17, 2020
Merged

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!

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Looks good, and thanks for the explanation (which I had forgotten).

Could you do the other files in this project too?

@cool-RR cool-RR changed the title Fix exception causes in stubutil.py Fix exception causes all over the codebase Jun 17, 2020
@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 17, 2020

Yup, done.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Thanks! I wonder -- do you have a script to find these, or are you just good at grepping and reading the code? :-)

@gvanrossum gvanrossum merged commit d3edd60 into python:master Jun 17, 2020
@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 18, 2020

LOL, I did this exercise to a few dozen projects by now, and you're the first one to ask me that.

Yes, I have a script. It's too ugly and buggy to share, but basically it takes a GitHub repo and outputs a list of modules and lines in which this mistake occurs. The next step would be a script that automatically opens PRs :)

I wrote a more elegant version of it as a PyLint rule here, hopefully it gets accepted soon. It's likely to make a more long-term impact than me going through repos one-by-one.

@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.

2 participants