pydantic
pydantic copied to clipboard
Incorrect valudation of None in TypedDict constructor
In case of using TypedDcit with non-optional field and total=False
pydantic allow None as a value though validation of other types is done correctly:
from typing_extensions import TypedDict
from pydantic import parse_obj_as
class PatchData(TypedDict, total=False):
title: str
price: Optional[float]
print(parse_obj_as(PatchData, {"title": None}))
Expected result: validation error. Bacause title
field should be only str
according to annotation
Actual result: {'title': None}
Pydantic 1.9.1 Python 3.8.10 Ubuntu 20.04
Ye, might have been a mistake to add partial typeddict support. Full (and very fast) typeddict support will be added in v2.
I'd rather concentrate on getting v2 out asap rather than fixing support in 1.x
The thing is you use a TypedDict
with total=False
and pydantic has no way (currently) to distinguish a nullable field and a not required field
Exactly, thanks for the clear explanation.
This is already solved on V2. 🙏
We have no plans to fix this on V1.