-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. #27120
bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. #27120
Conversation
…type. * Fix issubclass() for None. E.g. issubclass(type(None), int | None) returns now True. * Fix issubclass() for virtual subclasses. E.g. issubclass(dict, int | collections.abc.Mapping) returns now True. * Fix crash in isinstance() if the check for one of items raises exception.
We don't need to backport to 3.9, because union is 3.10 only. |
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. Once again, thanks for fixing this nasty crash!
x = int | BadMeta('A', (), {}) | ||
self.assertTrue(issubclass(int, x)) | ||
self.assertRaises(ZeroDivisionError, issubclass, list, x) | ||
|
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.
Maybe you can add your test from the bpo for virtual subclasses issubclass(dict, int | collections.abc.Mapping)
?
Then again we may not need that test. If the previous tests throw the ZeroDivisionError, then that means it's already looking into the __subclasscheck__
dunder.
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.
Oh, I forget about this. Thank you for reminder.
@@ -93,8 +99,17 @@ union_subclasscheck(PyObject *self, PyObject *instance) | |||
Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args); | |||
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { | |||
PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); | |||
if (PyType_Check(arg) && (PyType_IsSubtype((PyTypeObject *)instance, (PyTypeObject *)arg) != 0)) { | |||
Py_RETURN_TRUE; | |||
if (arg == Py_None) { |
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.
Reminder for myself that this can probably be removed when we do the auto conversion of None to type(None).
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10. |
GH-27132 is a backport of this pull request to the 3.10 branch. |
…type. (pythonGH-27120) * Fix issubclass() for None. E.g. issubclass(type(None), int | None) returns now True. * Fix issubclass() for virtual subclasses. E.g. issubclass(dict, int | collections.abc.Mapping) returns now True. * Fix crash in isinstance() if the check for one of items raises exception. (cherry picked from commit 8198905) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
…type. (GH-27120) * Fix issubclass() for None. E.g. issubclass(type(None), int | None) returns now True. * Fix issubclass() for virtual subclasses. E.g. issubclass(dict, int | collections.abc.Mapping) returns now True. * Fix crash in isinstance() if the check for one of items raises exception. (cherry picked from commit 8198905) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
E.g. issubclass(type(None), int | None) returns now True.
E.g. issubclass(dict, int | collections.abc.Mapping) returns now True.
https://bugs.python.org/issue44606