Skip to content

Commit e442820

Browse files
committed
Flatten schemas and replace macros with boilerplate
1 parent 7a46664 commit e442820

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+444
-650
lines changed

generate_self_schema.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,26 +197,9 @@ def main() -> None:
197197
assert m, f'Unknown schema type: {type_}'
198198
key = m.group(1)
199199
value = get_schema(s)
200-
if key == 'function':
201-
mode = value['fields']['mode']['schema']['expected']
202-
if mode == ['plain']:
203-
key = 'function-plain'
204-
elif mode == ['wrap']:
205-
key = 'function-wrap'
206-
elif key == 'tuple':
207-
if value['fields']['mode']['schema']['expected'] == ['positional']:
208-
key = 'tuple-positional'
209-
else:
210-
key = 'tuple-variable'
211-
212200
choices[key] = value
213201

214-
schema = {
215-
'type': 'tagged-union',
216-
'ref': 'root-schema',
217-
'discriminator': 'self-schema-discriminator',
218-
'choices': choices,
219-
}
202+
schema = {'type': 'tagged-union', 'ref': 'root-schema', 'discriminator': 'type', 'choices': choices}
220203
python_code = (
221204
f'# this file is auto-generated by generate_self_schema.py, DO NOT edit manually\nself_schema = {schema}\n'
222205
)

pydantic_core/core_schema.py

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,7 @@ def list_schema(
10921092

10931093

10941094
class TuplePositionalSchema(TypedDict, total=False):
1095-
type: Required[Literal['tuple']]
1096-
mode: Required[Literal['positional']]
1095+
type: Required[Literal['tuple-positional']]
10971096
items_schema: Required[List[CoreSchema]]
10981097
extra_schema: CoreSchema
10991098
strict: bool
@@ -1132,8 +1131,7 @@ def tuple_positional_schema(
11321131
serialization: Custom serialization schema
11331132
"""
11341133
return dict_not_none(
1135-
type='tuple',
1136-
mode='positional',
1134+
type='tuple-positional',
11371135
items_schema=items_schema,
11381136
extra_schema=extra_schema,
11391137
strict=strict,
@@ -1144,8 +1142,7 @@ def tuple_positional_schema(
11441142

11451143

11461144
class TupleVariableSchema(TypedDict, total=False):
1147-
type: Required[Literal['tuple']]
1148-
mode: Literal['variable']
1145+
type: Required[Literal['tuple-variable']]
11491146
items_schema: CoreSchema
11501147
min_length: int
11511148
max_length: int
@@ -1185,8 +1182,7 @@ def tuple_variable_schema(
11851182
serialization: Custom serialization schema
11861183
"""
11871184
return dict_not_none(
1188-
type='tuple',
1189-
mode='variable',
1185+
type='tuple-variable',
11901186
items_schema=items_schema,
11911187
min_length=min_length,
11921188
max_length=max_length,
@@ -1447,24 +1443,30 @@ def __call__(self, __input_value: Any, __info: ValidationInfo) -> Any: # pragma
14471443
...
14481444

14491445

1450-
class FunctionSchema(TypedDict, total=False):
1451-
type: Required[Literal['function']]
1452-
mode: Required[Literal['before', 'after']]
1446+
class _FunctionSchema(TypedDict, total=False):
14531447
function: Required[ValidatorFunction]
14541448
schema: Required[CoreSchema]
14551449
ref: str
14561450
metadata: Any
14571451
serialization: SerSchema
14581452

14591453

1454+
class FunctionBeforeSchema(_FunctionSchema, total=False):
1455+
type: Required[Literal['function-before']]
1456+
1457+
1458+
class FunctionAfterSchema(_FunctionSchema, total=False):
1459+
type: Required[Literal['function-after']]
1460+
1461+
14601462
def function_before_schema(
14611463
function: ValidatorFunction,
14621464
schema: CoreSchema,
14631465
*,
14641466
ref: str | None = None,
14651467
metadata: Any = None,
14661468
serialization: SerSchema | None = None,
1467-
) -> FunctionSchema:
1469+
) -> FunctionBeforeSchema:
14681470
"""
14691471
Returns a schema that calls a validator function before validating the provided schema, e.g.:
14701472
@@ -1490,8 +1492,7 @@ def fn(v: Any, info: core_schema.ValidationInfo) -> str:
14901492
serialization: Custom serialization schema
14911493
"""
14921494
return dict_not_none(
1493-
type='function',
1494-
mode='before',
1495+
type='function-before',
14951496
function=function,
14961497
schema=schema,
14971498
ref=ref,
@@ -1507,7 +1508,7 @@ def function_after_schema(
15071508
ref: str | None = None,
15081509
metadata: Any = None,
15091510
serialization: SerSchema | None = None,
1510-
) -> FunctionSchema:
1511+
) -> FunctionAfterSchema:
15111512
"""
15121513
Returns a schema that calls a validator function after validating the provided schema, e.g.:
15131514
@@ -1531,13 +1532,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
15311532
serialization: Custom serialization schema
15321533
"""
15331534
return dict_not_none(
1534-
type='function',
1535-
mode='after',
1536-
function=function,
1537-
schema=schema,
1538-
ref=ref,
1539-
metadata=metadata,
1540-
serialization=serialization,
1535+
type='function-after', function=function, schema=schema, ref=ref, metadata=metadata, serialization=serialization
15411536
)
15421537

15431538

@@ -1554,8 +1549,7 @@ def __call__(
15541549

15551550

15561551
class FunctionWrapSchema(TypedDict, total=False):
1557-
type: Required[Literal['function']]
1558-
mode: Required[Literal['wrap']]
1552+
type: Required[Literal['function-wrap']]
15591553
function: Required[WrapValidatorFunction]
15601554
schema: Required[CoreSchema]
15611555
ref: str
@@ -1595,19 +1589,12 @@ def fn(v: str, validator: core_schema.CallableValidator, info: core_schema.Valid
15951589
serialization: Custom serialization schema
15961590
"""
15971591
return dict_not_none(
1598-
type='function',
1599-
mode='wrap',
1600-
function=function,
1601-
schema=schema,
1602-
ref=ref,
1603-
metadata=metadata,
1604-
serialization=serialization,
1592+
type='function-wrap', function=function, schema=schema, ref=ref, metadata=metadata, serialization=serialization
16051593
)
16061594

16071595

16081596
class FunctionPlainSchema(TypedDict, total=False):
1609-
type: Required[Literal['function']]
1610-
mode: Required[Literal['plain']]
1597+
type: Required[Literal['function-plain']]
16111598
function: Required[ValidatorFunction]
16121599
ref: str
16131600
metadata: Any
@@ -1639,7 +1626,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
16391626
serialization: Custom serialization schema
16401627
"""
16411628
return dict_not_none(
1642-
type='function', mode='plain', function=function, ref=ref, metadata=metadata, serialization=serialization
1629+
type='function-plain', function=function, ref=ref, metadata=metadata, serialization=serialization
16431630
)
16441631

16451632

@@ -2638,7 +2625,8 @@ def definition_reference_schema(
26382625
FrozenSetSchema,
26392626
GeneratorSchema,
26402627
DictSchema,
2641-
FunctionSchema,
2628+
FunctionBeforeSchema,
2629+
FunctionAfterSchema,
26422630
FunctionWrapSchema,
26432631
FunctionPlainSchema,
26442632
WithDefaultSchema,
@@ -2677,12 +2665,16 @@ def definition_reference_schema(
26772665
'is-subclass',
26782666
'callable',
26792667
'list',
2680-
'tuple',
2668+
'tuple-positional',
2669+
'tuple-variable',
26812670
'set',
26822671
'frozenset',
26832672
'generator',
26842673
'dict',
2685-
'function',
2674+
'function-before',
2675+
'function-after',
2676+
'function-wrap',
2677+
'function-plain',
26862678
'default',
26872679
'nullable',
26882680
'union',

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly
1+
stable

0 commit comments

Comments
 (0)