@@ -2818,6 +2818,7 @@ def typed_dict_field(
2818
2818
class TypedDictSchema (TypedDict , total = False ):
2819
2819
type : Required [Literal ['typed-dict' ]]
2820
2820
fields : Required [Dict [str , TypedDictField ]]
2821
+ cls : Type [TypedDict ]
2821
2822
computed_fields : List [ComputedField ]
2822
2823
strict : bool
2823
2824
extras_schema : CoreSchema
@@ -2834,6 +2835,7 @@ class TypedDictSchema(TypedDict, total=False):
2834
2835
def typed_dict_schema (
2835
2836
fields : Dict [str , TypedDictField ],
2836
2837
* ,
2838
+ cls : Type [TypedDict ] | None = None ,
2837
2839
computed_fields : list [ComputedField ] | None = None ,
2838
2840
strict : bool | None = None ,
2839
2841
extras_schema : CoreSchema | None = None ,
@@ -2849,17 +2851,24 @@ def typed_dict_schema(
2849
2851
Returns a schema that matches a typed dict, e.g.:
2850
2852
2851
2853
```py
2854
+ from typing_extensions import TypedDict
2855
+
2852
2856
from pydantic_core import SchemaValidator, core_schema
2853
2857
2858
+ class MyTypedDict(TypedDict):
2859
+ a: str
2860
+
2854
2861
wrapper_schema = core_schema.typed_dict_schema(
2855
2862
{'a': core_schema.typed_dict_field(core_schema.str_schema())}
2863
+ cls=MyTypedDict
2856
2864
)
2857
2865
v = SchemaValidator(wrapper_schema)
2858
2866
assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}
2859
2867
```
2860
2868
2861
2869
Args:
2862
2870
fields: The fields to use for the typed dict
2871
+ cls: The class to use for the typed dict
2863
2872
computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
2864
2873
strict: Whether the typed dict is strict
2865
2874
extras_schema: The extra validator to use for the typed dict
@@ -2873,6 +2882,7 @@ def typed_dict_schema(
2873
2882
return _dict_not_none (
2874
2883
type = 'typed-dict' ,
2875
2884
fields = fields ,
2885
+ cls = cls ,
2876
2886
computed_fields = computed_fields ,
2877
2887
strict = strict ,
2878
2888
extras_schema = extras_schema ,
0 commit comments