Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-36077: Update handling of defaulted fields #17322

Closed
wants to merge 4 commits into from

Conversation

EpicWink
Copy link

@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 jdlourenco commented May 6, 2020

Looking forward to this!

@EpicWink
Copy link
Author

@EpicWink 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

@ericvsmith ericvsmith commented May 7, 2020

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

@EpicWink
Copy link
Author

@EpicWink 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

@ericvsmith ericvsmith commented May 7, 2020

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

@EpicWink
Copy link
Author

@EpicWink EpicWink commented Sep 21, 2020

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

@NeilGirdhar
Copy link

@NeilGirdhar NeilGirdhar commented Oct 3, 2020

This is so useful! Thanks for the patch.

@randallpittman
Copy link

@randallpittman randallpittman commented Oct 28, 2020

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

@EpicWink
Copy link
Author

@EpicWink EpicWink commented Oct 28, 2020

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

@ericvsmith ericvsmith commented Mar 16, 2021

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
Author

@EpicWink EpicWink commented Apr 27, 2021

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.

None yet

7 participants