pydantic icon indicating copy to clipboard operation
pydantic copied to clipboard

Incorrect valudation of None in TypedDict constructor

Open Tishka17 opened this issue 2 years ago • 3 comments

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

Tishka17 avatar Jun 17 '22 08:06 Tishka17

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

samuelcolvin avatar Jun 17 '22 08:06 samuelcolvin

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

PrettyWood avatar Jun 17 '22 09:06 PrettyWood

Exactly, thanks for the clear explanation.

samuelcolvin avatar Jun 17 '22 09:06 samuelcolvin

This is already solved on V2. 🙏

We have no plans to fix this on V1.

Kludex avatar Apr 30 '23 01:04 Kludex