Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip update tuple schema #8527

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions pydantic/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import pydantic_core
from pydantic_core import CoreSchema, PydanticOmit, core_schema, to_jsonable_python
from pydantic_core.core_schema import ComputedField
from typing_extensions import Annotated, Literal, TypeAlias, assert_never
from typing_extensions import Annotated, Literal, TypeAlias, assert_never, deprecated, final

from ._internal import (
_config,
Expand Down Expand Up @@ -803,7 +803,9 @@ def list_schema(self, schema: core_schema.ListSchema) -> JsonSchemaValue:
self.update_with_validations(json_schema, schema, self.ValidationsMapping.array)
return json_schema

def tuple_positional_schema(self, schema: core_schema.TuplePositionalSchema) -> JsonSchemaValue:
@deprecated('tuple_positional_schema is deprecated. Use tuple_schema instead.')
@final
def tuple_positional_schema(self, schema: core_schema.TupleSchema) -> JsonSchemaValue:
"""Generates a JSON schema that matches a positional tuple schema e.g. `Tuple[int, str, bool]`.

Args:
Expand All @@ -812,19 +814,11 @@ def tuple_positional_schema(self, schema: core_schema.TuplePositionalSchema) ->
Returns:
The generated JSON schema.
"""
json_schema: JsonSchemaValue = {'type': 'array'}
json_schema['minItems'] = len(schema['items_schema'])
prefixItems = [self.generate_inner(item) for item in schema['items_schema']]
if prefixItems:
json_schema['prefixItems'] = prefixItems
if 'extras_schema' in schema:
json_schema['items'] = self.generate_inner(schema['extras_schema'])
else:
json_schema['maxItems'] = len(schema['items_schema'])
self.update_with_validations(json_schema, schema, self.ValidationsMapping.array)
return json_schema
return self.tuple_schema(schema)

def tuple_variable_schema(self, schema: core_schema.TupleVariableSchema) -> JsonSchemaValue:
@deprecated('tuple_variable_schema is deprecated. Use tuple_schema instead.')
@final
def tuple_variable_schema(self, schema: core_schema.TupleSchema) -> JsonSchemaValue:
"""Generates a JSON schema that matches a variable tuple schema e.g. `Tuple[int, ...]`.

Args:
Expand All @@ -833,8 +827,37 @@ def tuple_variable_schema(self, schema: core_schema.TupleVariableSchema) -> Json
Returns:
The generated JSON schema.
"""
items_schema = {} if 'items_schema' not in schema else self.generate_inner(schema['items_schema'])
json_schema = {'type': 'array', 'items': items_schema}
return self.tuple_schema(schema)

def tuple_schema(self, schema: core_schema.TupleSchema) -> JsonSchemaValue:
"""Generates a JSON schema that matches a tuple schema e.g. `Tuple[int, ...]`.

Args:
schema: The core schema.

Returns:
The generated JSON schema.
"""
json_schema: JsonSchemaValue = {'type': 'array'}
if 'variadic_item_index' in schema:
variadic_item_index = schema['variadic_item_index']
if variadic_item_index > 0:
json_schema['prefixItems'] = [
self.generate_inner(item) for item in schema['items_schema'][:variadic_item_index]
]
if variadic_item_index + 1 == len(schema['items_schema']):
# if the variadic item is the last item, then represent it faithfully
json_schema['items'] = self.generate_inner(schema['items_schema'][variadic_item_index])
else:
# otherwise, 'items' represents the schema for the variadic
# item plus the suffix, so just allow anything for simplicity
# for now
json_schema['items'] = True
else:
prefixItems = [self.generate_inner(item) for item in schema['items_schema']]
if prefixItems:
json_schema['prefixItems'] = prefixItems
json_schema['items'] = False
self.update_with_validations(json_schema, schema, self.ValidationsMapping.array)
return json_schema

Expand Down
Loading