Skip to content

Class is not subclass of itself: issubclass(First, First) is False #119699

Open
@treykeown

Description

@treykeown

Bug report

Bug description:

This was a nasty bug to track down. I really don't have a clue why this would happen. It seems to be triggered under these conditions:

  • A root class (Root) inherits from ABC
  • There's diamond-shaped inheritance for a sub-subclass (Multi)
  • In Root.__init_subclass__, when Multi is initializing, a check is performed to see if First is a subclass of Second
  • First is no longer a subclass of itself
from abc import ABC

class Root(ABC):
    def __init_subclass__(cls, **kwargs):
        if cls.__name__ == "Multi":
            # issubclass(Second, First)  # Wouldn't trigger the bug for Second
            issubclass(First, Second)  # Triggers the bug for First
            issubclass(Second, First)  # Doesn't trigger the bug for Second

class First(Root): ...
class Second(Root): ...
class Multi(First, Second): ...

assert not issubclass(First, First)  # This is the bug.
assert issubclass(Second, Second)

print("bug!")

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions