Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
serhiy-storchaka merged 2 commits into
python:mainpython/cpython:mainfrom
serhiy-storchaka:union-instancecheckserhiy-storchaka/cpython:union-instancecheckCopy head branch name to clipboard
Jul 14, 2021
Merged

bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type.#27120
serhiy-storchaka merged 2 commits into
python:mainpython/cpython:mainfrom
serhiy-storchaka:union-instancecheckserhiy-storchaka/cpython:union-instancecheckCopy head branch name to clipboard

Conversation

@serhiy-storchaka

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

Copy link
Copy Markdown
Member
  • 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.
@Fidget-Spinner

Copy link
Copy Markdown
Member

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

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

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!

Comment thread Lib/test/test_types.py
x = int | BadMeta('A', (), {})
self.assertTrue(issubclass(int, x))
self.assertRaises(ZeroDivisionError, issubclass, list, x)

@Fidget-Spinner Fidget-Spinner Jul 13, 2021

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

Comment thread Objects/unionobject.c
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
Copy Markdown
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
Copy Markdown
Contributor

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

@bedevere-bot

Copy link
Copy Markdown

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.