Navigation Menu

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

Discrepancy between isinstance() and issubclass() for union types #88772

Closed
serhiy-storchaka opened this issue Jul 12, 2021 · 11 comments
Closed
Labels
3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@serhiy-storchaka
Copy link
Member

BPO 44606
Nosy @gvanrossum, @serhiy-storchaka, @miss-islington, @Fidget-Spinner
PRs
  • bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. #27120
  • [3.10] bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. (GH-27120) #27132
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-07-14.05:21:16.922>
    created_at = <Date 2021-07-12.05:33:29.993>
    labels = ['interpreter-core', '3.10', 'type-crash', '3.11']
    title = 'Discrepancy between isinstance() and issubclass() for union types'
    updated_at = <Date 2021-07-14.05:21:16.921>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2021-07-14.05:21:16.921>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-07-14.05:21:16.922>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2021-07-12.05:33:29.993>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44606
    keywords = ['patch']
    message_count = 11.0
    messages = ['397281', '397355', '397385', '397389', '397405', '397417', '397430', '397431', '397432', '397463', '397464']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'miss-islington', 'kj']
    pr_nums = ['27120', '27132']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue44606'
    versions = ['Python 3.10', 'Python 3.11']

    @serhiy-storchaka
    Copy link
    Member Author

    1. Different handling of None:
    >>> isinstance(None, int | type(None))
    True
    >>> issubclass(type(None), int | type(None))
    True
    >>> isinstance(None, int | None)
    True
    >>> issubclass(type(None), int | None)
    False
    1. Different handling of virtual subclasses:
    >>> import collections.abc
    >>> isinstance({}, int | collections.abc.Mapping)
    True
    >>> issubclass(dict, int | collections.abc.Mapping)
    False

    I do not know what behavior is correct.

    @serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jul 12, 2021
    @gvanrossum
    Copy link
    Member

    Ken Jin, should we treat type(None) as a subclass of int|None?

    @serhiy-storchaka
    Copy link
    Member Author

    I have found this when refactored the code of Objects/unionobject.c. So I have a patch which fixes this, but I want to make sure what behavior is considered correct.

    @Fidget-Spinner
    Copy link
    Member

    @serhiy

    1. Different handling of virtual subclasses:

    This looks like a bug. I think the union form of isinstance/issubclass should have the same behavior as the tuple form. We promise checking of virtual classes in the docs for union types:
    https://docs.python.org/3.10/library/functions.html#issubclass

    The fix seems to be changing one line of code in Objects/unionobject.c from PyType_IsSubtype to PyObject_IsSubclass. Since it isn't a large change and it's a bug, I think we can backport it to 3.10 too.

    @serhiy and Guido,

    1. Different handling of None:

    Ken Jin, should we treat type(None) as a subclass of int|None?

    I think we can avoid this special case altogether by converting all None to type(None) when creating _Py_Union. This is what the typing.Union counterpart does, and it is also what PEP-484 says about None https://www.python.org/dev/peps/pep-0484/#using-none:

    >>> Union[None, str].__args__
    (<class 'NoneType'>, <class 'str'>)

    Though I know some people are opinionated about None -> type(None).

    This would require adding one more check at https://github.com/python/cpython/blob/3.10/Objects/unionobject.c#L266 .
    Maybe this?

    if(Py_IsNone(i_element))
        i_element = &_PyNone_Type

    @gvanrossum
    Copy link
    Member

    Converting None to type(None) is fine, as long as the str() /repr()
    converts it back, e.g. repr(int | None) should print just that, not
    NoneType.--
    --Guido (mobile)

    @Fidget-Spinner
    Copy link
    Member

    @serhiy, can I work on converting None to type(None) please?

    @serhiy-storchaka
    Copy link
    Member Author

    1. There is also a crash in isinstance():
    >>> class BadMeta(type):
    ...     def __instancecheck__(cls, inst):
    ...         1/0
    ... 
    >>> x = int | BadMeta('A', (), {})
    >>> isinstance([], x)
    Fatal Python error: _Py_CheckFunctionResult: a function returned a result with an exception set
    Python runtime state: initialized
    Traceback (most recent call last):
      File "<stdin>", line 3, in __instancecheck__
    ZeroDivisionError: division by zero
    
    The above exception was the direct cause of the following exception:

    SystemError: <built-in method __instancecheck__ of types.Union object at 0x7f024bbb8960> returned a result with an exception set

    Current thread 0x00007f024beb1280 (most recent call first):
    File "<stdin>", line 1 in <module>
    Aborted (core dumped)

    @serhiy-storchaka serhiy-storchaka added type-crash A hard crash of the interpreter, possibly with a core dump and removed type-bug An unexpected behavior, bug, or error labels Jul 13, 2021
    @serhiy-storchaka
    Copy link
    Member Author

    PR 27120 fixes __instancecheck__ and __subclasscheck__. Converting None to type(None) will be done in other PR.

    @Fidget-Spinner
    Copy link
    Member

    1. There is also a crash in isinstance():

    That's unfortunate :(.

    Serhiy, thanks for catching all these bugs in union. I recently realized you probably made 50% of all bug reports for union and they're very much appreciated :).

    Converting None to type(None) will be done in other PR.

    Alright. Do tell me if you're too busy and want me to take it up instead. I'll be glad to help.

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 8198905 by Serhiy Storchaka in branch 'main':
    bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. (GH-27120)
    8198905

    @miss-islington
    Copy link
    Contributor

    New changeset b42eee7 by Miss Islington (bot) in branch '3.10':
    bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. (GH-27120)
    b42eee7

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants