dataclass: __init__ kwargs and Optional[type]
I've created a helper class in my own library that enhances the existing dataclass: a) __init__ accepts keyword-only arguments, b) Optional[...] attribute without a specified default value would default to None in __init__. I think this could be useful in stdlib. I'm thinking a dataclass decorator parameter like "init_kwonly" (default=False to provide backward compatibility) that if True would implement this behavior. Thoughts?
The main benefits of this proposal: - the order of fields (those with defaults, those without) is irrelevant - don't need to pedantically add default = None for Optional values On Sun, 2021-01-24 at 19:46 +0000, Paul Bryan via Python-ideas wrote:
I've created a helper class in my own library that enhances the existing dataclass:
a) __init__ accepts keyword-only arguments, b) Optional[...] attribute without a specified default value would default to None in __init__.
I think this could be useful in stdlib. I'm thinking a dataclass decorator parameter like "init_kwonly" (default=False to provide backward compatibility) that if True would implement this behavior.
Thoughts?
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/2NLPDO... Code of Conduct: http://python.org/psf/codeofconduct/
See https://bugs.python.org/issue33129. I've not done much with this issue, because I'm not crazy about the API, and I'd like to do something with positionally-only arguments at the same time. But I think in general it's a good idea. Eric On 1/24/2021 3:07 PM, Paul Bryan via Python-ideas wrote:
The main benefits of this proposal:
- the order of fields (those with defaults, those without) is irrelevant - don't need to pedantically add default = None for Optional values
On Sun, 2021-01-24 at 19:46 +0000, Paul Bryan via Python-ideas wrote:
I've created a helper class in my own library that enhances the existing dataclass:
a) __init__ accepts keyword-only arguments, b) Optional[...] attribute without a specified default value would default to None in __init__.
I think this could be useful in stdlib. I'm thinking a dataclass decorator parameter like "init_kwonly" (default=False to provide backward compatibility) that if True would implement this behavior.
Thoughts?
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org mailto:python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org mailto:python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.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/2NLPDO... https://mail.python.org/archives/list/python-ideas@python.org/message/2NLPDO... Code of Conduct: http://python.org/psf/codeofconduct/ http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/T2BBB4... Code of Conduct: http://python.org/psf/codeofconduct/
Added myself to the nosy list. Seems like the suggested kw_only: str variant—perhaps it should also support Iterable—would allow continued support for positional arguments. Is your idea around positional-only arguments different? Another thought I had was around Optional and default-None semantics: Optional[type] should safely default to None in __init__; I'm thinking the opposite could also be supported (default of None can imply Optional type in annotation). Given various objectives, would it make sense to codify in a PEP to build consensus? On Mon, 2021-01-25 at 11:07 -0500, Eric V. Smith wrote:
See https://bugs.python.org/issue33129. I've not done much with this issue, because I'm not crazy about the API, and I'd like to do something with positionally-only arguments at the same time. But I think in general it's a good idea. Eric On 1/24/2021 3:07 PM, Paul Bryan via Python-ideas wrote:
The main benefits of this proposal:
- the order of fields (those with defaults, those without) is irrelevant - don't need to pedantically add default = None for Optional values
On Sun, 2021-01-24 at 19:46 +0000, Paul Bryan via Python-ideas wrote:
I've created a helper class in my own library that enhances the existing dataclass:
a) __init__ accepts keyword-only arguments, b) Optional[...] attribute without a specified default value would default to None in __init__.
I think this could be useful in stdlib. I'm thinking a dataclass decorator parameter like "init_kwonly" (default=False to provide backward compatibility) that if True would implement this behavior.
Thoughts?
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/2NLPDO... Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/T2BBB4... Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/LP6LCI... Code of Conduct: http://python.org/psf/codeofconduct/
participants (2)
-
Eric V. Smith
-
Paul Bryan