Skip to content

Conversation

@EpicWink
Copy link
Contributor

@EpicWink EpicWink commented Nov 21, 2019

When a non-defaulted dataclass field follows a defaulted field, the behaviour has changed from raising a TypeError to making all arguments in __init__ following the defaulted field keyword-only.

I'm not sold on the prettiness of the versionchanged area in the docs change: please comment.

https://bugs.python.org/issue36077

When a non-defaulted dataclass field follows a defaulted field, the
behaviour has changed from raising a `TypeError` to making all arguments
in `__init__` following the defaulted field keyword-only.
@jdlourenco
Copy link

jdlourenco commented May 6, 2020

Looking forward to this!

@EpicWink
Copy link
Contributor Author

EpicWink commented May 7, 2020

This is waiting on PEP 557 to be changed to allow this in the first place. I've submitted a feature-request in Discourse but there's been no discussion.

@ericvsmith
Copy link
Member

We don't typically update PEPs when new features are added.

@EpicWink
Copy link
Contributor Author

EpicWink commented May 7, 2020

Other way around: this pull-request is in direct contradiction with the PEP (it says that non-defaulted fields following defaulted fields cause a TypeError)

@ericvsmith
Copy link
Member

It doesn't matter: PEPs are not meant to describe current behavior.

@EpicWink
Copy link
Contributor Author

For those wanting a patch: https://gist.github.com/EpicWink/195f43286e47c26eef7d5eb3263fee75

@NeilGirdhar
Copy link

This is so useful! Thanks for the patch.

@randallpittman
Copy link

Thank you for the patch. Any way to know if this will get into 3.10?

@EpicWink
Copy link
Contributor Author

There hasn't been much discussion in the mailing list entry I posted or the Discourse post, so it seems the core devs aren't interested.

This suggestion from 2018 suggests allowing it in a backwards-compatible way by introducing a keyword_only parameter, which enables the behaviour in this PR.

There is an existing solution in the mailing list: pass init=False to the dataclass decorator. I don't like this as then you need to write a custom __init__ and __repr__ for subclasses.

@ericvsmith
Copy link
Member

I've posted a different proposal in python-ideas at https://mail.python.org/archives/list/python-ideas@python.org/message/FI6KS4O67XDEIDYOFWCXMDLDOSCNSEYG/

I'd be interested in any opinions on it. Please comment on python-ideas for a wider audience.

Thanks!

@EpicWink
Copy link
Contributor Author

This has been superseded by the more explicit solution to be included in Python 3.10 #24909

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants