[New-bugs-announce] [issue32896] Error when subclassing a dataclass with a field that uses a defaultfactory

John Didion report at bugs.python.org
Wed Feb 21 11:21:46 EST 2018


New submission from John Didion <johnpaul at didion.net>:

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

----------
messages: 312496
nosy: John Didion
priority: normal
severity: normal
status: open
title: Error when subclassing a dataclass with a field that uses a defaultfactory
type: crash
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32896>
_______________________________________


More information about the New-bugs-announce mailing list