title: Can I provide defaults for a subclass of a dataclass?
body: <p>I want to create a class hierarchy, where all objects of class <code>Base</code> has a field <code>field1</code>, but different subclasses have different default values for <code>field1</code>. The classes are data holders, so <code>dataclasses</code> seems like the perfect library to use.</p>
  <p>Consider the following python 3.7+ code.</p>
  <pre><code>from dataclasses import dataclass, field
  from typing import Any, Iterable
  
  @dataclass
  class Base:
      field1: Iterable[Any]
  
  @dataclass
  class Sub(Base):
      field2: Any
      field1: Iterable[Any] = field(default_factory=list)
  </code></pre>
  <p>The code fails, giving me <code>TypeError: non-default argument 'field2' follows default argument</code>. This is a little surprising, since <code>field2</code> follows the non-default <code>field1</code> of the superclass, and the defalt argument <code>field1</code> of the class <code>Sub</code> actually comes after <code>field2</code>.</p>
  <p>According to the example in <a href="https://docs.python.org/3/library/dataclasses.html#inheritance" rel="nofollow noreferrer">the docs on dataclass inheritence</a> though, fields in subclasses overrides the fields in superclasses, but field order is preserved. So the error makes sense.</p>
  <p>Is there any suitable workaround here, or must I implement everything by hand?</p>