Skip to content

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

Open

Description

@grahamd
mannequin
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']

Activity

  1. grahamd commented on Aug 6, 2021

    grahamdmannequin
    MannequinAuthor

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

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Aug 6, 2021
  3. 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.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions