I have a simple class which I decorate with @dataclass
. This class has few properties, but all of them are optional.
@dataclass
class Sample:
field_one: int
field_two: int
...
So far so good. I get the fields parameterized in the __init__
function, however, they are mandatory.
I get the following error if I do not specify them when initializing Sample()
:
TypeError: __init__() missing 2 required positional arguments: 'field_one', 'field_two'
How can I make all the fields of Sample to be optional and default to None
, but without having to specify Optional[type]
on each property? I am looking for some kind of "apply-to-all-properties" solution.
This way, I could have the following:
sample = Sample()
print(sample.field_one) # Outputs None