Skip to content

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

@vfdev-5

Description

@vfdev-5

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions