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: eric.smith
Priority: normal Keywords:

Created on 2018-05-14 10:32 by eric.smith, last changed 2018-05-16 08:27 by eric.smith. This issue is now closed.

Messages (2)
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.
History
Date User Action Args
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