Initialization order of fields overridden in subclasses
还没有人认领这个 Issue。
评估
调研方向
Start by reproducing the provided attrs example and reading the subclassing discussion in related issue #637, alongside the referenced dataclasses inheritance behavior in PEP 557. The issue does not name implementation files or tests; completion would require maintainer agreement on the intended initialization semantics and a verified resolution for the failing BetterXContainer case.
由索引模型根据 Issue 内容生成。
描述
I have a code similar to the following, that relies very heavily on mypy for correctness:
import attr
class X:
""" Some other class from the code """
class XButBetter(X):
""" Some other class from the code """
@attr.s(auto_attribs=True)
class XContainer:
the_x: X
x_metadata: int = attr.ib(init=False)
@x_metadata.default
def _x_metadata_default(self) -> int:
return self.the_x.get_metadata()
def some_useful_function(self) -> str:
""" A useful but very complex function dealing with self.the_x """
@attr.s(auto_attribs=True)
class BetterXContainer(XContainer):
the_x: XButBetter
def some_function(self) -> None:
""" some code assuming the_x is the better version """
xc = XContainer(X())
xcb = BetterXContainer(XButBetter())
The last line fails with AttributeError: 'BetterXContainer' object has no attribute 'the_x', because the initialization order of the attributes changed in the subclass. Another similar issue is described on stackoverflow.
Any plans on implementing subclassing similarly to dataclasses? Would that be a breaking API change? (Probably.)
Related attrs issue I found: #637
PS.: I realize this is subclassing issue n+1 when subclassing is frowned upon, and this is somewhat similar to the classic OOP problem of subclassing Circle from Ellipse, so feel free to just say "damn man, this is not proper OOP, so wontfix".
- 主要语言
- Python
- 星标
- 5.8k
- 派生
- 480
- 平均合并
- 2 小时 15 分钟
- 30 天内合并 PR
- 2
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-attrs/attrs 的其他 Issue
-
难度 5/5 一周以上 新手友好度 20/100
python-attrs/attrs#1620 ·
-
难度 3/5 1-2 天 新手友好度 55/100
python-attrs/attrs#1596 · 2 条评论 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 35/100
python-attrs/attrs#1549 · 3 条评论 ·
-
难度 4/5 3-5 天 新手友好度 38/100
python-attrs/attrs#1543 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
python-attrs/attrs#1532 ·
查看 python-attrs/attrs 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
stephrobert/dsoxlab#238 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 75/100
sublimehq/package_control#1780 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 70/100
nwg-piotr/nwg-displays#145 ·