@@ -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,23 @@ 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
- {'a': core_schema.typed_dict_field(core_schema.str_schema())}
2862
+ {'a': core_schema.typed_dict_field(core_schema.str_schema())}, cls=MyTypedDict
2856
2863
)
2857
2864
v = SchemaValidator(wrapper_schema)
2858
2865
assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}
2859
2866
```
2860
2867
2861
2868
Args:
2862
2869
fields: The fields to use for the typed dict
2870
+ cls: The class to use for the typed dict
2863
2871
computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
2864
2872
strict: Whether the typed dict is strict
2865
2873
extras_schema: The extra validator to use for the typed dict
@@ -2873,6 +2881,7 @@ def typed_dict_schema(
2873
2881
return _dict_not_none (
2874
2882
type = 'typed-dict' ,
2875
2883
fields = fields ,
2884
+ cls = cls ,
2876
2885
computed_fields = computed_fields ,
2877
2886
strict = strict ,
2878
2887
extras_schema = extras_schema ,
0 commit comments