MCPcopy
hub / github.com/pydantic/pydantic / tuple_schema

Method tuple_schema

pydantic/json_schema.py:993–1026  ·  view source on GitHub ↗

Generates a JSON schema that matches a tuple schema e.g. `tuple[int, str, bool]` or `tuple[int, ...]`. Args: schema: The core schema. Returns: The generated JSON schema.

(self, schema: core_schema.TupleSchema)

Source from the content-addressed store, hash-verified

991 return self.tuple_schema(schema)
992
993 def tuple_schema(self, schema: core_schema.TupleSchema) -> JsonSchemaValue:
994 """Generates a JSON schema that matches a tuple schema e.g. `tuple[int,
995 str, bool]` or `tuple[int, ...]`.
996
997 Args:
998 schema: The core schema.
999
1000 Returns:
1001 The generated JSON schema.
1002 """
1003 json_schema: JsonSchemaValue = {'type': 'array'}
1004 if 'variadic_item_index' in schema:
1005 variadic_item_index = schema['variadic_item_index']
1006 if variadic_item_index > 0:
1007 json_schema['minItems'] = variadic_item_index
1008 json_schema['prefixItems'] = [
1009 self.generate_inner(item) for item in schema['items_schema'][:variadic_item_index]
1010 ]
1011 if variadic_item_index + 1 == len(schema['items_schema']):
1012 # if the variadic item is the last item, then represent it faithfully
1013 json_schema['items'] = self.generate_inner(schema['items_schema'][variadic_item_index])
1014 else:
1015 # otherwise, 'items' represents the schema for the variadic
1016 # item plus the suffix, so just allow anything for simplicity
1017 # for now
1018 json_schema['items'] = True
1019 else:
1020 prefixItems = [self.generate_inner(item) for item in schema['items_schema']]
1021 if prefixItems:
1022 json_schema['prefixItems'] = prefixItems
1023 json_schema['minItems'] = len(prefixItems)
1024 json_schema['maxItems'] = len(prefixItems)
1025 self.update_with_validations(json_schema, schema, self.ValidationsMapping.array)
1026 return json_schema
1027
1028 def set_schema(self, schema: core_schema.SetSchema) -> JsonSchemaValue:
1029 """Generates a JSON schema that matches a set schema.

Callers 15

tuple_variable_schemaMethod · 0.95
_tuple_schemaMethod · 0.80
test_tuple_positionalFunction · 0.80
test_outside_parentFunction · 0.80
multiple_tuple_schemaFunction · 0.80
test_definition_wrapFunction · 0.80
test_tuple_jsonFunction · 0.80
test_any_no_copyFunction · 0.80

Calls 2

generate_innerMethod · 0.95

Tested by 15

test_tuple_positionalFunction · 0.64
test_outside_parentFunction · 0.64
multiple_tuple_schemaFunction · 0.64
test_definition_wrapFunction · 0.64
test_tuple_jsonFunction · 0.64
test_any_no_copyFunction · 0.64