Challenge - 5 Problems
Model Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Output of inherited Pydantic model serialization
Given the following FastAPI Pydantic models using inheritance, what is the JSON output of
ChildModel().dict()?FastAPI
from pydantic import BaseModel class ParentModel(BaseModel): id: int = 1 name: str = "parent" class ChildModel(ParentModel): age: int = 10 result = ChildModel().dict()
Attempts:
2 left
💡 Hint
Remember that child models inherit all fields from parent models.
✗ Incorrect
The ChildModel inherits all fields from ParentModel and adds 'age'. Calling dict() returns all fields with their default values.
📝 Syntax
intermediate2:00remaining
Correct syntax for extending Pydantic model with extra field
Which option correctly defines a Pydantic model
ExtendedModel that inherits from BaseModel and adds a new field extra: str with default "extra"?FastAPI
from pydantic import BaseModel class ExtendedModel(BaseModel): extra: str = "extra"
Attempts:
2 left
💡 Hint
Check the correct syntax for type annotations in Python classes.
✗ Incorrect
Option D uses correct Python type annotation syntax. Others have syntax errors or incorrect assignments.
🔧 Debug
advanced2:00remaining
Why does this inherited Pydantic model raise ValidationError?
Given these models, why does creating
ChildModel(name=123) raise a ValidationError?FastAPI
from pydantic import BaseModel class ParentModel(BaseModel): name: str class ChildModel(ParentModel): age: int = 10 child = ChildModel(name=123)
Attempts:
2 left
💡 Hint
Check the type of the 'name' field and the value passed.
✗ Incorrect
Pydantic validates types strictly. Passing an int where a str is expected causes ValidationError.
❓ state_output
advanced2:00remaining
Value of attribute after model inheritance and override
What is the value of
child.name after running this code?FastAPI
from pydantic import BaseModel class ParentModel(BaseModel): name: str = "parent" class ChildModel(ParentModel): name: str = "child" child = ChildModel()
Attempts:
2 left
💡 Hint
Child class fields override parent fields with the same name.
✗ Incorrect
The ChildModel overrides the 'name' field default to 'child', so child.name is 'child'.
🧠 Conceptual
expert3:00remaining
Effect of multiple inheritance on Pydantic model fields
Given these models, what fields does
MultiChildModel have and what are their default values?FastAPI
from pydantic import BaseModel class ModelA(BaseModel): a: int = 1 class ModelB(BaseModel): b: int = 2 class MultiChildModel(ModelA, ModelB): c: int = 3 model = MultiChildModel()
Attempts:
2 left
💡 Hint
Pydantic supports multiple inheritance but only from BaseModel subclasses.
✗ Incorrect
MultiChildModel inherits fields from both ModelA and ModelB, plus its own field c.