attrs: subclass with mandatory attribute cannot be created when the base class has a factory based one
failing example
import attr
def test_example():
@attr.s
class Base(object):
attr = attr.ib(default=False)
@attr.s
class Sub(Base):
needed = attr.ib()
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 24 (8 by maintainers)
Commits related to this issue
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 — committed to malinoff/attrs by malinoff 7 years ago
- Added support for keyword-only attributes. Closes #106, and closes #38 (Rebases #281) Co-authored-by: Alex Ford <fordas@uw.edu> — committed to uw-ipd/attrs by malinoff 7 years ago
- Added support for keyword-only arguments on Python 3+ [rebase] (#411) * Added support for keyword-only attributes. Closes #106, and closes #38 (Rebases #281) Co-authored-by: Alex Ford <fordas@u... — committed to python-attrs/attrs by asford 6 years ago
For anyone stumbling on this issue and still unclear on how to implement a keyword-only attribute, here’s the syntax:
Can anyone tell me what is the recommended way now?
I accidentally opened up a duplicate of this.
My personal opinion™ is that we should just allow users to mark attributes as
init='kwonly'
. That will allow users to fix the problem themselves on Python 3 and consider upgrading on Python 2. 😽Full example:
Also, in the case of the error we have now, a helpful error message can direct users to use
kwonly
.Either make the subclass kw_only=True or overwrite the attribute with the default in the subclass:
I think this should be working code:
It isn’t though, currently, because:
_internal
(which iskw_only = False
) can not followoptional
(which iskw_only = True
), even though_internal
isinit = False
and wouldn’t appear in an__init__()
arg list anyway. (Is this a separate bug?)required
(which iskw_only = False
) can not followoptional
(which iskw_only = True
). While I understand the design here, I think it is an imposition on developers. At the very least it would be nice to be able to opt-in to being able to this with something like:(Obviously with a better option name than that. 😉)
or like this
i see what you mean, in python3 its
*,
and valid, for python2 there is need for a workaround in form of a sentinel object and exploding thenthey can always be given as keyword
the order should stay the same
it should be possible to give them as keyword instead of a positional argument
once inheritance is involved it seems sensible to go towards keywords in any case
basically all mandatory arguments after a optional one can be primary keyword based