python / python/cpython

`isinstance` removes `cls.__dict__["__annotations__"]` in a complex setup with Protocol and ABCMeta

Open
#141,681 7 comments 1 reaction 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.