`isinstance` removes `cls.__dict__["__annotations__"]` in a complex setup with Protocol and ABCMeta
Open
Nobody has claimed this yet.
3.13
stdlib
topic-typing
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
import abc
from collections.abc import Iterator
from typing import Protocol
# ---- Expected behavior:
class Steppable(metaclass=abc.ABCMeta):
pass
class Module:
@classmethod
def __init_subclass__(cls, **kwargs) -> None:
super().__init_subclass__(**kwargs)
name, annotation, default = "name", str | None, "abc"
cls.__annotations__[name] = annotation
setattr(cls, name, default)
class SequenceLayer1(Module, Steppable):
pass
print(SequenceLayer1.__dict__.get("__annotations__"))
# {'name': str | None}
assert SequenceLayer1.__dict__.get("__annotations__") is not None
# ---- Unexpected behavior:
class CheckpointableIterator(Iterator, Protocol):
pass
isinstance(CheckpointableIterator, Iterator)
class SequenceLayer2(Module, Steppable):
pass
print(SequenceLayer2.__dict__.get("__annotations__"))
# None
assert SequenceLayer2.__dict__.get("__annotations__") is not None
- Failing on 3.12 and 3.13
- Passing on 3.11 and 3.14 (For 3.14 it is expected to have
__dict__["__annotations__"]missing)
CPython versions tested on:
3.12
Operating systems tested on:
Linux
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 reproducer involving Module.init_subclass, CheckpointableIterator, Protocol, ABCMeta, and isinstance on Python 3.12 and 3.13, then compare it with 3.11 and 3.14. Trace how SequenceLayer1 and SequenceLayer2 receive annotations and identify where the second class loses its entry. Done means the behavior is explained and a regression test covers the affected version range.
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
- Needs clarification
- Newbie friendliness
- 35/100