classification
Title: Error when subclassing a dataclass with a field that uses a defaultfactory
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: John Didion, eric.smith, ned.deily
Priority: release blocker Keywords: patch

Created on 2018-02-21 16:21 by John Didion, last changed 2018-03-21 09:23 by eric.smith. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6170 merged eric.smith, 2018-03-21 00:52
PR 6171 merged miss-islington, 2018-03-21 02:01
Messages (5)
msg312496 - (view) Author: John Didion (John Didion) Date: 2018-02-21 16:21
> @dataclass
> class Foo:
>  x: dict = field(default_factory=dict)

> @dataclass
> class Bar(Foo):
>   y: int = 1

> @dataclass
> class Baz(Foo):
>   def blorf(self):
>     print('hello')

> Foo().x
{}

> Bar().x
{}

> Baz().x

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'x'

---

I understand that this is desired behavior when the subclass contains non-default attributes. But subclasses that define no additional attributes should work just the same as those that define only additional default attributes.

A similar issue was raised and dismissed when dataclasses was in development on GitHub: https://github.com/ericvsmith/dataclasses/issues/112, but that only concerned the case of subclasses defining non-default attributes.
msg312517 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-02-22 00:16
That's a great bug report. Thanks for the tiny code to replicate it.

It turns out the code isn't quite doing what I thought. I'll have to give some thought to exactly how I'm going to handle this without breaking other cases, but I should have it fixed soon.

Thanks again.
msg314182 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-03-21 02:00
New changeset 8f6eccdc64cab735c47620fea948e64b19f83684 by Eric V. Smith in branch 'master':
bpo-32896: Fix error when subclassing a dataclass with a field that uses a default_factory (GH-6170)
https://github.com/python/cpython/commit/8f6eccdc64cab735c47620fea948e64b19f83684
msg314188 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-03-21 09:22
New changeset 22136c94b6e43c8c584a54f3a513b83b753b96ee by Eric V. Smith (Miss Islington (bot)) in branch '3.7':
bpo-32896: Fix error when subclassing a dataclass with a field that uses a default_factory (GH-6170) (GH-6171)
https://github.com/python/cpython/commit/22136c94b6e43c8c584a54f3a513b83b753b96ee
msg314189 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-03-21 09:23
Thanks again for the bug report. This ended up being a simple fix, but an important one.
History
Date User Action Args
2018-03-21 09:23:12eric.smithsetstatus: open -> closed
resolution: fixed
messages: + msg314189

stage: patch review -> resolved
2018-03-21 09:22:08eric.smithsetmessages: + msg314188
2018-03-21 02:01:50miss-islingtonsetpull_requests: + pull_request5926
2018-03-21 02:00:26eric.smithsetmessages: + msg314182
2018-03-21 00:52:04eric.smithsetkeywords: + patch
stage: patch review
pull_requests: + pull_request5925
2018-03-21 00:50:47eric.smithsetpriority: deferred blocker -> release blocker
versions: + Python 3.8
2018-02-26 14:03:04eric.smithsetpriority: release blocker -> deferred blocker
2018-02-22 00:17:05eric.smithsetpriority: normal -> release blocker
nosy: + ned.deily
2018-02-22 00:16:04eric.smithsetmessages: + msg312517
2018-02-21 18:43:45eric.smithsetassignee: eric.smith
type: crash -> behavior
components: + Library (Lib)
2018-02-21 16:36:54ned.deilysetnosy: + eric.smith
2018-02-21 16:21:46John Didioncreate