What is the difference between a Pydantic default and Optional field?

WHAT IT TESTS: Separation of type constraints from requiredness. ANSWER OUTLINE: Both forms are non-required; str = 'guest' rejects None, Optional[str] = None accepts it.
WHAT IT TESTS: Whether you separate Pydantic type constraints from requiredness. ANSWER OUTLINE: Both forms make the field non-required because both provide a default. str = 'guest' validates that input is a string and rejects None, falling back to 'guest' when omitted. Optional[str] = None accepts None or a string, falling back to None when omitted. RED FLAG: Claiming Optional[str] alone makes a field optional without a default, or missing that None fails on the plain str field.
Read the original → pydantic.dev
- #python
- #pydantic
- #fastapi
- #validation
- #type-hints
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.