This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add keyword-only fields to dataclasses
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Epic_Wink, Henry Schreiner, cdce8p, eric.smith, finite-state-machine, jack1142, kamilturek, miss-islington, rhettinger, ryanhiebert, xtreak
Priority: normal Keywords: patch

Created on 2021-03-17 17:39 by eric.smith, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24909 closed eric.smith, 2021-03-17 22:30
PR 25608 merged eric.smith, 2021-04-25 21:43
PR 31235 merged Henry Schreiner, 2022-02-09 20:00
PR 31236 merged miss-islington, 2022-02-09 20:56
Messages (11)
msg388949 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-03-17 17:39
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:

@dataclasses.dataclass
class A:
    a: Any = field(kw_only=True) 

Will have __init__(self, *, a)

@dataclasses.dataclass(kw_only=True)
class B:
    a: Any
    b: Any 

Will have __init__(self, *, a, b)

@dataclasses.dataclass
class C:
    a: Any
    _: dataclasses.KW_ONLY
    b: Any
    c: 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.dataclass
class D:
    a: Any
    b: Any = field(kw_only=True)
    c: Any

Will have __init__(self, a, c, *, b)

PR to follow.
msg388951 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-03-17 18:10
This looks like a duplicate of https://bugs.python.org/issue33129 or that ticket can be closed.
msg388984 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-03-18 02:40
+1 for this idea.  I don't see any downside.
msg391920 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-04-26 13:09
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.
msg392018 - (view) Author: Laurie Opperman (Epic_Wink) * Date: 2021-04-27 03:28
Will this close https://bugs.python.org/issue36077 ?
msg392115 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-04-27 18:21
> Will this close https://bugs.python.org/issue36077 ?

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:

>>> @dataclass(kw_only=True)
... class Child(Parent):
...   y: int
...
>>> Child(0, y=1)
Child(x=0, y=1)
msg398856 - (view) Author: Jakub Kuczys (jack1142) * Date: 2021-08-04 01:00
Does this change deserve an entry in the What's New in Python document in addition to the changelog entry?
msg404609 - (view) Author: Finite State Machine (finite-state-machine) Date: 2021-10-21 15:40
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.
msg412943 - (view) Author: miss-islington (miss-islington) Date: 2022-02-09 20:56
New changeset 5a3f97291eea96037cceee097ebc00bba44bc9ed by Henry Schreiner in branch 'main':
bpo-43532: add version added to KW_ONLY (GH-31235)
https://github.com/python/cpython/commit/5a3f97291eea96037cceee097ebc00bba44bc9ed
msg412944 - (view) Author: miss-islington (miss-islington) Date: 2022-02-09 21:19
New changeset 7445949a4399ab19fbdd5a0b0aca53a690c3251b by Miss Islington (bot) in branch '3.10':
bpo-43532: add version added to KW_ONLY (GH-31235)
https://github.com/python/cpython/commit/7445949a4399ab19fbdd5a0b0aca53a690c3251b
msg412946 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-02-09 21:49
Thanks, Henry Schreiner!
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87698
2022-02-09 21:49:39eric.smithsetstatus: open -> closed

messages: + msg412946
stage: patch review -> resolved
2022-02-09 21:19:01miss-islingtonsetmessages: + msg412944
2022-02-09 20:56:22miss-islingtonsetpull_requests: + pull_request29406
2022-02-09 20:56:17miss-islingtonsetnosy: + miss-islington
messages: + msg412943
2022-02-09 20:00:00Henry Schreinersetnosy: + Henry Schreiner

pull_requests: + pull_request29405
stage: patch review
2021-10-21 15:40:42finite-state-machinesetnosy: + finite-state-machine
messages: + msg404609
2021-08-08 10:37:02cdce8psetnosy: + cdce8p
2021-08-04 01:00:06jack1142setmessages: + msg398856
2021-04-27 18:21:29eric.smithsetmessages: + msg392115
2021-04-27 03:28:37Epic_Winksetnosy: + Epic_Wink
messages: + msg392018
2021-04-26 13:09:38eric.smithsetresolution: fixed
messages: + msg391920
stage: patch review -> (no value)
2021-04-25 21:43:54eric.smithsetpull_requests: + pull_request24320
2021-03-18 02:40:29rhettingersetnosy: + rhettinger
messages: + msg388984
2021-03-18 00:37:30jack1142setnosy: + jack1142
2021-03-17 22:30:04eric.smithsetkeywords: + patch
stage: patch review
pull_requests: + pull_request23671
2021-03-17 19:16:59ryanhiebertsetnosy: + ryanhiebert
2021-03-17 19:06:40kamiltureksetnosy: + kamilturek
2021-03-17 18:21:09eric.smithlinkissue33129 superseder
2021-03-17 18:10:34xtreaksetnosy: + xtreak
messages: + msg388951
2021-03-17 17:39:21eric.smithcreate