Dataclasses fields should evaluate their annotations
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ả
Feature or enhancement
Proposal:
In Python 3.14 (a5), I was surprised that while
import dataclasses
@dataclasses.dataclass
class X:
a: X | None
print(X.__annotations__)
works as expected and prints,
{'a': __main__.X | None}
if one tries to look at the type of the field using the dataclasses.fields interface:
print(dataclasses.fields(X)[0].type)
then this instead results in a ForwardRef
ForwardRef('X | None')
Surely, someone trying to inspect the field would want the value version, consistent with the new default functionality of __annotations__ rather than trying to figure out how to evaluate the ForwardRef precisely.
The current behaviour was introduced in
where the dataclasses fields are constructed by reading the ForwardRef mode of annotationlib.
I believe this would be backwards in that the type value had to exists without from __future__ import annotations and would be a string with it.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
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 bằng việc xem xét commit CPython được tham chiếu và cách xây dựng field của dataclasses, vốn đọc chế độ ForwardRef của annotationlib. So sánh hành vi đó với các giá trị đã được đánh giá trong annotations, sau đó xác định ngữ nghĩa tương thích của field.type và xác thực thay đổi được đề xuất dựa trên các mối lo ngại về khả năng tương thích ngược đã được ghi lại.
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
- Tính năng
- Độ 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
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100