ABC subclasses may break `issubclass(T, T)` invariant
Open
Nobody has claimed this yet.
extension-modules
stdlib
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 36k
- PR merge metrics
- PR metrics pending
Description
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 fromABC - There's diamond-shaped inheritance for a sub-subclass (
Multi) - In
Root.__init_subclass__, whenMultiis initializing, a check is performed to see ifFirstis a subclass ofSecond Firstis 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the supplied Python reproducer on CPython 3.12 and trace ABC.init_subclass and issubclass during Multi initialization. Investigate why First no longer satisfies issubclass(First, First); done means the invariant assertions pass for the diamond hierarchy, with regression coverage for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100