Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding cls reference to TypedDictSchema #1410

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading