You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The @DataClass decorator will get a new parameter, kw_only, which defaults to False. If kw_only=True, all fields in the dataclass will be by efault keyword-only. In addition, field() will have a new kw_only parameter. If true, the field will be keyword-only. If false, it will not be keyword-only. If unspecified, it will use the value of dataclass's kw_only parameter.
In addition, a module-level variable KW_ONLY will be added. If a field has this type, then all fields after it will default to kw_only=True. The field is otherwise completely ignored.
@dataclasses.dataclass(kw_only=True)classB:
a: Anyb: Any
Will have __init__(self, *, a, b)
@dataclasses.dataclassclassC:
a: Any_: dataclasses.KW_ONLYb: Anyc: Any
Will have __init__(self, a, *, b, c)
If any non-keyword-only parameters are present, they will be moved before all keyword-only parameters, only for the generated __init__. All other generated methods (repr, __lt__, etc.) will keep fields in the declared order, which is the case in versions 3.9 and earlier.
@dataclasses.dataclassclassD:
a: Anyb: Any=field(kw_only=True)
c: Any
I've checked in the code, tests, and some basic documentation. I didn't finish the documentation in order to make sure I could get this in before beta 1. I'm going to leave this issue open until I have time for the rest of the documentation.
Activity
ericvsmith commentedon Mar 17, 2021
The idea is that a keyword-only field becomes a keyword-only argument to __init__().
For the proposal and a discussion, see https://mail.python.org/archives/list/python-ideas@python.org/message/FI6KS4O67XDEIDYOFWCXMDLDOSCNSEYG/
The @DataClass decorator will get a new parameter, kw_only, which defaults to False. If kw_only=True, all fields in the dataclass will be by efault keyword-only. In addition, field() will have a new kw_only parameter. If true, the field will be keyword-only. If false, it will not be keyword-only. If unspecified, it will use the value of dataclass's kw_only parameter.
In addition, a module-level variable KW_ONLY will be added. If a field has this type, then all fields after it will default to kw_only=True. The field is otherwise completely ignored.
Examples:
Will have __init__(self, *, a)
Will have __init__(self, *, a, b)
Will have __init__(self, a, *, b, c)
If any non-keyword-only parameters are present, they will be moved before all keyword-only parameters, only for the generated __init__. All other generated methods (repr, __lt__, etc.) will keep fields in the declared order, which is the case in versions 3.9 and earlier.
Will have __init__(self, a, c, *, b)
PR to follow.
tirkarthi commentedon Mar 17, 2021
This looks like a duplicate of https://bugs.python.org/issue33129 or that ticket can be closed.
rhettinger commentedon Mar 18, 2021
+1 for this idea. I don't see any downside.
ericvsmith commentedon Apr 26, 2021
I've checked in the code, tests, and some basic documentation. I didn't finish the documentation in order to make sure I could get this in before beta 1. I'm going to leave this issue open until I have time for the rest of the documentation.
EpicWink commentedon Apr 27, 2021
Will this close https://bugs.python.org/issue36077 ?
ericvsmith commentedon Apr 27, 2021
No, not directly, although I'm going to close that issue as "won't fix".
But you can specify that the child class requires all params as kw_only. Continuing the example:
jack1142 commentedon Aug 4, 2021
Does this change deserve an entry in the What's New in Python document in addition to the changelog entry?
finite-state-machine commentedon Oct 21, 2021
I doubt it's worth opening a separate issue for this, so I'll mention it here:
In the documentation (https://docs.python.org/3.10/library/dataclasses.html#dataclasses.KW_ONLY) nothing documents KW_ONLY as being new in Python 3.10.
miss-islington commentedon Feb 9, 2022
New changeset 5a3f972 by Henry Schreiner in branch 'main':
bpo-43532: add version added to KW_ONLY (GH-31235)
5a3f972
miss-islington commentedon Feb 9, 2022
New changeset 7445949 by Miss Islington (bot) in branch '3.10':
bpo-43532: add version added to KW_ONLY (GH-31235)
7445949
ericvsmith commentedon Feb 9, 2022
Thanks, Henry Schreiner!