[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-13 Thread Brandt Bucher
Change by Brandt Bucher : -- nosy: +brandtbucher ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: All typing related tests are fixed by updating types.GenericAlias.__getattribute__. So the only thing is left to make all tests passed is removing some special methods tests. The lookup of __instancecheck__ and __subclasscheck__ was changed by Benjamin

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Guido van Rossum
Guido van Rossum added the comment: I believe that the PEP 3119 example doesn't work (I've confirmed something simpler) but I have a feeling that the uses of __instancecheck__ in typing.py are actually okay. For example, isinstance(42, typing.Any) correctly calls _SpecialForm.__instancecheck__

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Raymond Hettinger
Raymond Hettinger added the comment: Here's an example in PEP 3119 that appears to work but actually bypasses the method: from abc import ABCMeta, abstractmethod class Sized(metaclass=ABCMeta): @abstractmethod def __hash__(self): return 0 @classmethod def

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: Most of failed tests are related to typing, but there is a purposed test for __instancecheck__ and __subclasscheck__ bypassing __getattr__ and __getattribute__ (test.test_descr.ClassPropertiesAndMethods.test_special_method_lookup). --

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Raymond Hettinger
Raymond Hettinger added the comment: I did a scan of the standard library and code in the wild. It looks like almost all uses are in metaclasses (which makes sense because that matches the AppendableSequence example in PEP 3119). However, the typing module does have some cases of

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: Failed tests: == FAIL: test_isinstance_with_or_union (test.test_isinstance.TestIsInstanceIsSubclass) -- Traceback (most

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Serhiy Storchaka
Change by Serhiy Storchaka : -- keywords: +patch pull_requests: +27789 stage: -> patch review pull_request: https://github.com/python/cpython/pull/29540 ___ Python tracker

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Raymond Hettinger
Raymond Hettinger added the comment: FWIW, I discovered the issue when experimenting with ways to use the class pattern in structural pattern matching. --- Code that should work but doesn't --- class Warm: def __instancecheck__(cls, inst): return inst in {'red',

[issue45791] __instancecheck__ being checked on type(cls) instead of cls

2021-11-12 Thread Raymond Hettinger
Change by Raymond Hettinger : -- title: __instancecheck__ being checked of type(cls) instead of cls -> __instancecheck__ being checked on type(cls) instead of cls ___ Python tracker

[issue45791] __instancecheck__ being checked of type(cls) instead of cls

2021-11-12 Thread Guido van Rossum
Guido van Rossum added the comment: I'm torn. It's been like this for a very long time and Serhiy's reasoning makes some sense. OTOH it *does* make more sense as it's written in the PEP. The basic idea is "the class has control over what is considered an instance of it" and it would be

[issue45791] __instancecheck__ being checked of type(cls) instead of cls

2021-11-12 Thread Raymond Hettinger
Raymond Hettinger added the comment: > As in most (but not all) cases of dunder methods it > is looked up in a class, ignoring instance attributes. That is true, but the starting point in this case is a class so the attribute lookup should be in that class, not its metaclass. Guido's

[issue45791] __instancecheck__ being checked of type(cls) instead of cls

2021-11-11 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: I think that the code is correct, and the documentation is not complete. As in most (but not all) cases of dunder methods it is looked up in a class, ignoring instance attributes. -- nosy: +serhiy.storchaka ___

[issue45791] __instancecheck__ being checked of type(cls) instead of cls

2021-11-11 Thread Raymond Hettinger
New submission from Raymond Hettinger : Per PEP 3119, "Overloading works as follows: The call isinstance(x, C) first checks whether C.__instancecheck__ exists, and if so, calls C.__instancecheck__(x) instead of its normal implementation." However, this doesn't work because the isinstance()