
It's been two years since this proposal has been postponed - I would like to revive it. A quick recap (quotes edited for brevity):
On 23 January 2018 at 03:33, George Leslie-Waksman <waksman@gmail.com mailto:waksman@gmail.com> wrote:
The proposed implementation of dataclasses prevents defining fields with defaults before fields without defaults. This can create limitations on logical grouping of fields and on inheritance.
Take, for example, the case:
@dataclass class Foo: some_default: dict = field(default_factory=dict)
@dataclass class Bar(Foo): other_field: int
this results in the error: TypeError: non-default argument 'other_field' follows default argument
I understand that this is a limitation of positional arguments because the effective __init__ signature is:
def __init__(self, some_default: dict = <something>, other_field: int):
However, keyword only arguments allow an entirely reasonable solution to this problem:
def __init__(self, *, some_default: dict = <something>, other_field: int):
And have the added benefit of making the fields in the
__init__
call entirely explicit.
So, I propose the addition of a keyword_only flag to the @dataclass decorator that renders the __init__ method using keyword only arguments:
@dataclass(keyword_only=True) class Bar(Foo): other_field: int
--George Leslie-Waksman
Guido van Rossum wrote:
We can reconsider for 3.8. --Guido van Rossum (python.org/~guido)
What do you think about implementing this now? What should be the next step? Would it require a PEP?