Skip to content

Commit 848023b

Browse files
committed
adding cls to TypedDictSchema
1 parent de04f03 commit 848023b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

python/pydantic_core/core_schema.py

Lines changed: 10 additions & 0 deletions
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,24 @@ 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(
28552862
{'a': core_schema.typed_dict_field(core_schema.str_schema())}
2863+
cls=MyTypedDict
28562864
)
28572865
v = SchemaValidator(wrapper_schema)
28582866
assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}
28592867
```
28602868
28612869
Args:
28622870
fields: The fields to use for the typed dict
2871+
cls: The class to use for the typed dict
28632872
computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
28642873
strict: Whether the typed dict is strict
28652874
extras_schema: The extra validator to use for the typed dict
@@ -2873,6 +2882,7 @@ def typed_dict_schema(
28732882
return _dict_not_none(
28742883
type='typed-dict',
28752884
fields=fields,
2885+
cls=cls,
28762886
computed_fields=computed_fields,
28772887
strict=strict,
28782888
extras_schema=extras_schema,

0 commit comments

Comments
 (0)