title: How to apply default value to Python dataclass field when None was passed?
body: <p>I need a class that will accept a number of parameters, I know that all parameters will be provided but some maybe passed as <code>None</code> in which case my <code>class</code> will have to provide default values.</p>
  <p>I want to setup a simple <code>dataclass</code> with a some default values like so:</p>
  <pre><code>@dataclass
  class Specs1:
      a: str
      b: str = 'Bravo'
      c: str = 'Charlie'
  </code></pre>
  <p>I would like to be able to get the default value for the second field but still set a value for the third one. I cannot do this with None because it is happily accepted as a value for my string:</p>
  <pre><code>r1 = Specs1('Apple', None, 'Cherry') # Specs1(a='Apple', b=None, c='Cherry')
  </code></pre>
  <p>I have come up with the following solution:</p>
  <pre><code>@dataclass
  class Specs2:
      def_b: ClassVar = 'Bravo'
      def_c: ClassVar = 'Charlie'
      a: str
      b: str = def_b
      c: str = def_c
      
      def __post_init__(self):
          self.b = self.def_b if self.b is None else self.b
          self.c = self.def_c if self.c is None else self.c
  </code></pre>
  <p>Which seems to behave as intended:</p>
  <pre><code>r2 = Specs2('Apple', None, 'Cherry') # Specs2(a='Apple', b='Bravo', c='Cherry')
  </code></pre>
  <p>However, I feel it is quite ugly and that I am maybe missing something here. My actual class will have more fields so it will only get uglier.</p>
  <p>The parameters passed to the class contain <code>None</code> and I do not have control over this aspect.</p>