On 3/17/2021 8:07 AM, Laurie O wrote:
A suggestion for ease of extension in the future: replace `field` parameter
`kw_only` with `init_type`.
As `field` is expected to be used less than `_: dataclasses.KW_ONLY` and
`dataclass(kw_only=True)`, it can afford to be more verbose. On top of that, if
you want to add positional-only fields in the future, with your current
proposal for keyword-only fields would mean you'd have to add another parameter
to `field` (eg `pos_only`).
My suggestion is to instead make the parameter to `field` become `init_type`,
which accepts an `enum` member (called `FieldInitParamType`, with members
`normal` (or `positional_or_keyword`) and `keyword_only` (and in the future,
`positional_only`)). This makes the parameter specifically about the type of
the parameter for the data-class's `__init__`, and allows for any number of
parameter types in the future without having to add new parameters to `field`.
I think kw_only=True is clearer as to the intent when reading the code.
And it doesn't preclude adding pos_only=True. It's not hard to add
parameters to field(). I also think it's extremely unlikely we'll ever
add positional arguments, or any other kind of argument (not sure what
that would be, but anything's possible in the future!). I sort of like
having "init" in the parameter name, but then again there are no other
member functions added by dataclasses that deal with fields as
parameters, so there's really nothing else that kw_only could refer to.
I guess to be more explicit it would be "init_arg_type".
And there's some value in doing what attrs has done, although I'll grant
that I've veered from what attrs has done before (and even did so in
this very proposal with dataclasses.KW_ONLY). But they have a lot of
experience with it, and I've looked for and not seen any complaints or
confusion.
As to using actual Enums, it's been avoided because of the expense of
importing the enum module. Although I'll admit I haven't seen timings of
that recently, so maybe that's not a valid argument any more.
Something to think about: how would positional arguments work with
pattern matching?
Eric
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/WZH2D6GVTT5UNT62GOZKGBPNWVBHOC5C/
Code of Conduct: http://python.org/psf/codeofconduct/