python / python/cpython

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

Đang mở
#141,681 7 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.13 stdlib topic-typing type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Chạy reproducer liên quan đến Module.init_subclass, CheckpointableIterator, Protocol, ABCMeta và isinstance trên Python 3.12 và 3.13, sau đó so sánh với 3.11 và 3.14. Theo dõi cách SequenceLayer1 và SequenceLayer2 nhận annotations và xác định vị trí lớp thứ hai mất mục nhập của nó. Hoàn tất khi hành vi được giải thích và một regression test bao phủ khoảng phiên bản bị ảnh hưởng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.