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 positional-only.
I'm open to other suggestions.
EricOn 3/10/2021 10:22 AM, Guido van Rossum wrote:_______________________________________________ Python-ideas mailing list -- To unsubscribe send an email to Message archived at Code of Conduct:--Eric V. Smith_______________________________________________Python-ideas mailing list -- firstname.lastname@example.orgTo unsubscribe send an email to email@example.comMessage archived at https://firstname.lastname@example.org/message/XSAYT2MFOIBYHYINDHLPR7V2WHCWRYPE/Code of Conduct: http://python.org/psf/codeofconduct/