As I've said before, I'm not opposed to the feature of keyword-only arguments. I think it would be a great addition.
However, the proposal from Laurie O is only for changing fields without default values following fields with default values to be keyword-only. At least that's how I understand it.
So, that means that:
x: int = 0
t: int = 0
Would generate a __init__ with this signature:
def __init__(self, x=0, *, y, z, t=0):
While it's an interesting application, I think that's just too limiting. Among other things, I can't define a dataclass where all fields are keyword-only, or a class where there's only a single field and it's keyword-only. I also have to have at least one keyword-only field (here, y) that has no default value. z and t can have defaults, but not y.
What I'd like to see is some way of having keyword-only arguments, with or without defaults. And I'd also like to see if we could get support for positional-only arguments at the same time.
I'm not sure of the best way to achieve this. Using flags to field() doesn't sound awesome, but could be made to work. Or maybe special field names or types? I'm not crazy about that, but using special types would let you do something like:
x: int = 0
t: int = 0
And the dataclasses machinery would ignore the "_" field except
for making everything after it keyword-only. Here the name "_"
isn't special: any field (of any name) that's of type
dataclasses.KEYWORD_ONLY would be ignored except for the
keyword-only changing behavior. In some ways, it's like
dataclasses.ClassVar, where the type is treated specially and the
field doesn't become a __init__ argument.
There are also issues with inheritance that would need to be
thought through. This idea could also be extended for
I'm open to other suggestions.
I've seen comments from Eric V Smith in this thread and in the PR. If he's in favor and it can be done in a backwards compatible way then he can guide you to acceptance and merging of your PR. But like all of us he has limited time.
On Wed, Mar 10, 2021 at 6:15 AM Laurie O <firstname.lastname@example.org> wrote:
I've had tens of people show interest in my proposal, asking what the blocker is in acceptance. As far as I know, no Python developer has shown any interest in it.
My proposal is to automatically make any non-default fields become keyword-only `__init__` parameters, with no need to have any arguments passed to the `dataclass` decorator.
My PR: https://github.com/python/cpython/pull/17322
My previous mailing-list message: https://email@example.com/message/2V2SCUFMK7PK3DVA7D77AVXWIXNTSQDK/
Python-ideas mailing list -- firstname.lastname@example.org
To unsubscribe send an email to email@example.com
Message archived at https://firstname.lastname@example.org/message/KPVB2G64BHFGGOTXZQXT3AU7SIO5ABJE/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://email@example.com/message/IN3D7UQQI4HSKSCUPMU3C3RTUG6NWUKG/ Code of Conduct: http://python.org/psf/codeofconduct/
-- Eric V. Smith