[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread miss-islington


miss-islington  added the comment:


New changeset b42eee78e7651693aa38c390f577e5d499dcf55d by Miss Islington (bot) 
in branch '3.10':
bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. 
(GH-27120)
https://github.com/python/cpython/commit/b42eee78e7651693aa38c390f577e5d499dcf55d


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:


New changeset 81989058de381108dfd0a4255b93d4fb34417002 by Serhiy Storchaka in 
branch 'main':
bpo-44606: Fix __instancecheck__ and __subclasscheck__ for the union type. 
(GH-27120)
https://github.com/python/cpython/commit/81989058de381108dfd0a4255b93d4fb34417002


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread miss-islington


Change by miss-islington :


--
nosy: +miss-islington
nosy_count: 3.0 -> 4.0
pull_requests: +25675
pull_request: https://github.com/python/cpython/pull/27132

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Ken Jin


Ken Jin  added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
type: behavior -> crash

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
keywords: +patch
pull_requests: +25665
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/27120

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

3. 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 "", line 3, in __instancecheck__
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

SystemError:  returned a result with an exception set

Current thread 0x7f024beb1280 (most recent call first):
  File "", line 1 in 
Aborted (core dumped)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Ken Jin


Ken Jin  added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Guido van Rossum


Guido van Rossum  added the comment:

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)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Ken Jin


Ken Jin  added the comment:

@Serhiy
> 2. 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__
(, )

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-13 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-12 Thread Guido van Rossum


Guido van Rossum  added the comment:

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

--
nosy: +kj

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44606] Discrepancy between isinstance() and issubclass() for union types

2021-07-11 Thread Serhiy Storchaka


New submission from Serhiy Storchaka :

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

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

--
components: Interpreter Core
messages: 397281
nosy: gvanrossum, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Discrepancy between isinstance() and issubclass() for union types
type: behavior
versions: Python 3.10, Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com