from dataclasses import dataclass
@dataclass
class CampingEquipment:
knife: bool
fork: bool
missing_flask_size: ? # removed field() per answers below.
kennys_stuff = {
'knife':True,
'fork': True
}
print(CampingEquipment(**kennys_stuff))
gives me:
TypeError: CampingEquipment.__init__() missing 1 required positional argument: 'missing_flask_size'
How do you make Optional attr's of a dataclass?
Edit
What I meant by optional wasn't clear. 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 it's 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. That was just confusing for everyone, including me, so it's removed.
So I want this test to pass:
with pytest.raises(AttributeError):
ce = CampingEquipment(**kennys_stuff)
print(ce.missing_flask_size)