Skip to content

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

Closed

Description

@serhiy-storchaka
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']

    Activity

    1. serhiy-storchaka commented on Jul 12, 2021

      @serhiy-storchaka
      MemberAuthor
      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.

    2. added
      type-bugAn unexpected behavior, bug, or error
      3.11only security fixes
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      on Jul 12, 2021
    3. gvanrossum commented on Jul 12, 2021

      @gvanrossum
      Member

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

    4. serhiy-storchaka commented on Jul 13, 2021

      @serhiy-storchaka
      MemberAuthor

      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.

    5. Fidget-Spinner commented on Jul 13, 2021

      @Fidget-Spinner
      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
    6. gvanrossum commented on Jul 13, 2021

      @gvanrossum
      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)

    7. Fidget-Spinner commented on Jul 13, 2021

      @Fidget-Spinner
      Member

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

    8. serhiy-storchaka commented on Jul 13, 2021

      @serhiy-storchaka
      MemberAuthor
      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)

    9. added
      type-crashA hard crash of the interpreter, possibly with a core dump
      and removed
      type-bugAn unexpected behavior, bug, or error
      on Jul 13, 2021
    10. serhiy-storchaka commented on Jul 13, 2021

      @serhiy-storchaka
      MemberAuthor

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

    11. Fidget-Spinner commented on Jul 13, 2021

      @Fidget-Spinner
      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.

    12. serhiy-storchaka commented on Jul 14, 2021

      @serhiy-storchaka
      MemberAuthor

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

    13. miss-islington commented on Jul 14, 2021

      @miss-islington
      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

    14. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)end of life3.11only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions