MCPcopy
hub / github.com/pydantic/pydantic / _tuple_schema

Method _tuple_schema

pydantic/_internal/_generate_schema.py:1674–1702  ·  view source on GitHub ↗

Generate schema for a Tuple, e.g. `tuple[int, str]` or `tuple[int, ...]`.

(self, tuple_type: Any)

Source from the content-addressed store, hash-verified

1672 return parameter_schema
1673
1674 def _tuple_schema(self, tuple_type: Any) -> core_schema.CoreSchema:
1675 """Generate schema for a Tuple, e.g. `tuple[int, str]` or `tuple[int, ...]`."""
1676 # TODO: do we really need to resolve type vars here?
1677 typevars_map = get_standard_typevars_map(tuple_type)
1678 params = self._get_args_resolving_forward_refs(tuple_type)
1679
1680 if typevars_map and params:
1681 params = tuple(replace_types(param, typevars_map) for param in params)
1682
1683 # NOTE: subtle difference: `tuple[()]` gives `params=()`, whereas `typing.Tuple[()]` gives `params=((),)`
1684 # This is only true for <3.11, on Python 3.11+ `typing.Tuple[()]` gives `params=()`
1685 if not params:
1686 if tuple_type in TUPLE_TYPES:
1687 return core_schema.tuple_schema([core_schema.any_schema()], variadic_item_index=0)
1688 else:
1689 # special case for `tuple[()]` which means `tuple[]` - an empty tuple
1690 return core_schema.tuple_schema([])
1691 elif params[-1] is Ellipsis:
1692 if len(params) == 2:
1693 return core_schema.tuple_schema([self.generate_schema(params[0])], variadic_item_index=0)
1694 else:
1695 # TODO: something like https://github.com/pydantic/pydantic/issues/5952
1696 raise ValueError('Variable tuples can only have one type')
1697 elif len(params) == 1 and params[0] == ():
1698 # special case for `tuple[()]` which means `tuple[]` - an empty tuple
1699 # NOTE: This conditional can be removed when we drop support for Python 3.10.
1700 return core_schema.tuple_schema([])
1701 else:
1702 return core_schema.tuple_schema([self.generate_schema(param) for param in params])
1703
1704 def _type_schema(self) -> core_schema.CoreSchema:
1705 return core_schema.custom_error_schema(

Callers 2

match_typeMethod · 0.95
_match_generic_typeMethod · 0.95

Calls 6

generate_schemaMethod · 0.95
tuple_schemaMethod · 0.80
any_schemaMethod · 0.80
replace_typesFunction · 0.70

Tested by

no test coverage detected