@@ -2609,7 +2609,6 @@ class TypedDictField(TypedDict, total=False):
2609
2609
validation_alias : Union [str , List [Union [str , int ]], List [List [Union [str , int ]]]]
2610
2610
serialization_alias : str
2611
2611
serialization_exclude : bool # default: False
2612
- frozen : bool
2613
2612
metadata : Any
2614
2613
2615
2614
@@ -2620,7 +2619,6 @@ def typed_dict_field(
2620
2619
validation_alias : str | list [str | int ] | list [list [str | int ]] | None = None ,
2621
2620
serialization_alias : str | None = None ,
2622
2621
serialization_exclude : bool | None = None ,
2623
- frozen : bool | None = None ,
2624
2622
metadata : Any = None ,
2625
2623
) -> TypedDictField :
2626
2624
"""
@@ -2638,7 +2636,6 @@ def typed_dict_field(
2638
2636
validation_alias: The alias(es) to use to find the field in the validation data
2639
2637
serialization_alias: The alias to use as a key when serializing
2640
2638
serialization_exclude: Whether to exclude the field when serializing
2641
- frozen: Whether the field is frozen
2642
2639
metadata: Any other information you want to include with the schema, not used by pydantic-core
2643
2640
"""
2644
2641
return dict_not_none (
@@ -2648,7 +2645,6 @@ def typed_dict_field(
2648
2645
validation_alias = validation_alias ,
2649
2646
serialization_alias = serialization_alias ,
2650
2647
serialization_exclude = serialization_exclude ,
2651
- frozen = frozen ,
2652
2648
metadata = metadata ,
2653
2649
)
2654
2650
@@ -2659,12 +2655,10 @@ class TypedDictSchema(TypedDict, total=False):
2659
2655
computed_fields : List [ComputedField ]
2660
2656
strict : bool
2661
2657
extra_validator : CoreSchema
2662
- return_fields_set : bool
2663
2658
# all these values can be set via config, equivalent fields have `typed_dict_` prefix
2664
2659
extra_behavior : ExtraBehavior
2665
2660
total : bool # default: True
2666
2661
populate_by_name : bool # replaces `allow_population_by_field_name` in pydantic v1
2667
- from_attributes : bool
2668
2662
ref : str
2669
2663
metadata : Any
2670
2664
serialization : SerSchema
@@ -2676,11 +2670,9 @@ def typed_dict_schema(
2676
2670
computed_fields : list [ComputedField ] | None = None ,
2677
2671
strict : bool | None = None ,
2678
2672
extra_validator : CoreSchema | None = None ,
2679
- return_fields_set : bool | None = None ,
2680
2673
extra_behavior : ExtraBehavior | None = None ,
2681
2674
total : bool | None = None ,
2682
2675
populate_by_name : bool | None = None ,
2683
- from_attributes : bool | None = None ,
2684
2676
ref : str | None = None ,
2685
2677
metadata : Any = None ,
2686
2678
serialization : SerSchema | None = None ,
@@ -2703,13 +2695,11 @@ def typed_dict_schema(
2703
2695
computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
2704
2696
strict: Whether the typed dict is strict
2705
2697
extra_validator: The extra validator to use for the typed dict
2706
- return_fields_set: Whether the typed dict should return a fields set
2707
2698
ref: optional unique identifier of the schema, used to reference the schema in other places
2708
2699
metadata: Any other information you want to include with the schema, not used by pydantic-core
2709
2700
extra_behavior: The extra behavior to use for the typed dict
2710
2701
total: Whether the typed dict is total
2711
2702
populate_by_name: Whether the typed dict should populate by name
2712
- from_attributes: Whether the typed dict should be populated from attributes
2713
2703
serialization: Custom serialization schema
2714
2704
"""
2715
2705
return dict_not_none (
@@ -2718,10 +2708,124 @@ def typed_dict_schema(
2718
2708
computed_fields = computed_fields ,
2719
2709
strict = strict ,
2720
2710
extra_validator = extra_validator ,
2721
- return_fields_set = return_fields_set ,
2722
2711
extra_behavior = extra_behavior ,
2723
2712
total = total ,
2724
2713
populate_by_name = populate_by_name ,
2714
+ ref = ref ,
2715
+ metadata = metadata ,
2716
+ serialization = serialization ,
2717
+ )
2718
+
2719
+
2720
+ class ModelField (TypedDict , total = False ):
2721
+ type : Required [Literal ['model-field' ]]
2722
+ schema : Required [CoreSchema ]
2723
+ validation_alias : Union [str , List [Union [str , int ]], List [List [Union [str , int ]]]]
2724
+ serialization_alias : str
2725
+ serialization_exclude : bool # default: False
2726
+ frozen : bool
2727
+ metadata : Any
2728
+
2729
+
2730
+ def model_field (
2731
+ schema : CoreSchema ,
2732
+ * ,
2733
+ validation_alias : str | list [str | int ] | list [list [str | int ]] | None = None ,
2734
+ serialization_alias : str | None = None ,
2735
+ serialization_exclude : bool | None = None ,
2736
+ frozen : bool | None = None ,
2737
+ metadata : Any = None ,
2738
+ ) -> ModelField :
2739
+ """
2740
+ Returns a schema for a model field, e.g.:
2741
+
2742
+ ```py
2743
+ from pydantic_core import core_schema
2744
+
2745
+ field = core_schema.model_field(schema=core_schema.int_schema())
2746
+ ```
2747
+
2748
+ Args:
2749
+ schema: The schema to use for the field
2750
+ validation_alias: The alias(es) to use to find the field in the validation data
2751
+ serialization_alias: The alias to use as a key when serializing
2752
+ serialization_exclude: Whether to exclude the field when serializing
2753
+ frozen: Whether the field is frozen
2754
+ metadata: Any other information you want to include with the schema, not used by pydantic-core
2755
+ """
2756
+ return dict_not_none (
2757
+ type = 'model-field' ,
2758
+ schema = schema ,
2759
+ validation_alias = validation_alias ,
2760
+ serialization_alias = serialization_alias ,
2761
+ serialization_exclude = serialization_exclude ,
2762
+ frozen = frozen ,
2763
+ metadata = metadata ,
2764
+ )
2765
+
2766
+
2767
+ class ModelFieldsSchema (TypedDict , total = False ):
2768
+ type : Required [Literal ['model-fields' ]]
2769
+ fields : Required [Dict [str , ModelField ]]
2770
+ computed_fields : List [ComputedField ]
2771
+ strict : bool
2772
+ extra_validator : CoreSchema
2773
+ # all these values can be set via config, equivalent fields have `typed_dict_` prefix
2774
+ extra_behavior : ExtraBehavior
2775
+ populate_by_name : bool # replaces `allow_population_by_field_name` in pydantic v1
2776
+ from_attributes : bool
2777
+ ref : str
2778
+ metadata : Any
2779
+ serialization : SerSchema
2780
+
2781
+
2782
+ def model_fields_schema (
2783
+ fields : Dict [str , ModelField ],
2784
+ * ,
2785
+ computed_fields : list [ComputedField ] | None = None ,
2786
+ strict : bool | None = None ,
2787
+ extra_validator : CoreSchema | None = None ,
2788
+ extra_behavior : ExtraBehavior | None = None ,
2789
+ populate_by_name : bool | None = None ,
2790
+ from_attributes : bool | None = None ,
2791
+ ref : str | None = None ,
2792
+ metadata : Any = None ,
2793
+ serialization : SerSchema | None = None ,
2794
+ ) -> ModelFieldsSchema :
2795
+ """
2796
+ Returns a schema that matches a typed dict, e.g.:
2797
+
2798
+ ```py
2799
+ from pydantic_core import SchemaValidator, core_schema
2800
+
2801
+ wrapper_schema = core_schema.model_fields_schema(
2802
+ {'a': core_schema.model_field(core_schema.str_schema())}
2803
+ )
2804
+ v = SchemaValidator(wrapper_schema)
2805
+ print(v.validate_python({'a': 'hello'}))
2806
+ #> ({'a': 'hello'}, None, {'a'})
2807
+ ```
2808
+
2809
+ Args:
2810
+ fields: The fields to use for the typed dict
2811
+ computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
2812
+ strict: Whether the typed dict is strict
2813
+ extra_validator: The extra validator to use for the typed dict
2814
+ ref: optional unique identifier of the schema, used to reference the schema in other places
2815
+ metadata: Any other information you want to include with the schema, not used by pydantic-core
2816
+ extra_behavior: The extra behavior to use for the typed dict
2817
+ populate_by_name: Whether the typed dict should populate by name
2818
+ from_attributes: Whether the typed dict should be populated from attributes
2819
+ serialization: Custom serialization schema
2820
+ """
2821
+ return dict_not_none (
2822
+ type = 'model-fields' ,
2823
+ fields = fields ,
2824
+ computed_fields = computed_fields ,
2825
+ strict = strict ,
2826
+ extra_validator = extra_validator ,
2827
+ extra_behavior = extra_behavior ,
2828
+ populate_by_name = populate_by_name ,
2725
2829
from_attributes = from_attributes ,
2726
2830
ref = ref ,
2727
2831
metadata = metadata ,
@@ -2768,14 +2872,13 @@ def model_schema(
2768
2872
from pydantic_core import CoreConfig, SchemaValidator, core_schema
2769
2873
2770
2874
class MyModel:
2771
- __slots__ = '__dict__', '__pydantic_fields_set__'
2875
+ __slots__ = '__dict__', '__pydantic_extra__', ' __pydantic_fields_set__'
2772
2876
2773
2877
schema = core_schema.model_schema(
2774
2878
cls=MyModel,
2775
2879
config=CoreConfig(str_max_length=5),
2776
- schema=core_schema.typed_dict_schema(
2777
- fields={'a': core_schema.typed_dict_field(core_schema.str_schema())},
2778
- return_fields_set=True,
2880
+ schema=core_schema.model_fields_schema(
2881
+ fields={'a': core_schema.model_field(core_schema.str_schema())},
2779
2882
),
2780
2883
)
2781
2884
v = SchemaValidator(schema)
@@ -3236,16 +3339,15 @@ def json_schema(
3236
3339
```py
3237
3340
from pydantic_core import SchemaValidator, core_schema
3238
3341
3239
- dict_schema = core_schema.typed_dict_schema (
3342
+ dict_schema = core_schema.model_fields_schema (
3240
3343
{
3241
- 'field_a': core_schema.typed_dict_field (core_schema.str_schema()),
3242
- 'field_b': core_schema.typed_dict_field (core_schema.bool_schema()),
3344
+ 'field_a': core_schema.model_field (core_schema.str_schema()),
3345
+ 'field_b': core_schema.model_field (core_schema.bool_schema()),
3243
3346
},
3244
- return_fields_set=True,
3245
3347
)
3246
3348
3247
3349
class MyModel:
3248
- __slots__ = '__dict__', '__pydantic_fields_set__'
3350
+ __slots__ = '__dict__', '__pydantic_extra__', ' __pydantic_fields_set__'
3249
3351
field_a: str
3250
3352
field_b: bool
3251
3353
@@ -3497,6 +3599,7 @@ def definition_reference_schema(
3497
3599
ChainSchema ,
3498
3600
LaxOrStrictSchema ,
3499
3601
TypedDictSchema ,
3602
+ ModelFieldsSchema ,
3500
3603
ModelSchema ,
3501
3604
DataclassArgsSchema ,
3502
3605
DataclassSchema ,
@@ -3548,6 +3651,7 @@ def definition_reference_schema(
3548
3651
'chain' ,
3549
3652
'lax-or-strict' ,
3550
3653
'typed-dict' ,
3654
+ 'model-fields' ,
3551
3655
'model' ,
3552
3656
'dataclass-args' ,
3553
3657
'dataclass' ,
0 commit comments