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

bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. #27120

Merged
merged 2 commits into from
Jul 14, 2021

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jul 13, 2021

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

https://bugs.python.org/issue44606

…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.
@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error needs backport to 3.9 only security fixes needs backport to 3.10 only security fixes labels Jul 13, 2021
@Fidget-Spinner
Copy link
Member

We don't need to backport to 3.9, because union is 3.10 only.

@Fidget-Spinner Fidget-Spinner removed the needs backport to 3.9 only security fixes label Jul 13, 2021
Copy link
Member

@Fidget-Spinner Fidget-Spinner left a 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)

Copy link
Member

@Fidget-Spinner Fidget-Spinner Jul 13, 2021

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.

Copy link
Member Author

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) {
Copy link
Member

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

@serhiy-storchaka serhiy-storchaka merged commit 8198905 into python:main Jul 14, 2021
@miss-islington
Copy link
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-27132 is a backport of this pull request to the 3.10 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Jul 14, 2021
@serhiy-storchaka serhiy-storchaka deleted the union-instancecheck branch July 14, 2021 04:35
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 14, 2021
…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>
miss-islington added a commit that referenced this pull request Jul 14, 2021
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants