Initialization order of fields overridden in subclasses
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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".
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-attrs/attrs
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
python-attrs/attrs#1620 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
python-attrs/attrs#1596 · 2 comments · 1 reaction ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-attrs/attrs#1549 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
python-attrs/attrs#1543 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-attrs/attrs#1532 ·
All issues in python-attrs/attrs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
OpenHands/extensions#626 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
CSCfi/sd-search-api#39 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
StevenBlack/hosts#3255 ·