I want to make the set parameter optional but still allow None to be a valid value. Based on the documentation, it suggested that dataclasses.MISSING could be used a default value to assist in this.
As shown above, the
MISSINGvalue is a sentinel object used to detect if some parameters are provided by the user. This sentinel is used becauseNoneis a valid value for some parameters with a distinct meaning. No code should directly use theMISSINGvalue.
But by using this as follows:
import dataclasses
from dataclasses import dataclass, field
@dataclass
class Var:
get: list
set: list = dataclasses.MISSING
def __post_init__(self):
if self.set is dataclasses.MISSING:
self.set = self.get
print(Var(get=['Title']))
I am getting an error:
Traceback (most recent call last):
File "main.py", line 31, in <module>
print(Var(get=['Title']))
TypeError: __init__() missing 1 required positional argument: 'set'