You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Discrepancy between isinstance() and issubclass() for union types #88772
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=Noneclosed_at=<Date2021-07-14.05:21:16.922>created_at=<Date2021-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=<Date2021-07-14.05:21:16.921>user='https://github.com/serhiy-storchaka'
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.
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.
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:
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)
>>> classBadMeta(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, 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.
New changeset 8198905 by Serhiy Storchaka in branch 'main': bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. (GH-27120) 8198905
New changeset b42eee7 by Miss Islington (bot) in branch '3.10': bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. (GH-27120) b42eee7
Activity
serhiy-storchaka commentedon Jul 12, 2021
I do not know what behavior is correct.
gvanrossum commentedon Jul 12, 2021
Ken Jin, should we treat type(None) as a subclass of int|None?
serhiy-storchaka commentedon Jul 13, 2021
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 commentedon Jul 13, 2021
@serhiy
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,
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:
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?
gvanrossum commentedon Jul 13, 2021
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 commentedon Jul 13, 2021
@serhiy, can I work on converting None to type(None) please?
serhiy-storchaka commentedon Jul 13, 2021
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 commentedon Jul 13, 2021
PR 27120 fixes __instancecheck__ and __subclasscheck__. Converting None to type(None) will be done in other PR.
Fidget-Spinner commentedon Jul 13, 2021
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 :).
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 commentedon Jul 14, 2021
New changeset 8198905 by Serhiy Storchaka in branch 'main':
bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. (GH-27120)
8198905
miss-islington commentedon Jul 14, 2021
New changeset b42eee7 by Miss Islington (bot) in branch '3.10':
bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. (GH-27120)
b42eee7