dataclass overriding a property in the parent class is broken
未关闭
@sobolevn 已经在做这个了。
开始于 2024年7月4日。
stdlib
topic-dataclasses
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
Consider the following snippet
@dc.dataclass
class A:
a: int
@property
def b(self) -> int:
return 1
@dc.dataclass
class B(A):
b: int = dc.field(default_factory=lambda: 0)
Dataclass apparently doesn't create a class attribute, so class B ends up with property , b of class A, which seems surprising, especially given that there is explicit assignent of field b in class B(A).
So this results in:
>>> B.b
<property object at ...>
>>> B(a=2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 4, in __init__
AttributeError: property 'b' of 'B' object has no setter
If i replace definition of b like so b: int = dc.field(default=0), or with b: int = 0 then everything works as expected:
>>> B.b
0
>>> B(a=2)
B(a=2, b=0)
I think it would be nice if dataclass would always create a class attribute (especially if there is explicit assignment!)
so that overloads work as expected.
CPython versions tested on:
3.11, 3.12
Operating systems tested on:
Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
评估
这个 Issue 还没有评估数据。