Skip to content

Commit b127652

Browse files
Adding cls reference to TypedDictSchema (#1410)
1 parent de04f03 commit b127652

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

python/pydantic_core/core_schema.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,7 @@ def typed_dict_field(
28182818
class TypedDictSchema(TypedDict, total=False):
28192819
type: Required[Literal['typed-dict']]
28202820
fields: Required[Dict[str, TypedDictField]]
2821+
cls: Type[TypedDict]
28212822
computed_fields: List[ComputedField]
28222823
strict: bool
28232824
extras_schema: CoreSchema
@@ -2834,6 +2835,7 @@ class TypedDictSchema(TypedDict, total=False):
28342835
def typed_dict_schema(
28352836
fields: Dict[str, TypedDictField],
28362837
*,
2838+
cls: Type[TypedDict] | None = None,
28372839
computed_fields: list[ComputedField] | None = None,
28382840
strict: bool | None = None,
28392841
extras_schema: CoreSchema | None = None,
@@ -2849,17 +2851,23 @@ def typed_dict_schema(
28492851
Returns a schema that matches a typed dict, e.g.:
28502852
28512853
```py
2854+
from typing_extensions import TypedDict
2855+
28522856
from pydantic_core import SchemaValidator, core_schema
28532857
2858+
class MyTypedDict(TypedDict):
2859+
a: str
2860+
28542861
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
28562863
)
28572864
v = SchemaValidator(wrapper_schema)
28582865
assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}
28592866
```
28602867
28612868
Args:
28622869
fields: The fields to use for the typed dict
2870+
cls: The class to use for the typed dict
28632871
computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
28642872
strict: Whether the typed dict is strict
28652873
extras_schema: The extra validator to use for the typed dict
@@ -2873,6 +2881,7 @@ def typed_dict_schema(
28732881
return _dict_not_none(
28742882
type='typed-dict',
28752883
fields=fields,
2884+
cls=cls,
28762885
computed_fields=computed_fields,
28772886
strict=strict,
28782887
extras_schema=extras_schema,

0 commit comments

Comments
 (0)