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. |
Fidget-Spinner
left a comment
There was a problem hiding this comment.
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.
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.
Oh, I forget about this. Thank you for reminder.
| 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.
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 <[email protected]>
…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 <[email protected]>
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