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

ABCMeta.__subclasscheck__() doesn't support duck typing. #89010

Open
grahamd mannequin opened this issue Aug 6, 2021 · 1 comment
Open

ABCMeta.__subclasscheck__() doesn't support duck typing. #89010

grahamd mannequin opened this issue Aug 6, 2021 · 1 comment
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@grahamd
Copy link
Mannequin

grahamd mannequin commented Aug 6, 2021

BPO 44847

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 = None
created_at = <Date 2021-08-06.06:56:59.358>
labels = ['type-bug', 'library', '3.9', '3.10']
title = "ABCMeta.__subclasscheck__() doesn't support duck typing."
updated_at = <Date 2021-08-06.06:56:59.358>
user = 'https://bugs.python.org/grahamd'

bugs.python.org fields:

activity = <Date 2021-08-06.06:56:59.358>
actor = 'grahamd'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-08-06.06:56:59.358>
creator = 'grahamd'
dependencies = []
files = []
hgrepos = []
issue_num = 44847
keywords = []
message_count = 1.0
messages = ['399060']
nosy_count = 1.0
nosy_names = ['grahamd']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue44847'
versions = ['Python 3.9', 'Python 3.10']

@grahamd
Copy link
Mannequin Author

grahamd mannequin commented Aug 6, 2021

The Python standard library has two effective implementations of helpers for the ABCMeta class. A C implementation, and a pure Python version which is only used if the C implementation isn't available (perhaps for PyPy).

These two implementations behave differently.

Specifically, the ABCMeta.__subclasscheck__() implementation for the C version doesn't support duck typing for the subclass argument to issubclass() when this delegates to ABCMeta.__subclasscheck__(). The Python implementation for this has no problems though.

In the pure Python version it uses isinstance().

In the C implementation it uses PyType_Check() which doesn't give the same result.

The consequence of this is that transparent object proxies used as decorators on classes (eg., as wrapt uses) will break when the C implementation us used with an error of:

    #       def __subclasscheck__(cls, subclass):
    #           """Override for issubclass(subclass, cls)."""
    #   >       return _abc_subclasscheck(cls, subclass)
    #   E       TypeError: issubclass() arg 1 must be a class

Example of tests from wrapt and how tests using C implementation must be disabled can be found at:

If instead of using PyType_Check() the C implementation used PyObject_IsInstance() at that point it is possible that wrapt may then work if the remainder of the C implementation is true to how the pure Python version works (not been able to test if that is the case or not as yet).

@grahamd grahamd mannequin added 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 6, 2021
@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.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants