title: Can I have an optional parameter in dataclasses that is omitted when transformed to dict?
body: <p>I wish to perform static type checking (<code>pylance</code> in <code>vscode</code>) on some dictionaries. The &quot;tricky&quot; part is the I want some of the parameters to be optional and not show up at all in the dictionary. I've tried using <code>dataclasses</code> and <code>TypedDict</code> but without luck so far.</p>
  <pre class="lang-py prettyprint-override"><code>from typing import Optional, List
  from dataclasses import dataclass, asdict
  
  
  @dataclass
  class SubOrder:
      name: str
  
  
  @dataclass
  class Order:
      name: str
      sub_orders: Optional[List[SubOrder]]
  
  
  assert asdict(Order(name=&quot;Pizza&quot;)) == {&quot;name&quot;: &quot;Pizza&quot;}
  assert asdict(Order(name=&quot;Pizza&quot;, sub_orders=[SubOrder(name=&quot;Pasta&quot;)])) == {
      &quot;name&quot;: &quot;Pizza&quot;,
      &quot;sub_orders&quot;: [{&quot;name&quot;: &quot;Pasta&quot;}],
  }
  </code></pre>
  <p>Is that achievable with <code>dataclasses</code>? I basically just want my static type checker (<code>pylance</code> / <code>pyright</code>) to check my dictionaries which is why I'm using <code>dataclasses</code>. I've tried with <code>TypedDict</code> as well but the type checkers does not seem to behave like I was. They always require me to set <code>sub_orders</code>.</p>
  <p>The following code passes but <code>pylance</code> is not happy about not having <code>sub_orders</code>.</p>
  <pre class="lang-py prettyprint-override"><code>from typing import Optional, List, TypedDict
  
  
  class SubOrder(TypedDict):
      name: str
  
  
  class Order(TypedDict):
      name: str
      sub_orders: Optional[List[SubOrder]]
  
  
  assert Order(name=&quot;Pizza&quot;) == {&quot;name&quot;: &quot;Pizza&quot;}
  assert Order(name=&quot;Pizza&quot;, sub_orders=[SubOrder(name=&quot;Pasta&quot;)]) == {
      &quot;name&quot;: &quot;Pizza&quot;,
      &quot;sub_orders&quot;: [{&quot;name&quot;: &quot;Pasta&quot;}],
  }
  </code></pre>
  <h3>EDIT</h3>
  <p>I've added a <a href="https://github.com/microsoft/pylance-release/issues/84" rel="noreferrer">bug report</a> in <code>pylance</code> since this might actually be a bug in <code>pylance</code> / <code>pyright</code></p>