Skip to content

Commit

Permalink
Adding cls reference to TypedDictSchema (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle authored Aug 19, 2024
1 parent de04f03 commit b127652
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -2849,17 +2851,23 @@ 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())}
{'a': core_schema.typed_dict_field(core_schema.str_schema())}, cls=MyTypedDict
)
v = SchemaValidator(wrapper_schema)
assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}
```
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
Expand All @@ -2873,6 +2881,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,
Expand Down

0 comments on commit b127652

Please sign in to comment.