# Make succeeding non-default init-parameters keyword-only in dataclasses instead of raising

**URL:** <https://discuss.python.org/t/make-succeeding-non-default-init-parameters-keyword-only-in-dataclasses-instead-of-raising/2952>\
**Category:** Ideas\
**Created:** [January 3, 2020, 6:49am UTC](https://discuss.python.org/t/make-succeeding-non-default-init-parameters-keyword-only-in-dataclasses-instead-of-raising/2952 "2020-01-03T06:49:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [January 3, 2020, 6:49am UTC](https://discuss.python.org/t/make-succeeding-non-default-init-parameters-keyword-only-in-dataclasses-instead-of-raising/2952/1 "2020-01-03T06:49:07Z")

</div>

Currently in “dataclasses”, if non-defaulted fields are defined after defaulted-fields (usually subclasses adding required fields) that are initialised via argument (ie `init=True`), a TypeError is raised.

We can support the idiom of subclassing dataclasses with required fields by forcing all arguments after defaulted arguments to be keyword-only in the ` __init__ ` definition.

This is almost completely backwards-compatible: the only code that will break is any code relying on a TypeError to be raised when non-defaulted fields are defined upon data-class construction, which is very unlikely as class declaration tends to not be try-catched. However, the PEP states explicitly that a TypeError will be raised, so this will need to be changed and noted:

> `TypeError` will be raised if a field without a default value follows a field with a default value. This is true either when this occurs in a single class, or as a result of class inheritance.

Related links:

- Python bug: [bpo-36077](https://bugs.python.org/issue36077) (includes my PR for the change implementation)
- Stack Overflow question: [Class inheritance in Python 3.7 dataclasses](https://stackoverflow.com/questions/51575931/class-inheritance-in-python-3-7-dataclasses)

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [May 7, 2020, 10:03am UTC](https://discuss.python.org/t/make-succeeding-non-default-init-parameters-keyword-only-in-dataclasses-instead-of-raising/2952/2 "2020-05-07T10:03:23Z")

</div>

I just learnt that PEPs don’t need to be updated to reflect changes (makes sense, they’re a proposal, not a spec). My comment in the original post doesn’t stand,

---

<div class="post-metadata">

**Author:** ![wyfo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/wyfo/32/3558_2.png) [@wyfo](https://discuss.python.org/u/wyfo)\
**Post date:** [February 20, 2021, 5:40pm UTC](https://discuss.python.org/t/make-succeeding-non-default-init-parameters-keyword-only-in-dataclasses-instead-of-raising/2952/3 "2021-02-20T17:40:06Z")

</div>

Is there any news on this proposal?  
I can’t wait to use it…

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [February 20, 2021, 11:48pm UTC](https://discuss.python.org/t/make-succeeding-non-default-init-parameters-keyword-only-in-dataclasses-instead-of-raising/2952/4 "2021-02-20T23:48:00Z")

</div>

Some people were interested, but there was no discussion on the python-ideas mailing list. It seems like none of the Python devs were particularly interested in looking in to this.

If you want the functionality in your own project, add this code: [Support non-defaulted after defaulted fields in dataclasses · GitHub](https://gist.github.com/EpicWink/195f43286e47c26eef7d5eb3263fee75)
