From 848023b6f161bf4ffe3dc201c037db9dbaae769c Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Mon, 19 Aug 2024 13:11:34 -0400 Subject: [PATCH 1/3] adding cls to TypedDictSchema --- python/pydantic_core/core_schema.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/python/pydantic_core/core_schema.py b/python/pydantic_core/core_schema.py index 886194cbc..c26b90e39 100644 --- a/python/pydantic_core/core_schema.py +++ b/python/pydantic_core/core_schema.py @@ -2818,6 +2818,7 @@ def typed_dict_field( class TypedDictSchema(TypedDict, total=False): type: Required[Literal['typed-dict']] fields: Required[Dict[str, TypedDictField]] + cls: Type[TypedDict] computed_fields: List[ComputedField] strict: bool extras_schema: CoreSchema @@ -2834,6 +2835,7 @@ class TypedDictSchema(TypedDict, total=False): def typed_dict_schema( fields: Dict[str, TypedDictField], *, + cls: Type[TypedDict] | None = None, computed_fields: list[ComputedField] | None = None, strict: bool | None = None, extras_schema: CoreSchema | None = None, @@ -2849,10 +2851,16 @@ def typed_dict_schema( Returns a schema that matches a typed dict, e.g.: ```py + from typing_extensions import TypedDict + from pydantic_core import SchemaValidator, core_schema + class MyTypedDict(TypedDict): + a: str + wrapper_schema = core_schema.typed_dict_schema( {'a': core_schema.typed_dict_field(core_schema.str_schema())} + cls=MyTypedDict ) v = SchemaValidator(wrapper_schema) assert v.validate_python({'a': 'hello'}) == {'a': 'hello'} @@ -2860,6 +2868,7 @@ def typed_dict_schema( Args: fields: The fields to use for the typed dict + cls: The class to use for the typed dict computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model strict: Whether the typed dict is strict extras_schema: The extra validator to use for the typed dict @@ -2873,6 +2882,7 @@ def typed_dict_schema( return _dict_not_none( type='typed-dict', fields=fields, + cls=cls, computed_fields=computed_fields, strict=strict, extras_schema=extras_schema, From 1f21c56a7f5a3a36dcd0aa1f147f84b49a844830 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Mon, 19 Aug 2024 13:27:53 -0400 Subject: [PATCH 2/3] fix docstring example --- python/pydantic_core/core_schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pydantic_core/core_schema.py b/python/pydantic_core/core_schema.py index c26b90e39..1e92c43ae 100644 --- a/python/pydantic_core/core_schema.py +++ b/python/pydantic_core/core_schema.py @@ -2859,7 +2859,7 @@ class MyTypedDict(TypedDict): a: str wrapper_schema = core_schema.typed_dict_schema( - {'a': core_schema.typed_dict_field(core_schema.str_schema())} + {'a': core_schema.typed_dict_field(core_schema.str_schema())}, cls=MyTypedDict ) v = SchemaValidator(wrapper_schema) From 45aad17717584a8696846a2cc495eb91c8fc3ea1 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Mon, 19 Aug 2024 13:38:51 -0400 Subject: [PATCH 3/3] fix docstring again --- python/pydantic_core/core_schema.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/pydantic_core/core_schema.py b/python/pydantic_core/core_schema.py index 1e92c43ae..2cb99ef33 100644 --- a/python/pydantic_core/core_schema.py +++ b/python/pydantic_core/core_schema.py @@ -2859,8 +2859,7 @@ class MyTypedDict(TypedDict): a: str wrapper_schema = core_schema.typed_dict_schema( - {'a': core_schema.typed_dict_field(core_schema.str_schema())}, - cls=MyTypedDict + {'a': core_schema.typed_dict_field(core_schema.str_schema())}, cls=MyTypedDict ) v = SchemaValidator(wrapper_schema) assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}