3.14 changes annotations behaviour for dataclasses.make_dataclass
Chưa có ai nhận issue này.
- 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:
I have many dataclasses created on demand by dataclasses.make_dataclass. 3.14 changes behaviour for instances of the dataclass.
In 3.13 and before, the instances have a __annotations__ attribute with the expected contents.
In 3.14, that attribute no longer exists, but calling instance.__annotate_func__ does return what was in __annotations__ before.
The 3.14 howto does say "you may access the annotations data member manually"
import dataclasses
import inspect
dc = dataclasses.make_dataclass("foo", (("one", int), ("two", str), ("three", complex)))
value = dc(1, 'hello', 3+4j)
print(f"{dir(value)=}")
try:
print(inspect.get_annotations(value))
except Exception as exc:
print("inspect.get_annotations exception: ", type(exc), exc)
if hasattr(dc, "__annotate_func__"):
print(f"{value.__annotate_func__()=}")
For Python 3.13:
dir(value)=['__annotations__', '__class__', '__dataclass_fields__', '__dataclass_params__ ....
For Python 3.14:
dir(value)=['__annotate_func__', '__annotations_cache__', '__class__', '__dataclass_fields__' ....
In both cases inspect.get_annotations() does give TypeError.
The release notes cover annotations, but nothing about changes to dataclasses instances.
Perhaps the annotations attributes shouldn't be on instances, but they have been. I also see __annotate_func__ on instances of regular objects from Python classes.
CPython versions tested on:
3.14, 3.13
Operating systems tested on:
Linux
Linked PRs
- gh-134387
- gh-134509
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với hành vi của dataclasses.make_dataclass và inspect.get_annotations được mô tả ở đây, sau đó xem xét các PR được liên kết gh-134387 và gh-134509. Công việc được coi là hoàn tất khi hành vi trong 3.14 được giải quyết hoặc được ghi chép rõ ràng, nhất quán với phần so sánh với 3.13 và annotations how-to.
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ó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- 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
- 25/100