Dataclasses fields should evaluate their annotations
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先檢視所參照的 CPython 提交,以及讀取 annotationlib 的 ForwardRef 模式的 dataclasses 欄位建構。將該行為與 annotations 中的求值結果進行比較,接著確定相容的 field.type 語意,並根據文件中記錄的向後相容性疑慮驗證所提議的變更。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100