@@ -1384,16 +1384,7 @@ def list_schema(
1384
1384
)
1385
1385
1386
1386
1387
- class TuplePositionalSchema (TypedDict , total = False ):
1388
- type : Required [Literal ['tuple-positional' ]]
1389
- items_schema : Required [List [CoreSchema ]]
1390
- extras_schema : CoreSchema
1391
- strict : bool
1392
- ref : str
1393
- metadata : Any
1394
- serialization : IncExSeqOrElseSerSchema
1395
-
1396
-
1387
+ # @deprecated('tuple_positional_schema is deprecated. Use pydantic_core.core_schema.tuple_schema instead.')
1397
1388
def tuple_positional_schema (
1398
1389
items_schema : list [CoreSchema ],
1399
1390
* ,
@@ -1402,7 +1393,7 @@ def tuple_positional_schema(
1402
1393
ref : str | None = None ,
1403
1394
metadata : Any = None ,
1404
1395
serialization : IncExSeqOrElseSerSchema | None = None ,
1405
- ) -> TuplePositionalSchema :
1396
+ ) -> TupleSchema :
1406
1397
"""
1407
1398
Returns a schema that matches a tuple of schemas, e.g.:
1408
1399
@@ -1427,20 +1418,70 @@ def tuple_positional_schema(
1427
1418
metadata: Any other information you want to include with the schema, not used by pydantic-core
1428
1419
serialization: Custom serialization schema
1429
1420
"""
1430
- return _dict_not_none (
1431
- type = 'tuple-positional' ,
1421
+ if extras_schema is not None :
1422
+ variadic_item_index = len (items_schema )
1423
+ items_schema = items_schema + [extras_schema ]
1424
+ else :
1425
+ variadic_item_index = None
1426
+ return tuple_schema (
1432
1427
items_schema = items_schema ,
1433
- extras_schema = extras_schema ,
1428
+ variadic_item_index = variadic_item_index ,
1434
1429
strict = strict ,
1435
1430
ref = ref ,
1436
1431
metadata = metadata ,
1437
1432
serialization = serialization ,
1438
1433
)
1439
1434
1440
1435
1441
- class TupleVariableSchema (TypedDict , total = False ):
1442
- type : Required [Literal ['tuple-variable' ]]
1443
- items_schema : CoreSchema
1436
+ # @deprecated('tuple_variable_schema is deprecated. Use pydantic_core.core_schema.tuple_schema instead.')
1437
+ def tuple_variable_schema (
1438
+ items_schema : CoreSchema | None = None ,
1439
+ * ,
1440
+ min_length : int | None = None ,
1441
+ max_length : int | None = None ,
1442
+ strict : bool | None = None ,
1443
+ ref : str | None = None ,
1444
+ metadata : Any = None ,
1445
+ serialization : IncExSeqOrElseSerSchema | None = None ,
1446
+ ) -> TupleSchema :
1447
+ """
1448
+ Returns a schema that matches a tuple of a given schema, e.g.:
1449
+
1450
+ ```py
1451
+ from pydantic_core import SchemaValidator, core_schema
1452
+
1453
+ schema = core_schema.tuple_variable_schema(
1454
+ items_schema=core_schema.int_schema(), min_length=0, max_length=10
1455
+ )
1456
+ v = SchemaValidator(schema)
1457
+ assert v.validate_python(('1', 2, 3)) == (1, 2, 3)
1458
+ ```
1459
+
1460
+ Args:
1461
+ items_schema: The value must be a tuple with items that match this schema
1462
+ min_length: The value must be a tuple with at least this many items
1463
+ max_length: The value must be a tuple with at most this many items
1464
+ strict: The value must be a tuple with exactly this many items
1465
+ ref: Optional unique identifier of the schema, used to reference the schema in other places
1466
+ metadata: Any other information you want to include with the schema, not used by pydantic-core
1467
+ serialization: Custom serialization schema
1468
+ """
1469
+ return tuple_schema (
1470
+ items_schema = [items_schema or any_schema ()],
1471
+ variadic_item_index = 0 ,
1472
+ min_length = min_length ,
1473
+ max_length = max_length ,
1474
+ strict = strict ,
1475
+ ref = ref ,
1476
+ metadata = metadata ,
1477
+ serialization = serialization ,
1478
+ )
1479
+
1480
+
1481
+ class TupleSchema (TypedDict , total = False ):
1482
+ type : Required [Literal ['tuple' ]]
1483
+ items_schema : Required [List [CoreSchema ]]
1484
+ variadic_item_index : int
1444
1485
min_length : int
1445
1486
max_length : int
1446
1487
strict : bool
@@ -1449,41 +1490,45 @@ class TupleVariableSchema(TypedDict, total=False):
1449
1490
serialization : IncExSeqOrElseSerSchema
1450
1491
1451
1492
1452
- def tuple_variable_schema (
1453
- items_schema : CoreSchema | None = None ,
1493
+ def tuple_schema (
1494
+ items_schema : list [ CoreSchema ] ,
1454
1495
* ,
1496
+ variadic_item_index : int | None = None ,
1455
1497
min_length : int | None = None ,
1456
1498
max_length : int | None = None ,
1457
1499
strict : bool | None = None ,
1458
1500
ref : str | None = None ,
1459
1501
metadata : Any = None ,
1460
1502
serialization : IncExSeqOrElseSerSchema | None = None ,
1461
- ) -> TupleVariableSchema :
1503
+ ) -> TupleSchema :
1462
1504
"""
1463
- Returns a schema that matches a tuple of a given schema , e.g.:
1505
+ Returns a schema that matches a tuple of schemas, with an optional variadic item , e.g.:
1464
1506
1465
1507
```py
1466
1508
from pydantic_core import SchemaValidator, core_schema
1467
1509
1468
- schema = core_schema.tuple_variable_schema(
1469
- items_schema=core_schema.int_schema(), min_length=0, max_length=10
1510
+ schema = core_schema.tuple_schema(
1511
+ [core_schema.int_schema(), core_schema.str_schema(), core_schema.float_schema()],
1512
+ variadic_item_index=1,
1470
1513
)
1471
1514
v = SchemaValidator(schema)
1472
- assert v.validate_python(('1', 2, 3 )) == (1, 2, 3 )
1515
+ assert v.validate_python((1, 'hello', 'world', 1.5 )) == (1, 'hello', 'world', 1.5 )
1473
1516
```
1474
1517
1475
1518
Args:
1476
- items_schema: The value must be a tuple with items that match this schema
1519
+ items_schema: The value must be a tuple with items that match these schemas
1520
+ variadic_item_index: The index of the schema in `items_schema` to be treated as variadic (following PEP 646)
1477
1521
min_length: The value must be a tuple with at least this many items
1478
1522
max_length: The value must be a tuple with at most this many items
1479
1523
strict: The value must be a tuple with exactly this many items
1480
- ref: optional unique identifier of the schema, used to reference the schema in other places
1524
+ ref: Optional unique identifier of the schema, used to reference the schema in other places
1481
1525
metadata: Any other information you want to include with the schema, not used by pydantic-core
1482
1526
serialization: Custom serialization schema
1483
1527
"""
1484
1528
return _dict_not_none (
1485
- type = 'tuple-variable ' ,
1529
+ type = 'tuple' ,
1486
1530
items_schema = items_schema ,
1531
+ variadic_item_index = variadic_item_index ,
1487
1532
min_length = min_length ,
1488
1533
max_length = max_length ,
1489
1534
strict = strict ,
@@ -3634,8 +3679,7 @@ def definition_reference_schema(
3634
3679
IsSubclassSchema ,
3635
3680
CallableSchema ,
3636
3681
ListSchema ,
3637
- TuplePositionalSchema ,
3638
- TupleVariableSchema ,
3682
+ TupleSchema ,
3639
3683
SetSchema ,
3640
3684
FrozenSetSchema ,
3641
3685
GeneratorSchema ,
@@ -3689,8 +3733,7 @@ def definition_reference_schema(
3689
3733
'is-subclass' ,
3690
3734
'callable' ,
3691
3735
'list' ,
3692
- 'tuple-positional' ,
3693
- 'tuple-variable' ,
3736
+ 'tuple' ,
3694
3737
'set' ,
3695
3738
'frozenset' ,
3696
3739
'generator' ,
0 commit comments