A validator that reads type hints
Write validate(cls, data) that checks a dictionary against the type hints of a class and returns a list of problem strings (an empty list means the data is valid). Use typing.get_type_hints(cls) to get the fields (Topic 10.7).
Go through the fields in the order the class declares them. If a field is missing from data and the class gives it no default value, add the text "<name>: missing". If a value has the wrong type, add "<name>: expected <type>, got <type>" using the class names, for example "age: expected int, got str".
A field that has a default on the class may be left out of data. Only plain classes such as int, str and bool need to work.
What Check tests
- Valid data gives an empty list
- A wrong type is reported
- A missing field without a default is reported
- A field with a default may be left out, and a wrong default-field type is reported
- Problems come in field order
- An empty dictionary reports every field without a default, and extra keys are ignored
- It works for another class too
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Output
Press Run to see what your program prints.