-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-33018: Improve issubclass() error checking and message. #5944
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
bpo-33018: Improve issubclass() error checking and message. #5944
Conversation
Here's a build with this patch applied showing the fix in action: Python 3.8.0a0 (heads/master-dirty:f0daa880a4, Mar 1 2018, 10:46:05)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections.abc import Reversible
>>> issubclass([1, 2, 3], Reversible)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jab/src/cpython/Lib/abc.py", line 143, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class |
Check https://bugs.python.org/issue32999 please, some core devs don't want to limit the first arg only to type objects. |
Please create new issue and discuss on it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I think this looks good. Could you please add a NEWS item?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you revert fc7df0e too?
((PyTypeObject *)subclass)->tp_mro
must be valid tuple.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I don't think we need to revert it. First of all the tests should stay, so I would just make another PR in addition to this one. Or you want to add the change to |
Yes. I didn't mean revert adding test. |
Just added the NEWS entry: I have made the requested changes; please review again. First time (getting a patch accepted (🎉!) and) using Thanks for helping to get this landed! |
Thanks for making the requested changes! @methane, @ilevkivskyi: please review the changes made to this pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will create another PR later.
Thanks! Don't forget to backport your PR to 3.7, as I am backporting this one to 3.7 as @gvanrossum suggested. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Thanks @jab for the PR, and @ilevkivskyi for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
GH-6188 is a backport of this pull request to the 3.7 branch. |
@jab Congrats on your first contribution! |
Thanks for making it possible! |
bpo-33018 (pythonGH-5944) fixed bpo-32999 too. So fc7df0e is not required anymore. Revert it except test case.
bpo-33018 (pythonGH-5944) fixed bpo-32999 too. So fc7df0e is not required anymore. Revert it except test case. (cherry picked from commit f757b72) Co-authored-by: INADA Naoki <methane@users.noreply.github.com>
…-5944) This improves error message for situations when a non-class is checked w.r.t. an abstract base class.
bpo-33018 (pythonGH-5944) fixed bpo-32999 too. So fc7df0e is not required anymore. Revert it except test case.
If you try something like
issubclass('not a class', str)
, you get a helpful error message that immediately clues you in on what you did wrong:("AHA! I meant isinstance there. Thanks, friendly error message!")
But if you try this with some ABC, the error message is much less friendly!
("WTF just went wrong?" Several more minutes of head-scratching ensues. Maybe a less experienced Python programmer who hits this hasn't seen weakrefs before and gets overwhelmed, maybe needlessly proceeding down a deep rabbit hole before realizing no knowledge of weakrefs was required to understand what they did wrong.)
Or how about this example:
Here you don't even get the same type of error (
AttributeError
rather thanTypeError
), which seems unintentionally inconsistent.This trivial patch fixes this, and will hopefully save untold numbers of future Python programmers some time and headache.
Let me know if any further changes are required, and thanks in advance for reviewing.
https://bugs.python.org/issue33018