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: dataclasses: allow keyword-only arguments
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Add kwarg-only option to dataclass
View: 33129
Assigned To: eric.smith Nosy List: ChrisBarker, eric.smith
Priority: normal Keywords:

Created on 2018-05-14 10:32 by eric.smith, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg316498 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-05-14 10:32
I've had several requests for keyword-only arguments. This is a placeholder to remind me to work on it. I have not decided if it's a good idea or not.

I propose adding a keyword_only argument to field(), defaulting to False.

I'm thinking that the basic idea would be to put all keyword-only fields at the end of the arguments to __init__, but for all other uses, leave them where they appear in the class definition. That way comparison operations, in particular, would use the fields as they appear in the class definition (which is the current behavior). Since they'd be at the end of __init__, and since order doesn't matter (they're keyword-only, after all), then this would work as expected for base classes.

That is, given:

@dataclass
class B:
    a: field(type=int, keyword_only=True)
    b: int

@dataclass
class C(B):
    c: int
    d: field(type=int, keyword_only=True)

Then B's __init__ would take (b, c, *, a, d) as its arguments, but its comparison functions would compare the tuples as (a, b, c, d).

It would be an error for a ClassVar field to be keyword-only. I think it would be okay if an InitVar field were keyword-only, but I haven't given it a lot of thought.
msg316786 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-05-16 08:27
Duplicate of #33129.
msg365518 - (view) Author: Chris Barker (ChrisBarker) * Date: 2020-04-01 22:04
This does not actually appear to be a Duplicate of #33129.

That one was asking to add **kwargs (I think) to the __init__. And was discussed and I think rejected on gitHub:

https://github.com/python/cpython/pull/19206

But this calls for having keyword-only parameters, which I think would still be a good idea.
History
Date User Action Args
2022-04-11 14:59:00adminsetgithub: 77674
2020-04-01 22:04:39ChrisBarkersetnosy: + ChrisBarker
messages: + msg365518
2018-05-16 08:27:07eric.smithsetstatus: open -> closed
superseder: Add kwarg-only option to dataclass
messages: + msg316786

resolution: duplicate
stage: resolved
2018-05-14 10:32:30eric.smithcreate