How do you make Optional attr's of a dataclass?
from dataclasses import dataclass
@dataclass
class CampingEquipment:
knife: bool
fork: bool
missing_flask_size: # what to write here?
kennys_stuff = {
'knife': True,
'fork': True
}
print(CampingEquipment(**kennys_stuff))
I tried field(init=False), but it gave me:
TypeError: CampingEquipment.__init__() missing 1 required positional argument: 'missing_flask_size'
By Optional I mean __dict__ may contain the key "missing_flask_size" or not. If I set a default value then the key will be there and it shouldn't be in some cases. I want to check its type if it is there.
I tried moving the field(init=False) to the type location (after the colon) so I could make it more explicit as to the thing I wanted optional would be the key and not the value.
So I want this test to pass:
with pytest.raises(AttributeError):
ce = CampingEquipment(**kennys_stuff)
print(ce.missing_flask_size)