Better support for descriptors at dataclass creation
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Feature or enhancement
Proposal:
Hello, I propose a change to the way dataclass works with fields defined using a descriptor class, particularly the way a default value is assigned.
What is the issue
According to the docs:
To determine whether a field contains a default value, @dataclass will call the descriptor’s get() method using its class access form: descriptor.get(obj=None, type=cls). If the descriptor returns a value in this case, it will be used as the field’s default. On the other hand, if the descriptor raises AttributeError in this situation, no default value will be provided for the field.
This behaviour is an issue for descriptors that need to be referenced at class creation: a common pattern in that case is to define the __get__ method with a test so that the descriptor returns itself outside of a call on an instance of the class:
class Descriptor:
...
def __get__(self, instance, instance_type=None):
if instance is None:
return self
...
...
In that case, when the dataclass decorator checks for a default value then self is returned, which means that:
- The field has a default value, when it probably shouldn't,
- The default value is a descriptor instance, which is almost definitely wrong.
Please note that the property builtin is such a descriptor, this is not an edge case. Here is an example using it:
from dataclasses import dataclass
from operator import attrgetter
@dataclass
class A:
a: int = property(attrgetter('_a'))
@a.setter
def a(self, val):
self._a = val
Then:
>>> A(1) # expected behaviour
A(a=1)
>>> A() # unexpected behaviour, but no exception is raised
A(a=<property object at 0x0000027C27023BA0>)
What could be changed ?
I propose adding a check to verify if __get__ returns the descriptor instance itself, to prevent it from using that instance as a default value. However, I am not well versed in the dataclass creation mecanisms, so I am not aware of any consequence it could have.
Is this a breaking change ?
Technically yes. I can't think of any applications where this behaviour could be used, and I don't know how to check if any project on github uses it, but it doesn't change the fact that it breaks backward compatibility.
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
Linked PRs
- gh-144940
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 issue 中描述的 dataclass 建立和 get 行為開始。查看連結的 PR gh-144940,並驗證類別存取結果如何被當作預設值處理;完成標準是 descriptor 執行個體不會在不破壞預期 descriptor 行為的情況下,被錯誤地用作欄位預設值。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 功能
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 15/100