Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
init=False attributes which depend on keyword-only attributes are impossible #450
The initialization of an
from attr import attrs, attrib @attrs class BrokenInitFirst: _to_init: str = attrib(init=False) kwarg: str = attrib(kw_only=True) @_to_init.default def _init_to_init(self) -> str: return self.kwarg + "foo" BrokenInitFirst(kwarg="meep")
AttributeError: 'BrokenInitFirst' object has no attribute 'kwarg'
from attr import attrs, attrib @attrs class BrokenKwArgFirst: kwarg: str = attrib(kw_only=True) _to_init: str = attrib(init=False) @_to_init.default def _init_to_init(self) -> str: return self.kwarg + "foo" BrokenKwArgFirst(kwarg="meep")
These examples can also be found in https://github.com/rgabbard/attrs-kwonly-init-bug
This is related to #448 . Assuming there is some sort of internal ordering of attributes which also controls their initialization order, I think the solution here is to sort
At a quick glance it looks like performing the sort mentioned above (first
I’m sorry you hit me in the middle of my vacation and I'm having a hard time to catch up with the more complex issues.
And yes I agree that:
So feel free to submit a PR if you manage to achieve that without breaking backward compatibility. Even in subclassing scenarios. I tend to think that it might be better to make the condition slightly more complicated instead of re-ordering them because that might have other unforeseen side effects.
I went through the initial and final PRs as well as the three related issues (#38 , #106 , #335 ). It looks not allowing
and subsequent discussion.
I think the way forward is:
@wsanchez : I am a little uncertain whether