Skip to content

Commit

Permalink
Add a docstring to the tuple_schema function
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Aug 14, 2023
1 parent 5a6b42b commit 913687d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion python/pydantic_core/core_schema.py
Expand Up @@ -1456,7 +1456,7 @@ def tuple_variable_schema(
min_length: The value must be a tuple with at least this many items
max_length: The value must be a tuple with at most this many items
strict: The value must be a tuple with exactly this many items
ref: optional unique identifier of the schema, used to reference the schema in other places
ref: Optional unique identifier of the schema, used to reference the schema in other places
metadata: Any other information you want to include with the schema, not used by pydantic-core
serialization: Custom serialization schema
"""
Expand Down Expand Up @@ -1495,6 +1495,29 @@ def tuple_schema(
metadata: Any = None,
serialization: IncExSeqOrElseSerSchema | None = None,
) -> TuplePositionalSchema:
"""
Returns a schema that matches a tuple of schemas, with an optional variadic item, e.g.:
```py
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.tuple_schema(
[core_schema.int_schema(), core_schema.str_schema(), core_schema.float_schema()],
)
v = SchemaValidator(schema, variadic_item_index=1)
assert v.validate_python((1, 'hello', 'world', 1.5)) == (1, 'hello', 'world', 1.5)
```
Args:
items_schema: The value must be a tuple with items that match these schemas
variadic_item_index: The index of the schema in `items_schema` to be treated as variadic (following PEP 646)
min_length: The value must be a tuple with at least this many items
max_length: The value must be a tuple with at most this many items
strict: The value must be a tuple with exactly this many items
ref: Optional unique identifier of the schema, used to reference the schema in other places
metadata: Any other information you want to include with the schema, not used by pydantic-core
serialization: Custom serialization schema
"""
return _dict_not_none(
type='tuple',
items_schema=items_schema,
Expand Down

0 comments on commit 913687d

Please sign in to comment.