-
Notifications
You must be signed in to change notification settings - Fork 242
/
core_schema.py
1227 lines (1041 loc) · 31 KB
/
core_schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations as _annotations
import sys
from datetime import date, datetime, time, timedelta
from typing import Any, Callable, Dict, List, Optional, Set, Type, Union
if sys.version_info < (3, 11):
from typing_extensions import Protocol, Required
else:
from typing import Protocol, Required
if sys.version_info < (3, 9):
from typing_extensions import Literal, TypedDict
else:
from typing import Literal, TypedDict
def dict_not_none(**kwargs: Any) -> Any:
return {k: v for k, v in kwargs.items() if v is not None}
class CoreConfig(TypedDict, total=False):
title: str
strict: bool
# higher priority configs take precedence of over lower, if priority matches the two configs are merged, default 0
config_choose_priority: int
# if configs are merged, which should take precedence, default 0, default means child takes precedence
config_merge_priority: int
# settings related to typed_dicts only
typed_dict_extra_behavior: Literal['allow', 'forbid', 'ignore']
typed_dict_total: bool # default: True
# used on typed-dicts and tagged union keys
from_attributes: bool
revalidate_models: bool
# used on typed-dicts and arguments
populate_by_name: bool # replaces `allow_population_by_field_name` in pydantic v1
# fields related to string fields only
str_max_length: int
str_min_length: int
str_strip_whitespace: bool
str_to_lower: bool
str_to_upper: bool
# fields related to float fields only
allow_inf_nan: bool # default: True
class AnySchema(TypedDict, total=False):
type: Required[Literal['any']]
ref: str
extra: Any
def any_schema(*, ref: str | None = None, extra: Any = None) -> AnySchema:
return dict_not_none(type='any', ref=ref, extra=extra)
class NoneSchema(TypedDict, total=False):
type: Required[Literal['none']]
ref: str
extra: Any
def none_schema(*, ref: str | None = None, extra: Any = None) -> NoneSchema:
return dict_not_none(type='none', ref=ref, extra=extra)
class BoolSchema(TypedDict, total=False):
type: Required[Literal['bool']]
strict: bool
ref: str
extra: Any
def bool_schema(strict: bool | None = None, ref: str | None = None, extra: Any = None) -> BoolSchema:
return dict_not_none(type='bool', strict=strict, ref=ref, extra=extra)
class IntSchema(TypedDict, total=False):
type: Required[Literal['int']]
multiple_of: int
le: int
ge: int
lt: int
gt: int
strict: bool
ref: str
extra: Any
def int_schema(
*,
multiple_of: int | None = None,
le: int | None = None,
ge: int | None = None,
lt: int | None = None,
gt: int | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> IntSchema:
return dict_not_none(
type='int', multiple_of=multiple_of, le=le, ge=ge, lt=lt, gt=gt, strict=strict, ref=ref, extra=extra
)
class FloatSchema(TypedDict, total=False):
type: Required[Literal['float']]
allow_inf_nan: bool # whether 'NaN', '+inf', '-inf' should be forbidden. default: True
multiple_of: float
le: float
ge: float
lt: float
gt: float
strict: bool
ref: str
extra: Any
def float_schema(
*,
allow_inf_nan: bool | None = None,
multiple_of: float | None = None,
le: float | None = None,
ge: float | None = None,
lt: float | None = None,
gt: float | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> FloatSchema:
return dict_not_none(
type='float',
allow_inf_nan=allow_inf_nan,
multiple_of=multiple_of,
le=le,
ge=ge,
lt=lt,
gt=gt,
strict=strict,
ref=ref,
extra=extra,
)
class StringSchema(TypedDict, total=False):
type: Required[Literal['str']]
pattern: str
max_length: int
min_length: int
strip_whitespace: bool
to_lower: bool
to_upper: bool
strict: bool
ref: str
extra: Any
def string_schema(
*,
pattern: str | None = None,
max_length: int | None = None,
min_length: int | None = None,
strip_whitespace: bool | None = None,
to_lower: bool | None = None,
to_upper: bool | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> StringSchema:
return dict_not_none(
type='str',
pattern=pattern,
max_length=max_length,
min_length=min_length,
strip_whitespace=strip_whitespace,
to_lower=to_lower,
to_upper=to_upper,
strict=strict,
ref=ref,
extra=extra,
)
class BytesSchema(TypedDict, total=False):
type: Required[Literal['bytes']]
max_length: int
min_length: int
strict: bool
ref: str
extra: Any
def bytes_schema(
*,
max_length: int | None = None,
min_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> BytesSchema:
return dict_not_none(
type='bytes', max_length=max_length, min_length=min_length, strict=strict, ref=ref, extra=extra
)
class DateSchema(TypedDict, total=False):
type: Required[Literal['date']]
strict: bool
le: date
ge: date
lt: date
gt: date
now_op: Literal['past', 'future']
# defaults to current local utc offset from `time.localtime().tm_gmtoff`
# value is restricted to -86_400 < offset < 86_400 by bounds in generate_self_schema.py
now_utc_offset: int
ref: str
extra: Any
def date_schema(
*,
strict: bool | None = None,
le: date | None = None,
ge: date | None = None,
lt: date | None = None,
gt: date | None = None,
ref: str | None = None,
now_op: Literal['past', 'future'] | None = None,
now_utc_offset: int | None = None,
extra: Any = None,
) -> DateSchema:
return dict_not_none(
type='date',
strict=strict,
le=le,
ge=ge,
lt=lt,
gt=gt,
now_op=now_op,
now_utc_offset=now_utc_offset,
ref=ref,
extra=extra,
)
class TimeSchema(TypedDict, total=False):
type: Required[Literal['time']]
strict: bool
le: time
ge: time
lt: time
gt: time
ref: str
extra: Any
def time_schema(
*,
strict: bool | None = None,
le: time | None = None,
ge: time | None = None,
lt: time | None = None,
gt: time | None = None,
ref: str | None = None,
extra: Any = None,
) -> TimeSchema:
return dict_not_none(type='time', strict=strict, le=le, ge=ge, lt=lt, gt=gt, ref=ref, extra=extra)
class DatetimeSchema(TypedDict, total=False):
type: Required[Literal['datetime']]
strict: bool
le: datetime
ge: datetime
lt: datetime
gt: datetime
now_op: Literal['past', 'future']
# defaults to current local utc offset from `time.localtime().tm_gmtoff`
# value is restricted to -86_400 < offset < 86_400 by bounds in generate_self_schema.py
now_utc_offset: int
ref: str
extra: Any
def datetime_schema(
*,
strict: bool | None = None,
le: datetime | None = None,
ge: datetime | None = None,
lt: datetime | None = None,
gt: datetime | None = None,
now_op: Literal['past', 'future'] | None = None,
now_utc_offset: int | None = None,
ref: str | None = None,
extra: Any = None,
) -> DatetimeSchema:
return dict_not_none(
type='datetime',
strict=strict,
le=le,
ge=ge,
lt=lt,
gt=gt,
now_op=now_op,
now_utc_offset=now_utc_offset,
ref=ref,
extra=extra,
)
class TimedeltaSchema(TypedDict, total=False):
type: Required[Literal['timedelta']]
strict: bool
le: timedelta
ge: timedelta
lt: timedelta
gt: timedelta
ref: str
extra: Any
def timedelta_schema(
*,
strict: bool | None = None,
le: timedelta | None = None,
ge: timedelta | None = None,
lt: timedelta | None = None,
gt: timedelta | None = None,
ref: str | None = None,
extra: Any = None,
) -> TimedeltaSchema:
return dict_not_none(type='timedelta', strict=strict, le=le, ge=ge, lt=lt, gt=gt, ref=ref, extra=extra)
class LiteralSchema(TypedDict, total=False):
type: Required[Literal['literal']]
expected: Required[List[Any]]
ref: str
extra: Any
def literal_schema(*expected: Any, ref: str | None = None, extra: Any = None) -> LiteralSchema:
return dict_not_none(type='literal', expected=expected, ref=ref, extra=extra)
# must match input/parse_json.rs::JsonType::try_from
JsonType = Literal['null', 'bool', 'int', 'float', 'str', 'list', 'dict']
class IsInstanceSchema(TypedDict, total=False):
type: Required[Literal['is-instance']]
cls: Required[Any]
cls_repr: str
json_types: Set[JsonType]
json_function: Callable[[Any], Any]
ref: str
extra: Any
def is_instance_schema(
cls: Any,
*,
json_types: Set[JsonType] | None = None,
json_function: Callable[[Any], Any] | None = None,
cls_repr: str | None = None,
ref: str | None = None,
extra: Any = None,
) -> IsInstanceSchema:
return dict_not_none(
type='is-instance',
cls=cls,
json_types=json_types,
json_function=json_function,
cls_repr=cls_repr,
ref=ref,
extra=extra,
)
class IsSubclassSchema(TypedDict, total=False):
type: Required[Literal['is-subclass']]
cls: Required[Type[Any]]
cls_repr: str
ref: str
extra: Any
def is_subclass_schema(
cls: Type[Any], *, cls_repr: str | None = None, ref: str | None = None, extra: Any = None
) -> IsInstanceSchema:
return dict_not_none(type='is-subclass', cls=cls, cls_repr=cls_repr, ref=ref, extra=extra)
class CallableSchema(TypedDict, total=False):
type: Required[Literal['callable']]
ref: str
extra: Any
def callable_schema(*, ref: str | None = None, extra: Any = None) -> CallableSchema:
return dict_not_none(type='callable', ref=ref, extra=extra)
class ListSchema(TypedDict, total=False):
type: Required[Literal['list']]
items_schema: CoreSchema
min_length: int
max_length: int
strict: bool
allow_any_iter: bool
ref: str
extra: Any
def list_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
allow_any_iter: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> ListSchema:
return dict_not_none(
type='list',
items_schema=items_schema,
min_length=min_length,
max_length=max_length,
strict=strict,
allow_any_iter=allow_any_iter,
ref=ref,
extra=extra,
)
class TuplePositionalSchema(TypedDict, total=False):
type: Required[Literal['tuple']]
mode: Required[Literal['positional']]
items_schema: Required[List[CoreSchema]]
extra_schema: CoreSchema
strict: bool
ref: str
extra: Any
def tuple_positional_schema(
*items_schema: CoreSchema,
extra_schema: CoreSchema | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> TuplePositionalSchema:
return dict_not_none(
type='tuple',
mode='positional',
items_schema=items_schema,
extra_schema=extra_schema,
strict=strict,
ref=ref,
extra=extra,
)
class TupleVariableSchema(TypedDict, total=False):
type: Required[Literal['tuple']]
mode: Literal['variable']
items_schema: CoreSchema
min_length: int
max_length: int
strict: bool
ref: str
extra: Any
def tuple_variable_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> TupleVariableSchema:
return dict_not_none(
type='tuple',
mode='variable',
items_schema=items_schema,
min_length=min_length,
max_length=max_length,
strict=strict,
ref=ref,
extra=extra,
)
class SetSchema(TypedDict, total=False):
type: Required[Literal['set']]
items_schema: CoreSchema
min_length: int
max_length: int
generator_max_length: int
strict: bool
ref: str
extra: Any
def set_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
generator_max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> SetSchema:
return dict_not_none(
type='set',
items_schema=items_schema,
min_length=min_length,
max_length=max_length,
generator_max_length=generator_max_length,
strict=strict,
ref=ref,
extra=extra,
)
class FrozenSetSchema(TypedDict, total=False):
type: Required[Literal['frozenset']]
items_schema: CoreSchema
min_length: int
max_length: int
generator_max_length: int
strict: bool
ref: str
extra: Any
def frozenset_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
generator_max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> FrozenSetSchema:
return dict_not_none(
type='frozenset',
items_schema=items_schema,
min_length=min_length,
max_length=max_length,
generator_max_length=generator_max_length,
strict=strict,
ref=ref,
extra=extra,
)
class GeneratorSchema(TypedDict, total=False):
type: Required[Literal['generator']]
items_schema: CoreSchema
max_length: int
ref: str
extra: Any
def generator_schema(
items_schema: CoreSchema | None = None, *, max_length: int | None = None, ref: str | None = None, extra: Any = None
) -> GeneratorSchema:
return dict_not_none(type='generator', items_schema=items_schema, max_length=max_length, ref=ref, extra=extra)
class DictSchema(TypedDict, total=False):
type: Required[Literal['dict']]
keys_schema: CoreSchema # default: AnySchema
values_schema: CoreSchema # default: AnySchema
min_length: int
max_length: int
strict: bool
ref: str
extra: Any
def dict_schema(
keys_schema: CoreSchema | None = None,
values_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> DictSchema:
return dict_not_none(
type='dict',
keys_schema=keys_schema,
values_schema=values_schema,
min_length=min_length,
max_length=max_length,
strict=strict,
ref=ref,
extra=extra,
)
class ValidatorFunction(Protocol):
def __call__(
self, __input_value: Any, *, data: Any, config: CoreConfig | None, context: Any, **future_kwargs: Any
) -> Any: # pragma: no cover
...
class FunctionSchema(TypedDict, total=False):
type: Required[Literal['function']]
mode: Required[Literal['before', 'after']]
function: Required[ValidatorFunction]
schema: Required[CoreSchema]
ref: str
extra: Any
def function_before_schema(
function: ValidatorFunction, schema: CoreSchema, *, ref: str | None = None, extra: Any = None
) -> FunctionSchema:
return dict_not_none(type='function', mode='before', function=function, schema=schema, ref=ref, extra=extra)
def function_after_schema(
schema: CoreSchema, function: ValidatorFunction, *, ref: str | None = None, extra: Any = None
) -> FunctionSchema:
return dict_not_none(type='function', mode='after', function=function, schema=schema, ref=ref, extra=extra)
class CallableValidator(Protocol):
def __call__(self, input_value: Any, outer_location: str | int | None = None) -> Any: # pragma: no cover
...
class WrapValidatorFunction(Protocol):
def __call__(
self,
__input_value: Any,
*,
validator: CallableValidator,
data: Any,
config: CoreConfig | None,
context: Any,
**future_kwargs: Any,
) -> Any: # pragma: no cover
...
class FunctionWrapSchema(TypedDict, total=False):
type: Required[Literal['function']]
mode: Required[Literal['wrap']]
function: Required[WrapValidatorFunction]
schema: Required[CoreSchema]
ref: str
extra: Any
def function_wrap_schema(
function: WrapValidatorFunction, schema: CoreSchema, *, ref: str | None = None, extra: Any = None
) -> FunctionWrapSchema:
return dict_not_none(type='function', mode='wrap', function=function, schema=schema, ref=ref, extra=extra)
class FunctionPlainSchema(TypedDict, total=False):
type: Required[Literal['function']]
mode: Required[Literal['plain']]
function: Required[ValidatorFunction]
ref: str
extra: Any
def function_plain_schema(
function: ValidatorFunction, *, ref: str | None = None, extra: Any = None
) -> FunctionPlainSchema:
return dict_not_none(type='function', mode='plain', function=function, ref=ref, extra=extra)
class WithDefaultSchema(TypedDict, total=False):
type: Required[Literal['default']]
schema: Required[CoreSchema]
default: Any
default_factory: Callable[[], Any]
on_error: Literal['raise', 'omit', 'default'] # default: 'raise'
strict: bool
ref: str
extra: Any
Omitted = object()
def with_default_schema(
schema: CoreSchema,
*,
default: Any = Omitted,
default_factory: Callable[[], Any] | None = None,
on_error: Literal['raise', 'omit', 'default'] | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> WithDefaultSchema:
s = dict_not_none(
type='default',
schema=schema,
default_factory=default_factory,
on_error=on_error,
strict=strict,
ref=ref,
extra=extra,
)
if default is not Omitted:
s['default'] = default
return s
class NullableSchema(TypedDict, total=False):
type: Required[Literal['nullable']]
schema: Required[CoreSchema]
strict: bool
ref: str
extra: Any
def nullable_schema(
schema: CoreSchema, *, strict: bool | None = None, ref: str | None = None, extra: Any = None
) -> NullableSchema:
return dict_not_none(type='nullable', schema=schema, strict=strict, ref=ref, extra=extra)
class UnionSchema(TypedDict, total=False):
type: Required[Literal['union']]
choices: Required[List[CoreSchema]]
custom_error_type: str
custom_error_message: str
custom_error_context: Dict[str, Union[str, int, float]]
strict: bool
ref: str
extra: Any
def union_schema(
*choices: CoreSchema,
custom_error_type: str | None = None,
custom_error_message: str | None = None,
custom_error_context: dict[str, str | int] | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> UnionSchema:
return dict_not_none(
type='union',
choices=choices,
custom_error_type=custom_error_type,
custom_error_message=custom_error_message,
custom_error_context=custom_error_context,
strict=strict,
ref=ref,
extra=extra,
)
class TaggedUnionSchema(TypedDict, total=False):
type: Required[Literal['tagged-union']]
choices: Required[Dict[str, CoreSchema]]
discriminator: Required[
Union[str, List[Union[str, int]], List[List[Union[str, int]]], Callable[[Any], Optional[str]]]
]
custom_error_type: str
custom_error_message: str
custom_error_context: Dict[str, Union[str, int, float]]
strict: bool
ref: str
extra: Any
def tagged_union_schema(
choices: Dict[str, CoreSchema],
discriminator: str | list[str | int] | list[list[str | int]] | Callable[[Any], str | None],
*,
custom_error_type: str | None = None,
custom_error_message: str | None = None,
custom_error_context: dict[str, int | str | float] | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
) -> TaggedUnionSchema:
return dict_not_none(
type='tagged-union',
choices=choices,
discriminator=discriminator,
custom_error_type=custom_error_type,
custom_error_message=custom_error_message,
custom_error_context=custom_error_context,
strict=strict,
ref=ref,
extra=extra,
)
class ChainSchema(TypedDict, total=False):
type: Required[Literal['chain']]
steps: Required[List[CoreSchema]]
ref: str
extra: Any
def chain_schema(*steps: CoreSchema, ref: str | None = None, extra: Any = None) -> ChainSchema:
return dict_not_none(type='chain', steps=steps, ref=ref, extra=extra)
class TypedDictField(TypedDict, total=False):
schema: Required[CoreSchema]
required: bool
alias: Union[str, List[Union[str, int]], List[List[Union[str, int]]]]
frozen: bool
def typed_dict_field(
schema: CoreSchema,
*,
required: bool | None = None,
alias: str | list[str | int] | list[list[str | int]] | None = None,
frozen: bool | None = None,
) -> TypedDictField:
return dict_not_none(schema=schema, required=required, alias=alias, frozen=frozen)
class TypedDictSchema(TypedDict, total=False):
type: Required[Literal['typed-dict']]
fields: Required[Dict[str, TypedDictField]]
strict: bool
extra_validator: CoreSchema
return_fields_set: bool
ref: str
extra: Any
# all these values can be set via config, equivalent fields have `typed_dict_` prefix
extra_behavior: Literal['allow', 'forbid', 'ignore']
total: bool # default: True
populate_by_name: bool # replaces `allow_population_by_field_name` in pydantic v1
from_attributes: bool
def typed_dict_schema(
fields: Dict[str, TypedDictField],
*,
strict: bool | None = None,
extra_validator: CoreSchema | None = None,
return_fields_set: bool | None = None,
ref: str | None = None,
extra: Any = None,
extra_behavior: Literal['allow', 'forbid', 'ignore'] | None = None,
total: bool | None = None,
populate_by_name: bool | None = None,
from_attributes: bool | None = None,
) -> TypedDictSchema:
return dict_not_none(
type='typed-dict',
fields=fields,
strict=strict,
extra_validator=extra_validator,
return_fields_set=return_fields_set,
ref=ref,
extra=extra,
extra_behavior=extra_behavior,
total=total,
populate_by_name=populate_by_name,
from_attributes=from_attributes,
)
class NewClassSchema(TypedDict, total=False):
type: Required[Literal['new-class']]
cls: Required[Type[Any]]
schema: Required[CoreSchema]
call_after_init: str
strict: bool
ref: str
extra: Any
config: CoreConfig
def new_class_schema(
cls: Type[Any],
schema: CoreSchema,
*,
call_after_init: str | None = None,
strict: bool | None = None,
ref: str | None = None,
extra: Any = None,
config: CoreConfig | None = None,
) -> NewClassSchema:
return dict_not_none(
type='new-class', cls=cls, schema=schema, call_after_init=call_after_init, strict=strict, ref=ref, config=config
)
class ArgumentsParameter(TypedDict, total=False):
name: Required[str]
schema: Required[CoreSchema]
mode: Literal['positional_only', 'positional_or_keyword', 'keyword_only'] # default positional_or_keyword
alias: Union[str, List[Union[str, int]], List[List[Union[str, int]]]]
def arguments_parameter(
name: str,
schema: CoreSchema,
*,
mode: Literal['positional_only', 'positional_or_keyword', 'keyword_only'] | None = None,
alias: str | list[str | int] | list[list[str | int]] | None = None,
) -> ArgumentsParameter:
return dict_not_none(name=name, schema=schema, mode=mode, alias=alias)
class ArgumentsSchema(TypedDict, total=False):
type: Required[Literal['arguments']]
arguments_schema: Required[List[ArgumentsParameter]]
populate_by_name: bool
var_args_schema: CoreSchema
var_kwargs_schema: CoreSchema
ref: str
extra: Any
def arguments_schema(
*arguments: ArgumentsParameter,
populate_by_name: bool | None = None,
var_args_schema: CoreSchema | None = None,
var_kwargs_schema: CoreSchema | None = None,
ref: str | None = None,
extra: Any = None,
) -> ArgumentsSchema:
return dict_not_none(
type='arguments',
arguments_schema=arguments,
populate_by_name=populate_by_name,
var_args_schema=var_args_schema,
var_kwargs_schema=var_kwargs_schema,
ref=ref,
extra=extra,
)
class CallSchema(TypedDict, total=False):
type: Required[Literal['call']]
arguments_schema: Required[CoreSchema]
function: Required[Callable[..., Any]]
return_schema: CoreSchema
ref: str
extra: Any
def call_schema(
arguments: CoreSchema,
function: Callable[..., Any],
*,
return_schema: CoreSchema | None = None,
ref: str | None = None,
extra: Any = None,
) -> CallSchema:
return dict_not_none(
type='call', arguments_schema=arguments, function=function, return_schema=return_schema, ref=ref, extra=extra
)
class RecursiveReferenceSchema(TypedDict, total=False):
type: Required[Literal['recursive-ref']]
schema_ref: Required[str]
def recursive_reference_schema(schema_ref: str) -> RecursiveReferenceSchema:
return {'type': 'recursive-ref', 'schema_ref': schema_ref}
class CustomErrorSchema(TypedDict, total=False):
type: Required[Literal['custom_error']]
schema: Required[CoreSchema]
custom_error_type: Required[str]
custom_error_message: str
custom_error_context: Dict[str, Union[str, int, float]]
ref: str
extra: Any
def custom_error_schema(
schema: CoreSchema,
custom_error_type: str,
*,
custom_error_message: str | None = None,
custom_error_context: dict[str, str | int | float] | None = None,
ref: str | None = None,
extra: Any = None,