_generate_schema.py 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867
  1. """Convert python types to pydantic-core schema."""
  2. from __future__ import annotations as _annotations
  3. import collections.abc
  4. import dataclasses
  5. import datetime
  6. import inspect
  7. import os
  8. import pathlib
  9. import re
  10. import sys
  11. import typing
  12. import warnings
  13. from collections.abc import Generator, Iterable, Iterator, Mapping
  14. from contextlib import contextmanager
  15. from copy import copy
  16. from decimal import Decimal
  17. from enum import Enum
  18. from fractions import Fraction
  19. from functools import partial
  20. from inspect import Parameter, _ParameterKind, signature
  21. from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
  22. from itertools import chain
  23. from operator import attrgetter
  24. from types import FunctionType, GenericAlias, LambdaType, MethodType
  25. from typing import (
  26. TYPE_CHECKING,
  27. Any,
  28. Callable,
  29. Final,
  30. ForwardRef,
  31. Literal,
  32. TypeVar,
  33. Union,
  34. cast,
  35. overload,
  36. )
  37. from uuid import UUID
  38. from zoneinfo import ZoneInfo
  39. import typing_extensions
  40. from pydantic_core import (
  41. MISSING,
  42. CoreSchema,
  43. MultiHostUrl,
  44. PydanticCustomError,
  45. PydanticSerializationUnexpectedValue,
  46. PydanticUndefined,
  47. Url,
  48. core_schema,
  49. to_jsonable_python,
  50. )
  51. from typing_extensions import TypeAlias, TypeAliasType, get_args, get_origin, is_typeddict
  52. from typing_inspection import typing_objects
  53. from typing_inspection.introspection import AnnotationSource, get_literal_values, is_union_origin
  54. from ..aliases import AliasChoices, AliasPath
  55. from ..annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler
  56. from ..config import ConfigDict, JsonDict, JsonEncoder, JsonSchemaExtraCallable
  57. from ..errors import PydanticSchemaGenerationError, PydanticUndefinedAnnotation, PydanticUserError
  58. from ..functional_validators import AfterValidator, BeforeValidator, FieldValidatorModes, PlainValidator, WrapValidator
  59. from ..json_schema import JsonSchemaValue
  60. from ..version import version_short
  61. from ..warnings import (
  62. ArbitraryTypeWarning,
  63. PydanticDeprecatedSince20,
  64. TypedDictExtraConfigWarning,
  65. UnsupportedFieldAttributeWarning,
  66. )
  67. from . import _decorators, _discriminated_union, _known_annotated_metadata, _repr, _typing_extra
  68. from ._config import ConfigWrapper, ConfigWrapperStack
  69. from ._core_metadata import CoreMetadata, update_core_metadata
  70. from ._core_utils import (
  71. get_ref,
  72. get_type_ref,
  73. is_list_like_schema_with_items_schema,
  74. )
  75. from ._decorators import (
  76. Decorator,
  77. DecoratorInfos,
  78. FieldSerializerDecoratorInfo,
  79. FieldValidatorDecoratorInfo,
  80. ModelSerializerDecoratorInfo,
  81. ModelValidatorDecoratorInfo,
  82. RootValidatorDecoratorInfo,
  83. ValidatorDecoratorInfo,
  84. get_attribute_from_bases,
  85. inspect_field_serializer,
  86. inspect_model_serializer,
  87. inspect_validator,
  88. )
  89. from ._docs_extraction import extract_docstrings_from_cls
  90. from ._fields import (
  91. collect_dataclass_fields,
  92. rebuild_dataclass_fields,
  93. rebuild_model_fields,
  94. takes_validated_data_argument,
  95. update_field_from_config,
  96. )
  97. from ._forward_ref import PydanticRecursiveRef
  98. from ._generics import get_standard_typevars_map, replace_types
  99. from ._import_utils import import_cached_base_model, import_cached_field_info
  100. from ._mock_val_ser import MockCoreSchema
  101. from ._namespace_utils import NamespacesTuple, NsResolver
  102. from ._schema_gather import MissingDefinitionError, gather_schemas_for_cleaning
  103. from ._schema_generation_shared import CallbackGetCoreSchemaHandler
  104. from ._utils import lenient_issubclass, smart_deepcopy
  105. if TYPE_CHECKING:
  106. from ..fields import ComputedFieldInfo, FieldInfo
  107. from ..main import BaseModel
  108. from ..types import Discriminator
  109. from ._dataclasses import StandardDataclass
  110. from ._schema_generation_shared import GetJsonSchemaFunction
  111. _SUPPORTS_TYPEDDICT = sys.version_info >= (3, 12)
  112. FieldDecoratorInfo = Union[ValidatorDecoratorInfo, FieldValidatorDecoratorInfo, FieldSerializerDecoratorInfo]
  113. FieldDecoratorInfoType = TypeVar('FieldDecoratorInfoType', bound=FieldDecoratorInfo)
  114. AnyFieldDecorator = Union[
  115. Decorator[ValidatorDecoratorInfo],
  116. Decorator[FieldValidatorDecoratorInfo],
  117. Decorator[FieldSerializerDecoratorInfo],
  118. ]
  119. ModifyCoreSchemaWrapHandler: TypeAlias = GetCoreSchemaHandler
  120. GetCoreSchemaFunction: TypeAlias = Callable[[Any, ModifyCoreSchemaWrapHandler], core_schema.CoreSchema]
  121. ParametersCallback: TypeAlias = "Callable[[int, str, Any], Literal['skip'] | None]"
  122. TUPLE_TYPES: list[type] = [typing.Tuple, tuple] # noqa: UP006
  123. LIST_TYPES: list[type] = [typing.List, list, collections.abc.MutableSequence] # noqa: UP006
  124. SET_TYPES: list[type] = [typing.Set, set, collections.abc.MutableSet] # noqa: UP006
  125. FROZEN_SET_TYPES: list[type] = [typing.FrozenSet, frozenset, collections.abc.Set] # noqa: UP006
  126. DICT_TYPES: list[type] = [typing.Dict, dict] # noqa: UP006
  127. IP_TYPES: list[type] = [IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network]
  128. SEQUENCE_TYPES: list[type] = [typing.Sequence, collections.abc.Sequence]
  129. ITERABLE_TYPES: list[type] = [typing.Iterable, collections.abc.Iterable, typing.Generator, collections.abc.Generator]
  130. TYPE_TYPES: list[type] = [typing.Type, type] # noqa: UP006
  131. PATTERN_TYPES: list[type] = [typing.Pattern, re.Pattern]
  132. PATH_TYPES: list[type] = [
  133. os.PathLike,
  134. pathlib.Path,
  135. pathlib.PurePath,
  136. pathlib.PosixPath,
  137. pathlib.PurePosixPath,
  138. pathlib.PureWindowsPath,
  139. ]
  140. MAPPING_TYPES = [
  141. typing.Mapping,
  142. typing.MutableMapping,
  143. collections.abc.Mapping,
  144. collections.abc.MutableMapping,
  145. collections.OrderedDict,
  146. typing_extensions.OrderedDict,
  147. typing.DefaultDict, # noqa: UP006
  148. collections.defaultdict,
  149. ]
  150. COUNTER_TYPES = [collections.Counter, typing.Counter]
  151. DEQUE_TYPES: list[type] = [collections.deque, typing.Deque] # noqa: UP006
  152. # Note: This does not play very well with type checkers. For example,
  153. # `a: LambdaType = lambda x: x` will raise a type error by Pyright.
  154. ValidateCallSupportedTypes = Union[
  155. LambdaType,
  156. FunctionType,
  157. MethodType,
  158. partial,
  159. ]
  160. VALIDATE_CALL_SUPPORTED_TYPES = get_args(ValidateCallSupportedTypes)
  161. UNSUPPORTED_STANDALONE_FIELDINFO_ATTRIBUTES: list[tuple[str, Any]] = [
  162. ('alias', None),
  163. ('validation_alias', None),
  164. ('serialization_alias', None),
  165. # will be set if any alias is set, so disable it to avoid double warnings:
  166. # 'alias_priority',
  167. ('default', PydanticUndefined),
  168. ('default_factory', None),
  169. ('exclude', None),
  170. ('deprecated', None),
  171. ('repr', True),
  172. ('validate_default', None),
  173. ('frozen', None),
  174. ('init', None),
  175. ('init_var', None),
  176. ('kw_only', None),
  177. ]
  178. """`FieldInfo` attributes (and their default value) that can't be used outside of a model (e.g. in a type adapter or a PEP 695 type alias)."""
  179. _mode_to_validator: dict[
  180. FieldValidatorModes, type[BeforeValidator | AfterValidator | PlainValidator | WrapValidator]
  181. ] = {'before': BeforeValidator, 'after': AfterValidator, 'plain': PlainValidator, 'wrap': WrapValidator}
  182. def check_validator_fields_against_field_name(
  183. info: FieldDecoratorInfo,
  184. field: str,
  185. ) -> bool:
  186. """Check if field name is in validator fields.
  187. Args:
  188. info: The field info.
  189. field: The field name to check.
  190. Returns:
  191. `True` if field name is in validator fields, `False` otherwise.
  192. """
  193. fields = info.fields
  194. return '*' in fields or field in fields
  195. def check_decorator_fields_exist(decorators: Iterable[AnyFieldDecorator], fields: Iterable[str]) -> None:
  196. """Check if the defined fields in decorators exist in `fields` param.
  197. It ignores the check for a decorator if the decorator has `*` as field or `check_fields=False`.
  198. Args:
  199. decorators: An iterable of decorators.
  200. fields: An iterable of fields name.
  201. Raises:
  202. PydanticUserError: If one of the field names does not exist in `fields` param.
  203. """
  204. fields = set(fields)
  205. for dec in decorators:
  206. if '*' in dec.info.fields:
  207. continue
  208. if dec.info.check_fields is False:
  209. continue
  210. for field in dec.info.fields:
  211. if field not in fields:
  212. raise PydanticUserError(
  213. f'Decorators defined with incorrect fields: {dec.cls_ref}.{dec.cls_var_name}'
  214. " (use check_fields=False if you're inheriting from the model and intended this)",
  215. code='decorator-missing-field',
  216. )
  217. def filter_field_decorator_info_by_field(
  218. validator_functions: Iterable[Decorator[FieldDecoratorInfoType]], field: str
  219. ) -> list[Decorator[FieldDecoratorInfoType]]:
  220. return [dec for dec in validator_functions if check_validator_fields_against_field_name(dec.info, field)]
  221. def apply_each_item_validators(
  222. schema: core_schema.CoreSchema,
  223. each_item_validators: list[Decorator[ValidatorDecoratorInfo]],
  224. ) -> core_schema.CoreSchema:
  225. # This V1 compatibility shim should eventually be removed
  226. # fail early if each_item_validators is empty
  227. if not each_item_validators:
  228. return schema
  229. # push down any `each_item=True` validators
  230. # note that this won't work for any Annotated types that get wrapped by a function validator
  231. # but that's okay because that didn't exist in V1
  232. if schema['type'] == 'nullable':
  233. schema['schema'] = apply_each_item_validators(schema['schema'], each_item_validators)
  234. return schema
  235. elif schema['type'] == 'tuple':
  236. if (variadic_item_index := schema.get('variadic_item_index')) is not None:
  237. schema['items_schema'][variadic_item_index] = apply_validators(
  238. schema['items_schema'][variadic_item_index],
  239. each_item_validators,
  240. )
  241. elif is_list_like_schema_with_items_schema(schema):
  242. inner_schema = schema.get('items_schema', core_schema.any_schema())
  243. schema['items_schema'] = apply_validators(inner_schema, each_item_validators)
  244. elif schema['type'] == 'dict':
  245. inner_schema = schema.get('values_schema', core_schema.any_schema())
  246. schema['values_schema'] = apply_validators(inner_schema, each_item_validators)
  247. else:
  248. raise TypeError(
  249. f'`@validator(..., each_item=True)` cannot be applied to fields with a schema of {schema["type"]}'
  250. )
  251. return schema
  252. def _extract_json_schema_info_from_field_info(
  253. info: FieldInfo | ComputedFieldInfo,
  254. ) -> tuple[JsonDict | None, JsonDict | JsonSchemaExtraCallable | None]:
  255. json_schema_updates = {
  256. 'title': info.title,
  257. 'description': info.description,
  258. 'deprecated': bool(info.deprecated) or info.deprecated == '' or None,
  259. 'examples': to_jsonable_python(info.examples),
  260. }
  261. json_schema_updates = {k: v for k, v in json_schema_updates.items() if v is not None}
  262. return (json_schema_updates or None, info.json_schema_extra)
  263. JsonEncoders = dict[type[Any], JsonEncoder]
  264. def _add_custom_serialization_from_json_encoders(
  265. json_encoders: JsonEncoders | None, tp: Any, schema: CoreSchema
  266. ) -> CoreSchema:
  267. """Iterate over the json_encoders and add the first matching encoder to the schema.
  268. Args:
  269. json_encoders: A dictionary of types and their encoder functions.
  270. tp: The type to check for a matching encoder.
  271. schema: The schema to add the encoder to.
  272. """
  273. if not json_encoders:
  274. return schema
  275. if 'serialization' in schema:
  276. return schema
  277. # Check the class type and its superclasses for a matching encoder
  278. # Decimal.__class__.__mro__ (and probably other cases) doesn't include Decimal itself
  279. # if the type is a GenericAlias (e.g. from list[int]) we need to use __class__ instead of .__mro__
  280. for base in (tp, *getattr(tp, '__mro__', tp.__class__.__mro__)[:-1]):
  281. encoder = json_encoders.get(base)
  282. if encoder is None:
  283. continue
  284. warnings.warn(
  285. f'`json_encoders` is deprecated. See https://docs.pydantic.dev/{version_short()}/concepts/serialization/#custom-serializers for alternatives',
  286. PydanticDeprecatedSince20,
  287. )
  288. # TODO: in theory we should check that the schema accepts a serialization key
  289. schema['serialization'] = core_schema.plain_serializer_function_ser_schema(encoder, when_used='json')
  290. return schema
  291. return schema
  292. class InvalidSchemaError(Exception):
  293. """The core schema is invalid."""
  294. class GenerateSchema:
  295. """Generate core schema for a Pydantic model, dataclass and types like `str`, `datetime`, ... ."""
  296. __slots__ = (
  297. '_config_wrapper_stack',
  298. '_ns_resolver',
  299. '_typevars_map',
  300. 'field_name_stack',
  301. 'model_type_stack',
  302. 'defs',
  303. )
  304. def __init__(
  305. self,
  306. config_wrapper: ConfigWrapper,
  307. ns_resolver: NsResolver | None = None,
  308. typevars_map: Mapping[TypeVar, Any] | None = None,
  309. ) -> None:
  310. # we need a stack for recursing into nested models
  311. self._config_wrapper_stack = ConfigWrapperStack(config_wrapper)
  312. self._ns_resolver = ns_resolver or NsResolver()
  313. self._typevars_map = typevars_map
  314. self.field_name_stack = _FieldNameStack()
  315. self.model_type_stack = _ModelTypeStack()
  316. self.defs = _Definitions()
  317. def __init_subclass__(cls) -> None:
  318. super().__init_subclass__()
  319. warnings.warn(
  320. 'Subclassing `GenerateSchema` is not supported. The API is highly subject to change in minor versions.',
  321. UserWarning,
  322. stacklevel=2,
  323. )
  324. @property
  325. def _config_wrapper(self) -> ConfigWrapper:
  326. return self._config_wrapper_stack.tail
  327. @property
  328. def _types_namespace(self) -> NamespacesTuple:
  329. return self._ns_resolver.types_namespace
  330. @property
  331. def _arbitrary_types(self) -> bool:
  332. return self._config_wrapper.arbitrary_types_allowed
  333. # the following methods can be overridden but should be considered
  334. # unstable / private APIs
  335. def _list_schema(self, items_type: Any) -> CoreSchema:
  336. return core_schema.list_schema(self.generate_schema(items_type))
  337. def _dict_schema(self, keys_type: Any, values_type: Any) -> CoreSchema:
  338. return core_schema.dict_schema(self.generate_schema(keys_type), self.generate_schema(values_type))
  339. def _set_schema(self, items_type: Any) -> CoreSchema:
  340. return core_schema.set_schema(self.generate_schema(items_type))
  341. def _frozenset_schema(self, items_type: Any) -> CoreSchema:
  342. return core_schema.frozenset_schema(self.generate_schema(items_type))
  343. def _enum_schema(self, enum_type: type[Enum]) -> CoreSchema:
  344. cases: list[Any] = list(enum_type.__members__.values())
  345. enum_ref = get_type_ref(enum_type)
  346. description = None if not enum_type.__doc__ else inspect.cleandoc(enum_type.__doc__)
  347. if (
  348. description == 'An enumeration.'
  349. ): # This is the default value provided by enum.EnumMeta.__new__; don't use it
  350. description = None
  351. js_updates = {'title': enum_type.__name__, 'description': description}
  352. js_updates = {k: v for k, v in js_updates.items() if v is not None}
  353. sub_type: Literal['str', 'int', 'float'] | None = None
  354. if issubclass(enum_type, int):
  355. sub_type = 'int'
  356. value_ser_type: core_schema.SerSchema = core_schema.simple_ser_schema('int')
  357. elif issubclass(enum_type, str):
  358. # this handles `StrEnum` (3.11 only), and also `Foobar(str, Enum)`
  359. sub_type = 'str'
  360. value_ser_type = core_schema.simple_ser_schema('str')
  361. elif issubclass(enum_type, float):
  362. sub_type = 'float'
  363. value_ser_type = core_schema.simple_ser_schema('float')
  364. else:
  365. # TODO this is an ugly hack, how do we trigger an Any schema for serialization?
  366. value_ser_type = core_schema.plain_serializer_function_ser_schema(lambda x: x)
  367. if cases:
  368. def get_json_schema(schema: CoreSchema, handler: GetJsonSchemaHandler) -> JsonSchemaValue:
  369. json_schema = handler(schema)
  370. original_schema = handler.resolve_ref_schema(json_schema)
  371. original_schema.update(js_updates)
  372. return json_schema
  373. # we don't want to add the missing to the schema if it's the default one
  374. default_missing = getattr(enum_type._missing_, '__func__', None) is Enum._missing_.__func__ # pyright: ignore[reportFunctionMemberAccess]
  375. enum_schema = core_schema.enum_schema(
  376. enum_type,
  377. cases,
  378. sub_type=sub_type,
  379. missing=None if default_missing else enum_type._missing_,
  380. ref=enum_ref,
  381. metadata={'pydantic_js_functions': [get_json_schema]},
  382. )
  383. if self._config_wrapper.use_enum_values:
  384. enum_schema = core_schema.no_info_after_validator_function(
  385. attrgetter('value'), enum_schema, serialization=value_ser_type
  386. )
  387. return enum_schema
  388. else:
  389. def get_json_schema_no_cases(_, handler: GetJsonSchemaHandler) -> JsonSchemaValue:
  390. json_schema = handler(core_schema.enum_schema(enum_type, cases, sub_type=sub_type, ref=enum_ref))
  391. original_schema = handler.resolve_ref_schema(json_schema)
  392. original_schema.update(js_updates)
  393. return json_schema
  394. # Use an isinstance check for enums with no cases.
  395. # The most important use case for this is creating TypeVar bounds for generics that should
  396. # be restricted to enums. This is more consistent than it might seem at first, since you can only
  397. # subclass enum.Enum (or subclasses of enum.Enum) if all parent classes have no cases.
  398. # We use the get_json_schema function when an Enum subclass has been declared with no cases
  399. # so that we can still generate a valid json schema.
  400. return core_schema.is_instance_schema(
  401. enum_type,
  402. metadata={'pydantic_js_functions': [get_json_schema_no_cases]},
  403. )
  404. def _ip_schema(self, tp: Any) -> CoreSchema:
  405. from ._validators import IP_VALIDATOR_LOOKUP, IpType
  406. ip_type_json_schema_format: dict[type[IpType], str] = {
  407. IPv4Address: 'ipv4',
  408. IPv4Network: 'ipv4network',
  409. IPv4Interface: 'ipv4interface',
  410. IPv6Address: 'ipv6',
  411. IPv6Network: 'ipv6network',
  412. IPv6Interface: 'ipv6interface',
  413. }
  414. def ser_ip(ip: Any, info: core_schema.SerializationInfo) -> str | IpType:
  415. if not isinstance(ip, (tp, str)):
  416. raise PydanticSerializationUnexpectedValue(
  417. f"Expected `{tp}` but got `{type(ip)}` with value `'{ip}'` - serialized value may not be as expected."
  418. )
  419. if info.mode == 'python':
  420. return ip
  421. return str(ip)
  422. return core_schema.lax_or_strict_schema(
  423. lax_schema=core_schema.no_info_plain_validator_function(IP_VALIDATOR_LOOKUP[tp]),
  424. strict_schema=core_schema.json_or_python_schema(
  425. json_schema=core_schema.no_info_after_validator_function(tp, core_schema.str_schema()),
  426. python_schema=core_schema.is_instance_schema(tp),
  427. ),
  428. serialization=core_schema.plain_serializer_function_ser_schema(ser_ip, info_arg=True, when_used='always'),
  429. metadata={
  430. 'pydantic_js_functions': [lambda _1, _2: {'type': 'string', 'format': ip_type_json_schema_format[tp]}]
  431. },
  432. )
  433. def _path_schema(self, tp: Any, path_type: Any) -> CoreSchema:
  434. if tp is os.PathLike and (path_type not in {str, bytes} and not typing_objects.is_any(path_type)):
  435. raise PydanticUserError(
  436. '`os.PathLike` can only be used with `str`, `bytes` or `Any`', code='schema-for-unknown-type'
  437. )
  438. path_constructor = pathlib.PurePath if tp is os.PathLike else tp
  439. strict_inner_schema = (
  440. core_schema.bytes_schema(strict=True) if (path_type is bytes) else core_schema.str_schema(strict=True)
  441. )
  442. lax_inner_schema = core_schema.bytes_schema() if (path_type is bytes) else core_schema.str_schema()
  443. def path_validator(input_value: str | bytes) -> os.PathLike[Any]: # type: ignore
  444. try:
  445. if path_type is bytes:
  446. if isinstance(input_value, bytes):
  447. try:
  448. input_value = input_value.decode()
  449. except UnicodeDecodeError as e:
  450. raise PydanticCustomError('bytes_type', 'Input must be valid bytes') from e
  451. else:
  452. raise PydanticCustomError('bytes_type', 'Input must be bytes')
  453. elif not isinstance(input_value, str):
  454. raise PydanticCustomError('path_type', 'Input is not a valid path')
  455. return path_constructor(input_value) # type: ignore
  456. except TypeError as e:
  457. raise PydanticCustomError('path_type', 'Input is not a valid path') from e
  458. def ser_path(path: Any, info: core_schema.SerializationInfo) -> str | os.PathLike[Any]:
  459. if not isinstance(path, (tp, str)):
  460. raise PydanticSerializationUnexpectedValue(
  461. f"Expected `{tp}` but got `{type(path)}` with value `'{path}'` - serialized value may not be as expected."
  462. )
  463. if info.mode == 'python':
  464. return path
  465. return str(path)
  466. instance_schema = core_schema.json_or_python_schema(
  467. json_schema=core_schema.no_info_after_validator_function(path_validator, lax_inner_schema),
  468. python_schema=core_schema.is_instance_schema(tp),
  469. )
  470. schema = core_schema.lax_or_strict_schema(
  471. lax_schema=core_schema.union_schema(
  472. [
  473. instance_schema,
  474. core_schema.no_info_after_validator_function(path_validator, strict_inner_schema),
  475. ],
  476. custom_error_type='path_type',
  477. custom_error_message=f'Input is not a valid path for {tp}',
  478. ),
  479. strict_schema=instance_schema,
  480. serialization=core_schema.plain_serializer_function_ser_schema(ser_path, info_arg=True, when_used='always'),
  481. metadata={'pydantic_js_functions': [lambda source, handler: {**handler(source), 'format': 'path'}]},
  482. )
  483. return schema
  484. def _deque_schema(self, items_type: Any) -> CoreSchema:
  485. from ._serializers import serialize_sequence_via_list
  486. from ._validators import deque_validator
  487. item_type_schema = self.generate_schema(items_type)
  488. # we have to use a lax list schema here, because we need to validate the deque's
  489. # items via a list schema, but it's ok if the deque itself is not a list
  490. list_schema = core_schema.list_schema(item_type_schema, strict=False)
  491. check_instance = core_schema.json_or_python_schema(
  492. json_schema=list_schema,
  493. python_schema=core_schema.is_instance_schema(collections.deque, cls_repr='Deque'),
  494. )
  495. lax_schema = core_schema.no_info_wrap_validator_function(deque_validator, list_schema)
  496. return core_schema.lax_or_strict_schema(
  497. lax_schema=lax_schema,
  498. strict_schema=core_schema.chain_schema([check_instance, lax_schema]),
  499. serialization=core_schema.wrap_serializer_function_ser_schema(
  500. serialize_sequence_via_list, schema=item_type_schema, info_arg=True
  501. ),
  502. )
  503. def _mapping_schema(self, tp: Any, keys_type: Any, values_type: Any) -> CoreSchema:
  504. from ._validators import MAPPING_ORIGIN_MAP, defaultdict_validator, get_defaultdict_default_default_factory
  505. mapped_origin = MAPPING_ORIGIN_MAP[tp]
  506. keys_schema = self.generate_schema(keys_type)
  507. with warnings.catch_warnings():
  508. # We kind of abused `Field()` default factories to be able to specify
  509. # the `defaultdict`'s `default_factory`. As a consequence, we get warnings
  510. # as normally `FieldInfo.default_factory` is unsupported in the context where
  511. # `Field()` is used and our only solution is to ignore them (note that this might
  512. # wrongfully ignore valid warnings, e.g. if the `value_type` is a PEP 695 type alias
  513. # with unsupported metadata).
  514. warnings.simplefilter('ignore', category=UnsupportedFieldAttributeWarning)
  515. values_schema = self.generate_schema(values_type)
  516. dict_schema = core_schema.dict_schema(keys_schema, values_schema, strict=False)
  517. if mapped_origin is dict:
  518. schema = dict_schema
  519. else:
  520. check_instance = core_schema.json_or_python_schema(
  521. json_schema=dict_schema,
  522. python_schema=core_schema.is_instance_schema(mapped_origin),
  523. )
  524. if tp is collections.defaultdict:
  525. default_default_factory = get_defaultdict_default_default_factory(values_type)
  526. coerce_instance_wrap = partial(
  527. core_schema.no_info_wrap_validator_function,
  528. partial(defaultdict_validator, default_default_factory=default_default_factory),
  529. )
  530. else:
  531. coerce_instance_wrap = partial(core_schema.no_info_after_validator_function, mapped_origin)
  532. lax_schema = coerce_instance_wrap(dict_schema)
  533. strict_schema = core_schema.chain_schema([check_instance, lax_schema])
  534. schema = core_schema.lax_or_strict_schema(
  535. lax_schema=lax_schema,
  536. strict_schema=strict_schema,
  537. serialization=core_schema.wrap_serializer_function_ser_schema(
  538. lambda v, h: h(v), schema=dict_schema, info_arg=False
  539. ),
  540. )
  541. return schema
  542. def _fraction_schema(self) -> CoreSchema:
  543. """Support for [`fractions.Fraction`][fractions.Fraction]."""
  544. from ._validators import fraction_validator
  545. # TODO: note, this is a fairly common pattern, re lax / strict for attempted type coercion,
  546. # can we use a helper function to reduce boilerplate?
  547. return core_schema.lax_or_strict_schema(
  548. lax_schema=core_schema.no_info_plain_validator_function(fraction_validator),
  549. strict_schema=core_schema.json_or_python_schema(
  550. json_schema=core_schema.no_info_plain_validator_function(fraction_validator),
  551. python_schema=core_schema.is_instance_schema(Fraction),
  552. ),
  553. # use str serialization to guarantee round trip behavior
  554. serialization=core_schema.to_string_ser_schema(when_used='always'),
  555. metadata={'pydantic_js_functions': [lambda _1, _2: {'type': 'string', 'format': 'fraction'}]},
  556. )
  557. def _arbitrary_type_schema(self, tp: Any) -> CoreSchema:
  558. if not isinstance(tp, type):
  559. warnings.warn(
  560. f'{tp!r} is not a Python type (it may be an instance of an object),'
  561. ' Pydantic will allow any object with no validation since we cannot even'
  562. ' enforce that the input is an instance of the given type.'
  563. ' To get rid of this error wrap the type with `pydantic.SkipValidation`.',
  564. ArbitraryTypeWarning,
  565. )
  566. return core_schema.any_schema()
  567. return core_schema.is_instance_schema(tp)
  568. def _unknown_type_schema(self, obj: Any) -> CoreSchema:
  569. raise PydanticSchemaGenerationError(
  570. f'Unable to generate pydantic-core schema for {obj!r}. '
  571. 'Set `arbitrary_types_allowed=True` in the model_config to ignore this error'
  572. ' or implement `__get_pydantic_core_schema__` on your type to fully support it.'
  573. '\n\nIf you got this error by calling handler(<some type>) within'
  574. ' `__get_pydantic_core_schema__` then you likely need to call'
  575. ' `handler.generate_schema(<some type>)` since we do not call'
  576. ' `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.'
  577. )
  578. def _apply_discriminator_to_union(
  579. self, schema: CoreSchema, discriminator: str | Discriminator | None
  580. ) -> CoreSchema:
  581. if discriminator is None:
  582. return schema
  583. try:
  584. return _discriminated_union.apply_discriminator(
  585. schema,
  586. discriminator,
  587. self.defs._definitions,
  588. )
  589. except _discriminated_union.MissingDefinitionForUnionRef:
  590. # defer until defs are resolved
  591. _discriminated_union.set_discriminator_in_metadata(
  592. schema,
  593. discriminator,
  594. )
  595. return schema
  596. def clean_schema(self, schema: CoreSchema) -> CoreSchema:
  597. return self.defs.finalize_schema(schema)
  598. def _add_js_function(self, metadata_schema: CoreSchema, js_function: Callable[..., Any]) -> None:
  599. metadata = metadata_schema.get('metadata', {})
  600. pydantic_js_functions = metadata.setdefault('pydantic_js_functions', [])
  601. # because of how we generate core schemas for nested generic models
  602. # we can end up adding `BaseModel.__get_pydantic_json_schema__` multiple times
  603. # this check may fail to catch duplicates if the function is a `functools.partial`
  604. # or something like that, but if it does it'll fail by inserting the duplicate
  605. if js_function not in pydantic_js_functions:
  606. pydantic_js_functions.append(js_function)
  607. metadata_schema['metadata'] = metadata
  608. def generate_schema(
  609. self,
  610. obj: Any,
  611. ) -> core_schema.CoreSchema:
  612. """Generate core schema.
  613. Args:
  614. obj: The object to generate core schema for.
  615. Returns:
  616. The generated core schema.
  617. Raises:
  618. PydanticUndefinedAnnotation:
  619. If it is not possible to evaluate forward reference.
  620. PydanticSchemaGenerationError:
  621. If it is not possible to generate pydantic-core schema.
  622. TypeError:
  623. - If `alias_generator` returns a disallowed type (must be str, AliasPath or AliasChoices).
  624. - If V1 style validator with `each_item=True` applied on a wrong field.
  625. PydanticUserError:
  626. - If `typing.TypedDict` is used instead of `typing_extensions.TypedDict` on Python < 3.12.
  627. - If `__modify_schema__` method is used instead of `__get_pydantic_json_schema__`.
  628. """
  629. schema = self._generate_schema_from_get_schema_method(obj, obj)
  630. if schema is None:
  631. schema = self._generate_schema_inner(obj)
  632. metadata_js_function = _extract_get_pydantic_json_schema(obj)
  633. if metadata_js_function is not None:
  634. metadata_schema = resolve_original_schema(schema, self.defs)
  635. if metadata_schema:
  636. self._add_js_function(metadata_schema, metadata_js_function)
  637. schema = _add_custom_serialization_from_json_encoders(self._config_wrapper.json_encoders, obj, schema)
  638. return schema
  639. def _model_schema(self, cls: type[BaseModel]) -> core_schema.CoreSchema:
  640. """Generate schema for a Pydantic model."""
  641. BaseModel_ = import_cached_base_model()
  642. with self.defs.get_schema_or_ref(cls) as (model_ref, maybe_schema):
  643. if maybe_schema is not None:
  644. return maybe_schema
  645. schema = cls.__dict__.get('__pydantic_core_schema__')
  646. if schema is not None and not isinstance(schema, MockCoreSchema):
  647. if schema['type'] == 'definitions':
  648. schema = self.defs.unpack_definitions(schema)
  649. ref = get_ref(schema)
  650. if ref:
  651. return self.defs.create_definition_reference_schema(schema)
  652. else:
  653. return schema
  654. config_wrapper = ConfigWrapper(cls.model_config, check=False)
  655. with self._config_wrapper_stack.push(config_wrapper), self._ns_resolver.push(cls):
  656. core_config = self._config_wrapper.core_config(title=cls.__name__)
  657. if cls.__pydantic_fields_complete__ or cls is BaseModel_:
  658. fields = getattr(cls, '__pydantic_fields__', {})
  659. else:
  660. if '__pydantic_fields__' not in cls.__dict__:
  661. # This happens when we have a loop in the schema generation:
  662. # class Base[T](BaseModel):
  663. # t: T
  664. #
  665. # class Other(BaseModel):
  666. # b: 'Base[Other]'
  667. # When we build fields for `Other`, we evaluate the forward annotation.
  668. # At this point, `Other` doesn't have the model fields set. We create
  669. # `Base[Other]`; model fields are successfully built, and we try to generate
  670. # a schema for `t: Other`. As `Other.__pydantic_fields__` aren't set, we abort.
  671. raise PydanticUndefinedAnnotation(
  672. name=cls.__name__,
  673. message=f'Class {cls.__name__!r} is not defined',
  674. )
  675. try:
  676. fields = rebuild_model_fields(
  677. cls,
  678. config_wrapper=self._config_wrapper,
  679. ns_resolver=self._ns_resolver,
  680. typevars_map=self._typevars_map or {},
  681. )
  682. except NameError as e:
  683. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  684. decorators = cls.__pydantic_decorators__
  685. computed_fields = decorators.computed_fields
  686. check_decorator_fields_exist(
  687. chain(
  688. decorators.field_validators.values(),
  689. decorators.field_serializers.values(),
  690. decorators.validators.values(),
  691. ),
  692. {*fields.keys(), *computed_fields.keys()},
  693. )
  694. model_validators = decorators.model_validators.values()
  695. extras_schema = None
  696. extras_keys_schema = None
  697. if core_config.get('extra_fields_behavior') == 'allow':
  698. assert cls.__mro__[0] is cls
  699. assert cls.__mro__[-1] is object
  700. for candidate_cls in cls.__mro__[:-1]:
  701. extras_annotation = getattr(candidate_cls, '__annotations__', {}).get(
  702. '__pydantic_extra__', None
  703. )
  704. if extras_annotation is not None:
  705. if isinstance(extras_annotation, str):
  706. extras_annotation = _typing_extra.eval_type_backport(
  707. _typing_extra._make_forward_ref(
  708. extras_annotation, is_argument=False, is_class=True
  709. ),
  710. *self._types_namespace,
  711. )
  712. tp = get_origin(extras_annotation)
  713. if tp not in DICT_TYPES:
  714. raise PydanticSchemaGenerationError(
  715. 'The type annotation for `__pydantic_extra__` must be `dict[str, ...]`'
  716. )
  717. extra_keys_type, extra_items_type = self._get_args_resolving_forward_refs(
  718. extras_annotation,
  719. required=True,
  720. )
  721. if extra_keys_type is not str:
  722. extras_keys_schema = self.generate_schema(extra_keys_type)
  723. if not typing_objects.is_any(extra_items_type):
  724. extras_schema = self.generate_schema(extra_items_type)
  725. if extras_keys_schema is not None or extras_schema is not None:
  726. break
  727. generic_origin: type[BaseModel] | None = getattr(cls, '__pydantic_generic_metadata__', {}).get('origin')
  728. if cls.__pydantic_root_model__:
  729. # FIXME: should the common field metadata be used here?
  730. inner_schema, _ = self._common_field_schema('root', fields['root'], decorators)
  731. inner_schema = apply_model_validators(inner_schema, model_validators, 'inner')
  732. model_schema = core_schema.model_schema(
  733. cls,
  734. inner_schema,
  735. generic_origin=generic_origin,
  736. custom_init=getattr(cls, '__pydantic_custom_init__', None),
  737. root_model=True,
  738. post_init=getattr(cls, '__pydantic_post_init__', None),
  739. config=core_config,
  740. ref=model_ref,
  741. )
  742. else:
  743. fields_schema: core_schema.CoreSchema = core_schema.model_fields_schema(
  744. {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
  745. computed_fields=[
  746. self._computed_field_schema(d, decorators.field_serializers)
  747. for d in computed_fields.values()
  748. ],
  749. extras_schema=extras_schema,
  750. extras_keys_schema=extras_keys_schema,
  751. model_name=cls.__name__,
  752. )
  753. inner_schema = apply_validators(fields_schema, decorators.root_validators.values())
  754. inner_schema = apply_model_validators(inner_schema, model_validators, 'inner')
  755. model_schema = core_schema.model_schema(
  756. cls,
  757. inner_schema,
  758. generic_origin=generic_origin,
  759. custom_init=getattr(cls, '__pydantic_custom_init__', None),
  760. root_model=False,
  761. post_init=getattr(cls, '__pydantic_post_init__', None),
  762. config=core_config,
  763. ref=model_ref,
  764. )
  765. schema = self._apply_model_serializers(model_schema, decorators.model_serializers.values())
  766. schema = apply_model_validators(schema, model_validators, 'outer')
  767. return self.defs.create_definition_reference_schema(schema)
  768. def _resolve_self_type(self, obj: Any) -> Any:
  769. obj = self.model_type_stack.get()
  770. if obj is None:
  771. raise PydanticUserError('`typing.Self` is invalid in this context', code='invalid-self-type')
  772. return obj
  773. def _generate_schema_from_get_schema_method(self, obj: Any, source: Any) -> core_schema.CoreSchema | None:
  774. BaseModel_ = import_cached_base_model()
  775. get_schema = getattr(obj, '__get_pydantic_core_schema__', None)
  776. is_base_model_get_schema = (
  777. getattr(get_schema, '__func__', None) is BaseModel_.__get_pydantic_core_schema__.__func__ # pyright: ignore[reportFunctionMemberAccess]
  778. )
  779. if (
  780. get_schema is not None
  781. # BaseModel.__get_pydantic_core_schema__ is defined for backwards compatibility,
  782. # to allow existing code to call `super().__get_pydantic_core_schema__` in Pydantic
  783. # model that overrides `__get_pydantic_core_schema__`. However, it raises a deprecation
  784. # warning stating that the method will be removed, and during the core schema gen we actually
  785. # don't call the method:
  786. and not is_base_model_get_schema
  787. ):
  788. # Some referenceable types might have a `__get_pydantic_core_schema__` method
  789. # defined on it by users (e.g. on a dataclass). This generally doesn't play well
  790. # as these types are already recognized by the `GenerateSchema` class and isn't ideal
  791. # as we might end up calling `get_schema_or_ref` (expensive) on types that are actually
  792. # not referenceable:
  793. with self.defs.get_schema_or_ref(obj) as (_, maybe_schema):
  794. if maybe_schema is not None:
  795. return maybe_schema
  796. if obj is source:
  797. ref_mode = 'unpack'
  798. else:
  799. ref_mode = 'to-def'
  800. schema = get_schema(
  801. source, CallbackGetCoreSchemaHandler(self._generate_schema_inner, self, ref_mode=ref_mode)
  802. )
  803. if schema['type'] == 'definitions':
  804. schema = self.defs.unpack_definitions(schema)
  805. ref = get_ref(schema)
  806. if ref:
  807. return self.defs.create_definition_reference_schema(schema)
  808. # Note: if schema is of type `'definition-ref'`, we might want to copy it as a
  809. # safety measure (because these are inlined in place -- i.e. mutated directly)
  810. return schema
  811. if get_schema is None and (validators := getattr(obj, '__get_validators__', None)) is not None:
  812. from pydantic.v1 import BaseModel as BaseModelV1
  813. if issubclass(obj, BaseModelV1):
  814. warnings.warn(
  815. f'Mixing V1 models and V2 models (or constructs, like `TypeAdapter`) is not supported. Please upgrade `{obj.__name__}` to V2.',
  816. UserWarning,
  817. )
  818. else:
  819. warnings.warn(
  820. '`__get_validators__` is deprecated and will be removed, use `__get_pydantic_core_schema__` instead.',
  821. PydanticDeprecatedSince20,
  822. )
  823. return core_schema.chain_schema([core_schema.with_info_plain_validator_function(v) for v in validators()])
  824. def _resolve_forward_ref(self, obj: Any) -> Any:
  825. # we assume that types_namespace has the target of forward references in its scope,
  826. # but this could fail, for example, if calling Validator on an imported type which contains
  827. # forward references to other types only defined in the module from which it was imported
  828. # `Validator(SomeImportedTypeAliasWithAForwardReference)`
  829. # or the equivalent for BaseModel
  830. # class Model(BaseModel):
  831. # x: SomeImportedTypeAliasWithAForwardReference
  832. try:
  833. obj = _typing_extra.eval_type_backport(obj, *self._types_namespace)
  834. except NameError as e:
  835. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  836. # if obj is still a ForwardRef, it means we can't evaluate it, raise PydanticUndefinedAnnotation
  837. if isinstance(obj, ForwardRef):
  838. raise PydanticUndefinedAnnotation(obj.__forward_arg__, f'Unable to evaluate forward reference {obj}')
  839. if self._typevars_map:
  840. obj = replace_types(obj, self._typevars_map)
  841. return obj
  842. @overload
  843. def _get_args_resolving_forward_refs(self, obj: Any, required: Literal[True]) -> tuple[Any, ...]: ...
  844. @overload
  845. def _get_args_resolving_forward_refs(self, obj: Any) -> tuple[Any, ...] | None: ...
  846. def _get_args_resolving_forward_refs(self, obj: Any, required: bool = False) -> tuple[Any, ...] | None:
  847. args = get_args(obj)
  848. if args:
  849. if isinstance(obj, GenericAlias):
  850. # PEP 585 generic aliases don't convert args to ForwardRefs, unlike `typing.List/Dict` etc.
  851. args = (_typing_extra._make_forward_ref(a) if isinstance(a, str) else a for a in args)
  852. args = tuple(self._resolve_forward_ref(a) if isinstance(a, ForwardRef) else a for a in args)
  853. elif required: # pragma: no cover
  854. raise TypeError(f'Expected {obj} to have generic parameters but it had none')
  855. return args
  856. def _get_first_arg_or_any(self, obj: Any) -> Any:
  857. args = self._get_args_resolving_forward_refs(obj)
  858. if not args:
  859. return Any
  860. return args[0]
  861. def _get_first_two_args_or_any(self, obj: Any) -> tuple[Any, Any]:
  862. args = self._get_args_resolving_forward_refs(obj)
  863. if not args:
  864. return (Any, Any)
  865. if len(args) < 2:
  866. origin = get_origin(obj)
  867. raise TypeError(f'Expected two type arguments for {origin}, got 1')
  868. return args[0], args[1]
  869. def _generate_schema_inner(self, obj: Any) -> core_schema.CoreSchema:
  870. if typing_objects.is_self(obj):
  871. obj = self._resolve_self_type(obj)
  872. if typing_objects.is_annotated(get_origin(obj)):
  873. return self._annotated_schema(obj)
  874. if isinstance(obj, dict):
  875. # we assume this is already a valid schema
  876. return obj # type: ignore[return-value]
  877. if isinstance(obj, str):
  878. obj = ForwardRef(obj)
  879. if isinstance(obj, ForwardRef):
  880. return self.generate_schema(self._resolve_forward_ref(obj))
  881. BaseModel = import_cached_base_model()
  882. if lenient_issubclass(obj, BaseModel):
  883. with self.model_type_stack.push(obj):
  884. return self._model_schema(obj)
  885. if isinstance(obj, PydanticRecursiveRef):
  886. return core_schema.definition_reference_schema(schema_ref=obj.type_ref)
  887. return self.match_type(obj)
  888. def match_type(self, obj: Any) -> core_schema.CoreSchema: # noqa: C901
  889. """Main mapping of types to schemas.
  890. The general structure is a series of if statements starting with the simple cases
  891. (non-generic primitive types) and then handling generics and other more complex cases.
  892. Each case either generates a schema directly, calls into a public user-overridable method
  893. (like `GenerateSchema.tuple_variable_schema`) or calls into a private method that handles some
  894. boilerplate before calling into the user-facing method (e.g. `GenerateSchema._tuple_schema`).
  895. The idea is that we'll evolve this into adding more and more user facing methods over time
  896. as they get requested and we figure out what the right API for them is.
  897. """
  898. if obj is str:
  899. return core_schema.str_schema()
  900. elif obj is bytes:
  901. return core_schema.bytes_schema()
  902. elif obj is int:
  903. return core_schema.int_schema()
  904. elif obj is float:
  905. return core_schema.float_schema()
  906. elif obj is bool:
  907. return core_schema.bool_schema()
  908. elif obj is complex:
  909. return core_schema.complex_schema()
  910. elif typing_objects.is_any(obj) or obj is object:
  911. return core_schema.any_schema()
  912. elif obj is datetime.date:
  913. return core_schema.date_schema()
  914. elif obj is datetime.datetime:
  915. return core_schema.datetime_schema()
  916. elif obj is datetime.time:
  917. return core_schema.time_schema()
  918. elif obj is datetime.timedelta:
  919. return core_schema.timedelta_schema()
  920. elif obj is Decimal:
  921. return core_schema.decimal_schema()
  922. elif obj is UUID:
  923. return core_schema.uuid_schema()
  924. elif obj is Url:
  925. return core_schema.url_schema()
  926. elif obj is Fraction:
  927. return self._fraction_schema()
  928. elif obj is MultiHostUrl:
  929. return core_schema.multi_host_url_schema()
  930. elif obj is None or obj is _typing_extra.NoneType:
  931. return core_schema.none_schema()
  932. if obj is MISSING:
  933. return core_schema.missing_sentinel_schema()
  934. elif obj in IP_TYPES:
  935. return self._ip_schema(obj)
  936. elif obj in TUPLE_TYPES:
  937. return self._tuple_schema(obj)
  938. elif obj in LIST_TYPES:
  939. return self._list_schema(Any)
  940. elif obj in SET_TYPES:
  941. return self._set_schema(Any)
  942. elif obj in FROZEN_SET_TYPES:
  943. return self._frozenset_schema(Any)
  944. elif obj in SEQUENCE_TYPES:
  945. return self._sequence_schema(Any)
  946. elif obj in ITERABLE_TYPES:
  947. return self._iterable_schema(obj)
  948. elif obj in DICT_TYPES:
  949. return self._dict_schema(Any, Any)
  950. elif obj in PATH_TYPES:
  951. return self._path_schema(obj, Any)
  952. elif obj in DEQUE_TYPES:
  953. return self._deque_schema(Any)
  954. elif obj in MAPPING_TYPES:
  955. return self._mapping_schema(obj, Any, Any)
  956. elif obj in COUNTER_TYPES:
  957. return self._mapping_schema(obj, Any, int)
  958. elif typing_objects.is_typealiastype(obj):
  959. return self._type_alias_type_schema(obj)
  960. elif obj is type:
  961. return self._type_schema()
  962. elif _typing_extra.is_callable(obj):
  963. return core_schema.callable_schema()
  964. elif typing_objects.is_literal(get_origin(obj)):
  965. return self._literal_schema(obj)
  966. elif is_typeddict(obj):
  967. return self._typed_dict_schema(obj, None)
  968. elif _typing_extra.is_namedtuple(obj):
  969. return self._namedtuple_schema(obj, None)
  970. elif typing_objects.is_newtype(obj):
  971. # NewType, can't use isinstance because it fails <3.10
  972. return self.generate_schema(obj.__supertype__)
  973. elif obj in PATTERN_TYPES:
  974. return self._pattern_schema(obj)
  975. elif _typing_extra.is_hashable(obj):
  976. return self._hashable_schema()
  977. elif isinstance(obj, typing.TypeVar):
  978. return self._unsubstituted_typevar_schema(obj)
  979. elif _typing_extra.is_finalvar(obj):
  980. if obj is Final:
  981. return core_schema.any_schema()
  982. return self.generate_schema(
  983. self._get_first_arg_or_any(obj),
  984. )
  985. elif isinstance(obj, VALIDATE_CALL_SUPPORTED_TYPES):
  986. return self._call_schema(obj)
  987. elif inspect.isclass(obj) and issubclass(obj, Enum):
  988. return self._enum_schema(obj)
  989. elif obj is ZoneInfo:
  990. return self._zoneinfo_schema()
  991. # dataclasses.is_dataclass coerces dc instances to types, but we only handle
  992. # the case of a dc type here
  993. if dataclasses.is_dataclass(obj):
  994. return self._dataclass_schema(obj, None) # pyright: ignore[reportArgumentType]
  995. origin = get_origin(obj)
  996. if origin is not None:
  997. return self._match_generic_type(obj, origin)
  998. if self._arbitrary_types:
  999. return self._arbitrary_type_schema(obj)
  1000. return self._unknown_type_schema(obj)
  1001. def _match_generic_type(self, obj: Any, origin: Any) -> CoreSchema: # noqa: C901
  1002. # Need to handle generic dataclasses before looking for the schema properties because attribute accesses
  1003. # on _GenericAlias delegate to the origin type, so lose the information about the concrete parametrization
  1004. # As a result, currently, there is no way to cache the schema for generic dataclasses. This may be possible
  1005. # to resolve by modifying the value returned by `Generic.__class_getitem__`, but that is a dangerous game.
  1006. if dataclasses.is_dataclass(origin):
  1007. return self._dataclass_schema(obj, origin) # pyright: ignore[reportArgumentType]
  1008. if _typing_extra.is_namedtuple(origin):
  1009. return self._namedtuple_schema(obj, origin)
  1010. schema = self._generate_schema_from_get_schema_method(origin, obj)
  1011. if schema is not None:
  1012. return schema
  1013. if typing_objects.is_typealiastype(origin):
  1014. return self._type_alias_type_schema(obj)
  1015. elif is_union_origin(origin):
  1016. return self._union_schema(obj)
  1017. elif origin in TUPLE_TYPES:
  1018. return self._tuple_schema(obj)
  1019. elif origin in LIST_TYPES:
  1020. return self._list_schema(self._get_first_arg_or_any(obj))
  1021. elif origin in SET_TYPES:
  1022. return self._set_schema(self._get_first_arg_or_any(obj))
  1023. elif origin in FROZEN_SET_TYPES:
  1024. return self._frozenset_schema(self._get_first_arg_or_any(obj))
  1025. elif origin in DICT_TYPES:
  1026. return self._dict_schema(*self._get_first_two_args_or_any(obj))
  1027. elif origin in PATH_TYPES:
  1028. return self._path_schema(origin, self._get_first_arg_or_any(obj))
  1029. elif origin in DEQUE_TYPES:
  1030. return self._deque_schema(self._get_first_arg_or_any(obj))
  1031. elif origin in MAPPING_TYPES:
  1032. return self._mapping_schema(origin, *self._get_first_two_args_or_any(obj))
  1033. elif origin in COUNTER_TYPES:
  1034. return self._mapping_schema(origin, self._get_first_arg_or_any(obj), int)
  1035. elif is_typeddict(origin):
  1036. return self._typed_dict_schema(obj, origin)
  1037. elif origin in TYPE_TYPES:
  1038. return self._subclass_schema(obj)
  1039. elif origin in SEQUENCE_TYPES:
  1040. return self._sequence_schema(self._get_first_arg_or_any(obj))
  1041. elif origin in ITERABLE_TYPES:
  1042. return self._iterable_schema(obj)
  1043. elif origin in PATTERN_TYPES:
  1044. return self._pattern_schema(obj)
  1045. if self._arbitrary_types:
  1046. return self._arbitrary_type_schema(origin)
  1047. return self._unknown_type_schema(obj)
  1048. def _generate_td_field_schema(
  1049. self,
  1050. name: str,
  1051. field_info: FieldInfo,
  1052. decorators: DecoratorInfos,
  1053. *,
  1054. required: bool = True,
  1055. ) -> core_schema.TypedDictField:
  1056. """Prepare a TypedDictField to represent a model or typeddict field."""
  1057. schema, metadata = self._common_field_schema(name, field_info, decorators)
  1058. return core_schema.typed_dict_field(
  1059. schema,
  1060. required=False if not field_info.is_required() else required,
  1061. serialization_exclude=field_info.exclude,
  1062. validation_alias=_convert_to_aliases(field_info.validation_alias),
  1063. serialization_alias=field_info.serialization_alias,
  1064. serialization_exclude_if=field_info.exclude_if,
  1065. metadata=metadata,
  1066. )
  1067. def _generate_md_field_schema(
  1068. self,
  1069. name: str,
  1070. field_info: FieldInfo,
  1071. decorators: DecoratorInfos,
  1072. ) -> core_schema.ModelField:
  1073. """Prepare a ModelField to represent a model field."""
  1074. schema, metadata = self._common_field_schema(name, field_info, decorators)
  1075. return core_schema.model_field(
  1076. schema,
  1077. serialization_exclude=field_info.exclude,
  1078. validation_alias=_convert_to_aliases(field_info.validation_alias),
  1079. serialization_alias=field_info.serialization_alias,
  1080. serialization_exclude_if=field_info.exclude_if,
  1081. frozen=field_info.frozen,
  1082. metadata=metadata,
  1083. )
  1084. def _generate_dc_field_schema(
  1085. self,
  1086. name: str,
  1087. field_info: FieldInfo,
  1088. decorators: DecoratorInfos,
  1089. ) -> core_schema.DataclassField:
  1090. """Prepare a DataclassField to represent the parameter/field, of a dataclass."""
  1091. schema, metadata = self._common_field_schema(name, field_info, decorators)
  1092. return core_schema.dataclass_field(
  1093. name,
  1094. schema,
  1095. init=field_info.init,
  1096. init_only=field_info.init_var or None,
  1097. kw_only=None if field_info.kw_only else False,
  1098. serialization_exclude=field_info.exclude,
  1099. validation_alias=_convert_to_aliases(field_info.validation_alias),
  1100. serialization_alias=field_info.serialization_alias,
  1101. serialization_exclude_if=field_info.exclude_if,
  1102. frozen=field_info.frozen,
  1103. metadata=metadata,
  1104. )
  1105. def _common_field_schema( # C901
  1106. self, name: str, field_info: FieldInfo, decorators: DecoratorInfos
  1107. ) -> tuple[CoreSchema, dict[str, Any]]:
  1108. source_type, annotations = field_info.annotation, field_info.metadata
  1109. def set_discriminator(schema: CoreSchema) -> CoreSchema:
  1110. schema = self._apply_discriminator_to_union(schema, field_info.discriminator)
  1111. return schema
  1112. # Convert `@field_validator` decorators to `Before/After/Plain/WrapValidator` instances:
  1113. validators_from_decorators = [
  1114. _mode_to_validator[decorator.info.mode]._from_decorator(decorator)
  1115. for decorator in filter_field_decorator_info_by_field(decorators.field_validators.values(), name)
  1116. ]
  1117. with self.field_name_stack.push(name):
  1118. if field_info.discriminator is not None:
  1119. schema = self._apply_annotations(
  1120. source_type, annotations + validators_from_decorators, transform_inner_schema=set_discriminator
  1121. )
  1122. else:
  1123. schema = self._apply_annotations(
  1124. source_type,
  1125. annotations + validators_from_decorators,
  1126. )
  1127. # This V1 compatibility shim should eventually be removed
  1128. # push down any `each_item=True` validators
  1129. # note that this won't work for any Annotated types that get wrapped by a function validator
  1130. # but that's okay because that didn't exist in V1
  1131. this_field_validators = filter_field_decorator_info_by_field(decorators.validators.values(), name)
  1132. if _validators_require_validate_default(this_field_validators):
  1133. field_info.validate_default = True
  1134. each_item_validators = [v for v in this_field_validators if v.info.each_item is True]
  1135. this_field_validators = [v for v in this_field_validators if v not in each_item_validators]
  1136. schema = apply_each_item_validators(schema, each_item_validators)
  1137. schema = apply_validators(schema, this_field_validators)
  1138. # the default validator needs to go outside of any other validators
  1139. # so that it is the topmost validator for the field validator
  1140. # which uses it to check if the field has a default value or not
  1141. if not field_info.is_required():
  1142. schema = wrap_default(field_info, schema)
  1143. schema = self._apply_field_serializers(
  1144. schema, filter_field_decorator_info_by_field(decorators.field_serializers.values(), name)
  1145. )
  1146. pydantic_js_updates, pydantic_js_extra = _extract_json_schema_info_from_field_info(field_info)
  1147. core_metadata: dict[str, Any] = {}
  1148. update_core_metadata(
  1149. core_metadata, pydantic_js_updates=pydantic_js_updates, pydantic_js_extra=pydantic_js_extra
  1150. )
  1151. return schema, core_metadata
  1152. def _union_schema(self, union_type: Any) -> core_schema.CoreSchema:
  1153. """Generate schema for a Union."""
  1154. args = self._get_args_resolving_forward_refs(union_type, required=True)
  1155. choices: list[CoreSchema] = []
  1156. nullable = False
  1157. for arg in args:
  1158. if arg is None or arg is _typing_extra.NoneType:
  1159. nullable = True
  1160. else:
  1161. choices.append(self.generate_schema(arg))
  1162. if len(choices) == 1:
  1163. s = choices[0]
  1164. else:
  1165. choices_with_tags: list[CoreSchema | tuple[CoreSchema, str]] = []
  1166. for choice in choices:
  1167. tag = cast(CoreMetadata, choice.get('metadata', {})).get('pydantic_internal_union_tag_key')
  1168. if tag is not None:
  1169. choices_with_tags.append((choice, tag))
  1170. else:
  1171. choices_with_tags.append(choice)
  1172. s = core_schema.union_schema(choices_with_tags)
  1173. if nullable:
  1174. s = core_schema.nullable_schema(s)
  1175. return s
  1176. def _type_alias_type_schema(self, obj: TypeAliasType) -> CoreSchema:
  1177. with self.defs.get_schema_or_ref(obj) as (ref, maybe_schema):
  1178. if maybe_schema is not None:
  1179. return maybe_schema
  1180. origin: TypeAliasType = get_origin(obj) or obj
  1181. typevars_map = get_standard_typevars_map(obj)
  1182. with self._ns_resolver.push(origin):
  1183. try:
  1184. annotation = _typing_extra.eval_type(origin.__value__, *self._types_namespace)
  1185. except NameError as e:
  1186. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  1187. annotation = replace_types(annotation, typevars_map)
  1188. schema = self.generate_schema(annotation)
  1189. assert schema['type'] != 'definitions'
  1190. schema['ref'] = ref # type: ignore
  1191. return self.defs.create_definition_reference_schema(schema)
  1192. def _literal_schema(self, literal_type: Any) -> CoreSchema:
  1193. """Generate schema for a Literal."""
  1194. expected = list(get_literal_values(literal_type, type_check=False, unpack_type_aliases='eager'))
  1195. assert expected, f'literal "expected" cannot be empty, obj={literal_type}'
  1196. schema = core_schema.literal_schema(expected)
  1197. if self._config_wrapper.use_enum_values and any(isinstance(v, Enum) for v in expected):
  1198. schema = core_schema.no_info_after_validator_function(
  1199. lambda v: v.value if isinstance(v, Enum) else v, schema
  1200. )
  1201. return schema
  1202. def _typed_dict_schema(self, typed_dict_cls: Any, origin: Any) -> core_schema.CoreSchema:
  1203. """Generate a core schema for a `TypedDict` class.
  1204. To be able to build a `DecoratorInfos` instance for the `TypedDict` class (which will include
  1205. validators, serializers, etc.), we need to have access to the original bases of the class
  1206. (see https://docs.python.org/3/library/types.html#types.get_original_bases).
  1207. However, the `__orig_bases__` attribute was only added in 3.12 (https://github.com/python/cpython/pull/103698).
  1208. For this reason, we require Python 3.12 (or using the `typing_extensions` backport).
  1209. """
  1210. FieldInfo = import_cached_field_info()
  1211. with (
  1212. self.model_type_stack.push(typed_dict_cls),
  1213. self.defs.get_schema_or_ref(typed_dict_cls) as (
  1214. typed_dict_ref,
  1215. maybe_schema,
  1216. ),
  1217. ):
  1218. if maybe_schema is not None:
  1219. return maybe_schema
  1220. typevars_map = get_standard_typevars_map(typed_dict_cls)
  1221. if origin is not None:
  1222. typed_dict_cls = origin
  1223. if not _SUPPORTS_TYPEDDICT and type(typed_dict_cls).__module__ == 'typing':
  1224. raise PydanticUserError(
  1225. 'Please use `typing_extensions.TypedDict` instead of `typing.TypedDict` on Python < 3.12.',
  1226. code='typed-dict-version',
  1227. )
  1228. try:
  1229. # if a typed dictionary class doesn't have config, we use the parent's config, hence a default of `None`
  1230. # see https://github.com/pydantic/pydantic/issues/10917
  1231. config: ConfigDict | None = get_attribute_from_bases(typed_dict_cls, '__pydantic_config__')
  1232. except AttributeError:
  1233. config = None
  1234. with self._config_wrapper_stack.push(config):
  1235. core_config = self._config_wrapper.core_config(title=typed_dict_cls.__name__)
  1236. required_keys: frozenset[str] = typed_dict_cls.__required_keys__
  1237. fields: dict[str, core_schema.TypedDictField] = {}
  1238. decorators = DecoratorInfos.build(typed_dict_cls)
  1239. decorators.update_from_config(self._config_wrapper)
  1240. if self._config_wrapper.use_attribute_docstrings:
  1241. field_docstrings = extract_docstrings_from_cls(typed_dict_cls, use_inspect=True)
  1242. else:
  1243. field_docstrings = None
  1244. try:
  1245. annotations = _typing_extra.get_cls_type_hints(typed_dict_cls, ns_resolver=self._ns_resolver)
  1246. except NameError as e:
  1247. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  1248. readonly_fields: list[str] = []
  1249. for field_name, annotation in annotations.items():
  1250. field_info = FieldInfo.from_annotation(annotation, _source=AnnotationSource.TYPED_DICT)
  1251. field_info.annotation = replace_types(field_info.annotation, typevars_map)
  1252. required = (
  1253. field_name in required_keys or 'required' in field_info._qualifiers
  1254. ) and 'not_required' not in field_info._qualifiers
  1255. if 'read_only' in field_info._qualifiers:
  1256. readonly_fields.append(field_name)
  1257. if (
  1258. field_docstrings is not None
  1259. and field_info.description is None
  1260. and field_name in field_docstrings
  1261. ):
  1262. field_info.description = field_docstrings[field_name]
  1263. update_field_from_config(self._config_wrapper, field_name, field_info)
  1264. fields[field_name] = self._generate_td_field_schema(
  1265. field_name, field_info, decorators, required=required
  1266. )
  1267. if readonly_fields:
  1268. fields_repr = ', '.join(repr(f) for f in readonly_fields)
  1269. plural = len(readonly_fields) >= 2
  1270. warnings.warn(
  1271. f'Item{"s" if plural else ""} {fields_repr} on TypedDict class {typed_dict_cls.__name__!r} '
  1272. f'{"are" if plural else "is"} using the `ReadOnly` qualifier. Pydantic will not protect items '
  1273. 'from any mutation on dictionary instances.',
  1274. UserWarning,
  1275. )
  1276. extra_behavior: core_schema.ExtraBehavior = 'ignore'
  1277. extras_schema: CoreSchema | None = None # For 'allow', equivalent to `Any` - no validation performed.
  1278. # `__closed__` is `None` when not specified (equivalent to `False`):
  1279. is_closed = bool(getattr(typed_dict_cls, '__closed__', False))
  1280. extra_items = getattr(typed_dict_cls, '__extra_items__', typing_extensions.NoExtraItems)
  1281. if is_closed:
  1282. extra_behavior = 'forbid'
  1283. extras_schema = None
  1284. elif not typing_objects.is_noextraitems(extra_items):
  1285. extra_behavior = 'allow'
  1286. extras_schema = self.generate_schema(replace_types(extra_items, typevars_map))
  1287. if (config_extra := self._config_wrapper.extra) in ('allow', 'forbid'):
  1288. if is_closed and config_extra == 'allow':
  1289. warnings.warn(
  1290. f"TypedDict class {typed_dict_cls.__qualname__!r} is closed, but 'extra' configuration "
  1291. "is set to `'allow'`. The 'extra' configuration value will be ignored.",
  1292. category=TypedDictExtraConfigWarning,
  1293. )
  1294. elif not typing_objects.is_noextraitems(extra_items) and config_extra == 'forbid':
  1295. warnings.warn(
  1296. f"TypedDict class {typed_dict_cls.__qualname__!r} allows extra items, but 'extra' configuration "
  1297. "is set to `'forbid'`. The 'extra' configuration value will be ignored.",
  1298. category=TypedDictExtraConfigWarning,
  1299. )
  1300. else:
  1301. extra_behavior = config_extra
  1302. td_schema = core_schema.typed_dict_schema(
  1303. fields,
  1304. cls=typed_dict_cls,
  1305. computed_fields=[
  1306. self._computed_field_schema(d, decorators.field_serializers)
  1307. for d in decorators.computed_fields.values()
  1308. ],
  1309. extra_behavior=extra_behavior,
  1310. extras_schema=extras_schema,
  1311. ref=typed_dict_ref,
  1312. config=core_config,
  1313. )
  1314. schema = self._apply_model_serializers(td_schema, decorators.model_serializers.values())
  1315. schema = apply_model_validators(schema, decorators.model_validators.values(), 'all')
  1316. return self.defs.create_definition_reference_schema(schema)
  1317. def _namedtuple_schema(self, namedtuple_cls: Any, origin: Any) -> core_schema.CoreSchema:
  1318. """Generate schema for a NamedTuple."""
  1319. with (
  1320. self.model_type_stack.push(namedtuple_cls),
  1321. self.defs.get_schema_or_ref(namedtuple_cls) as (
  1322. namedtuple_ref,
  1323. maybe_schema,
  1324. ),
  1325. ):
  1326. if maybe_schema is not None:
  1327. return maybe_schema
  1328. typevars_map = get_standard_typevars_map(namedtuple_cls)
  1329. if origin is not None:
  1330. namedtuple_cls = origin
  1331. try:
  1332. annotations = _typing_extra.get_cls_type_hints(namedtuple_cls, ns_resolver=self._ns_resolver)
  1333. except NameError as e:
  1334. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  1335. if not annotations:
  1336. # annotations is empty, happens if namedtuple_cls defined via collections.namedtuple(...)
  1337. annotations: dict[str, Any] = dict.fromkeys(namedtuple_cls._fields, Any)
  1338. if typevars_map:
  1339. annotations = {
  1340. field_name: replace_types(annotation, typevars_map)
  1341. for field_name, annotation in annotations.items()
  1342. }
  1343. arguments_schema = core_schema.arguments_schema(
  1344. [
  1345. self._generate_parameter_schema(
  1346. field_name,
  1347. annotation,
  1348. source=AnnotationSource.NAMED_TUPLE,
  1349. default=namedtuple_cls._field_defaults.get(field_name, Parameter.empty),
  1350. )
  1351. for field_name, annotation in annotations.items()
  1352. ],
  1353. metadata={'pydantic_js_prefer_positional_arguments': True},
  1354. )
  1355. schema = core_schema.call_schema(arguments_schema, namedtuple_cls, ref=namedtuple_ref)
  1356. return self.defs.create_definition_reference_schema(schema)
  1357. def _generate_parameter_schema(
  1358. self,
  1359. name: str,
  1360. annotation: type[Any],
  1361. source: AnnotationSource,
  1362. default: Any = Parameter.empty,
  1363. mode: Literal['positional_only', 'positional_or_keyword', 'keyword_only'] | None = None,
  1364. ) -> core_schema.ArgumentsParameter:
  1365. """Generate the definition of a field in a namedtuple or a parameter in a function signature.
  1366. This definition is meant to be used for the `'arguments'` core schema, which will be replaced
  1367. in V3 by the `'arguments-v3`'.
  1368. """
  1369. FieldInfo = import_cached_field_info()
  1370. if default is Parameter.empty:
  1371. field = FieldInfo.from_annotation(annotation, _source=source)
  1372. else:
  1373. field = FieldInfo.from_annotated_attribute(annotation, default, _source=source)
  1374. assert field.annotation is not None, 'field.annotation should not be None when generating a schema'
  1375. update_field_from_config(self._config_wrapper, name, field)
  1376. with self.field_name_stack.push(name):
  1377. schema = self._apply_annotations(
  1378. field.annotation,
  1379. [field],
  1380. # Because we pass `field` as metadata above (required for attributes relevant for
  1381. # JSON Scheme generation), we need to ignore the potential warnings about `FieldInfo`
  1382. # attributes that will not be used:
  1383. check_unsupported_field_info_attributes=False,
  1384. )
  1385. if not field.is_required():
  1386. schema = wrap_default(field, schema)
  1387. parameter_schema = core_schema.arguments_parameter(
  1388. name,
  1389. schema,
  1390. mode=mode,
  1391. alias=_convert_to_aliases(field.validation_alias),
  1392. )
  1393. return parameter_schema
  1394. def _generate_parameter_v3_schema(
  1395. self,
  1396. name: str,
  1397. annotation: Any,
  1398. source: AnnotationSource,
  1399. mode: Literal[
  1400. 'positional_only',
  1401. 'positional_or_keyword',
  1402. 'keyword_only',
  1403. 'var_args',
  1404. 'var_kwargs_uniform',
  1405. 'var_kwargs_unpacked_typed_dict',
  1406. ],
  1407. default: Any = Parameter.empty,
  1408. ) -> core_schema.ArgumentsV3Parameter:
  1409. """Generate the definition of a parameter in a function signature.
  1410. This definition is meant to be used for the `'arguments-v3'` core schema, which will replace
  1411. the `'arguments`' schema in V3.
  1412. """
  1413. FieldInfo = import_cached_field_info()
  1414. if default is Parameter.empty:
  1415. field = FieldInfo.from_annotation(annotation, _source=source)
  1416. else:
  1417. field = FieldInfo.from_annotated_attribute(annotation, default, _source=source)
  1418. update_field_from_config(self._config_wrapper, name, field)
  1419. with self.field_name_stack.push(name):
  1420. schema = self._apply_annotations(
  1421. field.annotation,
  1422. [field],
  1423. # Because we pass `field` as metadata above (required for attributes relevant for
  1424. # JSON Scheme generation), we need to ignore the potential warnings about `FieldInfo`
  1425. # attributes that will not be used:
  1426. check_unsupported_field_info_attributes=False,
  1427. )
  1428. if not field.is_required():
  1429. schema = wrap_default(field, schema)
  1430. parameter_schema = core_schema.arguments_v3_parameter(
  1431. name=name,
  1432. schema=schema,
  1433. mode=mode,
  1434. alias=_convert_to_aliases(field.validation_alias),
  1435. )
  1436. return parameter_schema
  1437. def _tuple_schema(self, tuple_type: Any) -> core_schema.CoreSchema:
  1438. """Generate schema for a Tuple, e.g. `tuple[int, str]` or `tuple[int, ...]`."""
  1439. # TODO: do we really need to resolve type vars here?
  1440. typevars_map = get_standard_typevars_map(tuple_type)
  1441. params = self._get_args_resolving_forward_refs(tuple_type)
  1442. if typevars_map and params:
  1443. params = tuple(replace_types(param, typevars_map) for param in params)
  1444. # NOTE: subtle difference: `tuple[()]` gives `params=()`, whereas `typing.Tuple[()]` gives `params=((),)`
  1445. # This is only true for <3.11, on Python 3.11+ `typing.Tuple[()]` gives `params=()`
  1446. if not params:
  1447. if tuple_type in TUPLE_TYPES:
  1448. return core_schema.tuple_schema([core_schema.any_schema()], variadic_item_index=0)
  1449. else:
  1450. # special case for `tuple[()]` which means `tuple[]` - an empty tuple
  1451. return core_schema.tuple_schema([])
  1452. elif params[-1] is Ellipsis:
  1453. if len(params) == 2:
  1454. return core_schema.tuple_schema([self.generate_schema(params[0])], variadic_item_index=0)
  1455. else:
  1456. # TODO: something like https://github.com/pydantic/pydantic/issues/5952
  1457. raise ValueError('Variable tuples can only have one type')
  1458. elif len(params) == 1 and params[0] == ():
  1459. # special case for `tuple[()]` which means `tuple[]` - an empty tuple
  1460. # NOTE: This conditional can be removed when we drop support for Python 3.10.
  1461. return core_schema.tuple_schema([])
  1462. else:
  1463. return core_schema.tuple_schema([self.generate_schema(param) for param in params])
  1464. def _type_schema(self) -> core_schema.CoreSchema:
  1465. return core_schema.custom_error_schema(
  1466. core_schema.is_instance_schema(type),
  1467. custom_error_type='is_type',
  1468. custom_error_message='Input should be a type',
  1469. )
  1470. def _zoneinfo_schema(self) -> core_schema.CoreSchema:
  1471. """Generate schema for a zone_info.ZoneInfo object"""
  1472. from ._validators import validate_str_is_valid_iana_tz
  1473. metadata = {'pydantic_js_functions': [lambda _1, _2: {'type': 'string', 'format': 'zoneinfo'}]}
  1474. return core_schema.no_info_plain_validator_function(
  1475. validate_str_is_valid_iana_tz,
  1476. serialization=core_schema.to_string_ser_schema(),
  1477. metadata=metadata,
  1478. )
  1479. def _union_is_subclass_schema(self, union_type: Any) -> core_schema.CoreSchema:
  1480. """Generate schema for `type[Union[X, ...]]`."""
  1481. args = self._get_args_resolving_forward_refs(union_type, required=True)
  1482. return core_schema.union_schema([self.generate_schema(type[args]) for args in args])
  1483. def _subclass_schema(self, type_: Any) -> core_schema.CoreSchema:
  1484. """Generate schema for a type, e.g. `type[int]`."""
  1485. type_param = self._get_first_arg_or_any(type_)
  1486. # Assume `type[Annotated[<typ>, ...]]` is equivalent to `type[<typ>]`:
  1487. type_param = _typing_extra.annotated_type(type_param) or type_param
  1488. if typing_objects.is_any(type_param):
  1489. return self._type_schema()
  1490. elif typing_objects.is_typealiastype(type_param):
  1491. return self.generate_schema(type[type_param.__value__])
  1492. elif typing_objects.is_typevar(type_param):
  1493. if type_param.__bound__:
  1494. if is_union_origin(get_origin(type_param.__bound__)):
  1495. return self._union_is_subclass_schema(type_param.__bound__)
  1496. return core_schema.is_subclass_schema(type_param.__bound__)
  1497. elif type_param.__constraints__:
  1498. return core_schema.union_schema([self.generate_schema(type[c]) for c in type_param.__constraints__])
  1499. else:
  1500. return self._type_schema()
  1501. elif is_union_origin(get_origin(type_param)):
  1502. return self._union_is_subclass_schema(type_param)
  1503. else:
  1504. if typing_objects.is_self(type_param):
  1505. type_param = self._resolve_self_type(type_param)
  1506. if _typing_extra.is_generic_alias(type_param):
  1507. raise PydanticUserError(
  1508. 'Subscripting `type[]` with an already parametrized type is not supported. '
  1509. f'Instead of using type[{type_param!r}], use type[{_repr.display_as_type(get_origin(type_param))}].',
  1510. code=None,
  1511. )
  1512. if not inspect.isclass(type_param):
  1513. # when using type[None], this doesn't type convert to type[NoneType], and None isn't a class
  1514. # so we handle it manually here
  1515. if type_param is None:
  1516. return core_schema.is_subclass_schema(_typing_extra.NoneType)
  1517. raise TypeError(f'Expected a class, got {type_param!r}')
  1518. return core_schema.is_subclass_schema(type_param)
  1519. def _sequence_schema(self, items_type: Any) -> core_schema.CoreSchema:
  1520. """Generate schema for a Sequence, e.g. `Sequence[int]`."""
  1521. from ._serializers import serialize_sequence_via_list
  1522. item_type_schema = self.generate_schema(items_type)
  1523. list_schema = core_schema.list_schema(item_type_schema)
  1524. json_schema = smart_deepcopy(list_schema)
  1525. python_schema = core_schema.is_instance_schema(typing.Sequence, cls_repr='Sequence')
  1526. if not typing_objects.is_any(items_type):
  1527. from ._validators import sequence_validator
  1528. python_schema = core_schema.chain_schema(
  1529. [python_schema, core_schema.no_info_wrap_validator_function(sequence_validator, list_schema)],
  1530. )
  1531. serialization = core_schema.wrap_serializer_function_ser_schema(
  1532. serialize_sequence_via_list, schema=item_type_schema, info_arg=True
  1533. )
  1534. return core_schema.json_or_python_schema(
  1535. json_schema=json_schema, python_schema=python_schema, serialization=serialization
  1536. )
  1537. def _iterable_schema(self, type_: Any) -> core_schema.GeneratorSchema:
  1538. """Generate a schema for an `Iterable`."""
  1539. item_type = self._get_first_arg_or_any(type_)
  1540. return core_schema.generator_schema(self.generate_schema(item_type))
  1541. def _pattern_schema(self, pattern_type: Any) -> core_schema.CoreSchema:
  1542. from . import _validators
  1543. metadata = {'pydantic_js_functions': [lambda _1, _2: {'type': 'string', 'format': 'regex'}]}
  1544. ser = core_schema.plain_serializer_function_ser_schema(
  1545. attrgetter('pattern'), when_used='json', return_schema=core_schema.str_schema()
  1546. )
  1547. if pattern_type is typing.Pattern or pattern_type is re.Pattern:
  1548. # bare type
  1549. return core_schema.no_info_plain_validator_function(
  1550. _validators.pattern_either_validator, serialization=ser, metadata=metadata
  1551. )
  1552. param = self._get_args_resolving_forward_refs(
  1553. pattern_type,
  1554. required=True,
  1555. )[0]
  1556. if param is str:
  1557. return core_schema.no_info_plain_validator_function(
  1558. _validators.pattern_str_validator, serialization=ser, metadata=metadata
  1559. )
  1560. elif param is bytes:
  1561. return core_schema.no_info_plain_validator_function(
  1562. _validators.pattern_bytes_validator, serialization=ser, metadata=metadata
  1563. )
  1564. else:
  1565. raise PydanticSchemaGenerationError(f'Unable to generate pydantic-core schema for {pattern_type!r}.')
  1566. def _hashable_schema(self) -> core_schema.CoreSchema:
  1567. return core_schema.custom_error_schema(
  1568. schema=core_schema.json_or_python_schema(
  1569. json_schema=core_schema.chain_schema(
  1570. [core_schema.any_schema(), core_schema.is_instance_schema(collections.abc.Hashable)]
  1571. ),
  1572. python_schema=core_schema.is_instance_schema(collections.abc.Hashable),
  1573. ),
  1574. custom_error_type='is_hashable',
  1575. custom_error_message='Input should be hashable',
  1576. )
  1577. def _dataclass_schema(
  1578. self, dataclass: type[StandardDataclass], origin: type[StandardDataclass] | None
  1579. ) -> core_schema.CoreSchema:
  1580. """Generate schema for a dataclass."""
  1581. with (
  1582. self.model_type_stack.push(dataclass),
  1583. self.defs.get_schema_or_ref(dataclass) as (
  1584. dataclass_ref,
  1585. maybe_schema,
  1586. ),
  1587. ):
  1588. if maybe_schema is not None:
  1589. return maybe_schema
  1590. schema = dataclass.__dict__.get('__pydantic_core_schema__')
  1591. if schema is not None and not isinstance(schema, MockCoreSchema):
  1592. if schema['type'] == 'definitions':
  1593. schema = self.defs.unpack_definitions(schema)
  1594. ref = get_ref(schema)
  1595. if ref:
  1596. return self.defs.create_definition_reference_schema(schema)
  1597. else:
  1598. return schema
  1599. typevars_map = get_standard_typevars_map(dataclass)
  1600. if origin is not None:
  1601. dataclass = origin
  1602. # if (plain) dataclass doesn't have config, we use the parent's config, hence a default of `None`
  1603. # (Pydantic dataclasses have an empty dict config by default).
  1604. # see https://github.com/pydantic/pydantic/issues/10917
  1605. config = getattr(dataclass, '__pydantic_config__', None)
  1606. from ..dataclasses import is_pydantic_dataclass
  1607. with self._ns_resolver.push(dataclass), self._config_wrapper_stack.push(config):
  1608. if is_pydantic_dataclass(dataclass):
  1609. if dataclass.__pydantic_fields_complete__():
  1610. # Copy the field info instances to avoid mutating the `FieldInfo` instances
  1611. # of the generic dataclass generic origin (e.g. `apply_typevars_map` below).
  1612. # Note that we don't apply `deepcopy` on `__pydantic_fields__` because we
  1613. # don't want to copy the `FieldInfo` attributes:
  1614. fields = {
  1615. f_name: copy(field_info) for f_name, field_info in dataclass.__pydantic_fields__.items()
  1616. }
  1617. if typevars_map:
  1618. for field in fields.values():
  1619. field.apply_typevars_map(typevars_map, *self._types_namespace)
  1620. else:
  1621. try:
  1622. fields = rebuild_dataclass_fields(
  1623. dataclass,
  1624. config_wrapper=self._config_wrapper,
  1625. ns_resolver=self._ns_resolver,
  1626. typevars_map=typevars_map or {},
  1627. )
  1628. except NameError as e:
  1629. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  1630. else:
  1631. fields = collect_dataclass_fields(
  1632. dataclass,
  1633. typevars_map=typevars_map,
  1634. config_wrapper=self._config_wrapper,
  1635. )
  1636. if self._config_wrapper.extra == 'allow':
  1637. # disallow combination of init=False on a dataclass field and extra='allow' on a dataclass
  1638. for field_name, field in fields.items():
  1639. if field.init is False:
  1640. raise PydanticUserError(
  1641. f'Field {field_name} has `init=False` and dataclass has config setting `extra="allow"`. '
  1642. f'This combination is not allowed.',
  1643. code='dataclass-init-false-extra-allow',
  1644. )
  1645. decorators = dataclass.__dict__.get('__pydantic_decorators__')
  1646. if decorators is None:
  1647. decorators = DecoratorInfos.build(dataclass)
  1648. decorators.update_from_config(self._config_wrapper)
  1649. # Move kw_only=False args to the start of the list, as this is how vanilla dataclasses work.
  1650. # Note that when kw_only is missing or None, it is treated as equivalent to kw_only=True
  1651. args = sorted(
  1652. (self._generate_dc_field_schema(k, v, decorators) for k, v in fields.items()),
  1653. key=lambda a: a.get('kw_only') is not False,
  1654. )
  1655. has_post_init = hasattr(dataclass, '__post_init__')
  1656. has_slots = hasattr(dataclass, '__slots__')
  1657. args_schema = core_schema.dataclass_args_schema(
  1658. dataclass.__name__,
  1659. args,
  1660. computed_fields=[
  1661. self._computed_field_schema(d, decorators.field_serializers)
  1662. for d in decorators.computed_fields.values()
  1663. ],
  1664. collect_init_only=has_post_init,
  1665. )
  1666. inner_schema = apply_validators(args_schema, decorators.root_validators.values())
  1667. model_validators = decorators.model_validators.values()
  1668. inner_schema = apply_model_validators(inner_schema, model_validators, 'inner')
  1669. core_config = self._config_wrapper.core_config(title=dataclass.__name__)
  1670. dc_schema = core_schema.dataclass_schema(
  1671. dataclass,
  1672. inner_schema,
  1673. generic_origin=origin,
  1674. post_init=has_post_init,
  1675. ref=dataclass_ref,
  1676. fields=[field.name for field in dataclasses.fields(dataclass)],
  1677. slots=has_slots,
  1678. config=core_config,
  1679. # we don't use a custom __setattr__ for dataclasses, so we must
  1680. # pass along the frozen config setting to the pydantic-core schema
  1681. frozen=self._config_wrapper_stack.tail.frozen,
  1682. )
  1683. schema = self._apply_model_serializers(dc_schema, decorators.model_serializers.values())
  1684. schema = apply_model_validators(schema, model_validators, 'outer')
  1685. return self.defs.create_definition_reference_schema(schema)
  1686. def _call_schema(self, function: ValidateCallSupportedTypes) -> core_schema.CallSchema:
  1687. """Generate schema for a Callable.
  1688. TODO support functional validators once we support them in Config
  1689. """
  1690. arguments_schema = self._arguments_schema(function)
  1691. return_schema: core_schema.CoreSchema | None = None
  1692. config_wrapper = self._config_wrapper
  1693. if config_wrapper.validate_return:
  1694. sig = signature(function)
  1695. return_hint = sig.return_annotation
  1696. if return_hint is not sig.empty:
  1697. globalns, localns = self._types_namespace
  1698. type_hints = _typing_extra.get_function_type_hints(
  1699. function, globalns=globalns, localns=localns, include_keys={'return'}
  1700. )
  1701. return_schema = self.generate_schema(type_hints['return'])
  1702. return core_schema.call_schema(
  1703. arguments_schema,
  1704. function,
  1705. return_schema=return_schema,
  1706. )
  1707. def _arguments_schema(
  1708. self, function: ValidateCallSupportedTypes, parameters_callback: ParametersCallback | None = None
  1709. ) -> core_schema.ArgumentsSchema:
  1710. """Generate schema for a Signature."""
  1711. mode_lookup: dict[_ParameterKind, Literal['positional_only', 'positional_or_keyword', 'keyword_only']] = {
  1712. Parameter.POSITIONAL_ONLY: 'positional_only',
  1713. Parameter.POSITIONAL_OR_KEYWORD: 'positional_or_keyword',
  1714. Parameter.KEYWORD_ONLY: 'keyword_only',
  1715. }
  1716. sig = signature(function)
  1717. globalns, localns = self._types_namespace
  1718. type_hints = _typing_extra.get_function_type_hints(function, globalns=globalns, localns=localns)
  1719. arguments_list: list[core_schema.ArgumentsParameter] = []
  1720. var_args_schema: core_schema.CoreSchema | None = None
  1721. var_kwargs_schema: core_schema.CoreSchema | None = None
  1722. var_kwargs_mode: core_schema.VarKwargsMode | None = None
  1723. for i, (name, p) in enumerate(sig.parameters.items()):
  1724. if p.annotation is sig.empty:
  1725. annotation = typing.cast(Any, Any)
  1726. else:
  1727. annotation = type_hints[name]
  1728. if parameters_callback is not None:
  1729. result = parameters_callback(i, name, annotation)
  1730. if result == 'skip':
  1731. continue
  1732. parameter_mode = mode_lookup.get(p.kind)
  1733. if parameter_mode is not None:
  1734. arg_schema = self._generate_parameter_schema(
  1735. name, annotation, AnnotationSource.FUNCTION, p.default, parameter_mode
  1736. )
  1737. arguments_list.append(arg_schema)
  1738. elif p.kind == Parameter.VAR_POSITIONAL:
  1739. var_args_schema = self.generate_schema(annotation)
  1740. else:
  1741. assert p.kind == Parameter.VAR_KEYWORD, p.kind
  1742. unpack_type = _typing_extra.unpack_type(annotation)
  1743. if unpack_type is not None:
  1744. origin = get_origin(unpack_type) or unpack_type
  1745. if not is_typeddict(origin):
  1746. raise PydanticUserError(
  1747. f'Expected a `TypedDict` class inside `Unpack[...]`, got {unpack_type!r}',
  1748. code='unpack-typed-dict',
  1749. )
  1750. non_pos_only_param_names = {
  1751. name for name, p in sig.parameters.items() if p.kind != Parameter.POSITIONAL_ONLY
  1752. }
  1753. overlapping_params = non_pos_only_param_names.intersection(origin.__annotations__)
  1754. if overlapping_params:
  1755. raise PydanticUserError(
  1756. f'Typed dictionary {origin.__name__!r} overlaps with parameter'
  1757. f'{"s" if len(overlapping_params) >= 2 else ""} '
  1758. f'{", ".join(repr(p) for p in sorted(overlapping_params))}',
  1759. code='overlapping-unpack-typed-dict',
  1760. )
  1761. var_kwargs_mode = 'unpacked-typed-dict'
  1762. var_kwargs_schema = self._typed_dict_schema(unpack_type, get_origin(unpack_type))
  1763. else:
  1764. var_kwargs_mode = 'uniform'
  1765. var_kwargs_schema = self.generate_schema(annotation)
  1766. return core_schema.arguments_schema(
  1767. arguments_list,
  1768. var_args_schema=var_args_schema,
  1769. var_kwargs_mode=var_kwargs_mode,
  1770. var_kwargs_schema=var_kwargs_schema,
  1771. validate_by_name=self._config_wrapper.validate_by_name,
  1772. )
  1773. def _arguments_v3_schema(
  1774. self, function: ValidateCallSupportedTypes, parameters_callback: ParametersCallback | None = None
  1775. ) -> core_schema.ArgumentsV3Schema:
  1776. mode_lookup: dict[
  1777. _ParameterKind, Literal['positional_only', 'positional_or_keyword', 'var_args', 'keyword_only']
  1778. ] = {
  1779. Parameter.POSITIONAL_ONLY: 'positional_only',
  1780. Parameter.POSITIONAL_OR_KEYWORD: 'positional_or_keyword',
  1781. Parameter.VAR_POSITIONAL: 'var_args',
  1782. Parameter.KEYWORD_ONLY: 'keyword_only',
  1783. }
  1784. sig = signature(function)
  1785. globalns, localns = self._types_namespace
  1786. type_hints = _typing_extra.get_function_type_hints(function, globalns=globalns, localns=localns)
  1787. parameters_list: list[core_schema.ArgumentsV3Parameter] = []
  1788. for i, (name, p) in enumerate(sig.parameters.items()):
  1789. if parameters_callback is not None:
  1790. result = parameters_callback(i, name, p.annotation)
  1791. if result == 'skip':
  1792. continue
  1793. if p.annotation is Parameter.empty:
  1794. annotation = typing.cast(Any, Any)
  1795. else:
  1796. annotation = type_hints[name]
  1797. parameter_mode = mode_lookup.get(p.kind)
  1798. if parameter_mode is None:
  1799. assert p.kind == Parameter.VAR_KEYWORD, p.kind
  1800. unpack_type = _typing_extra.unpack_type(annotation)
  1801. if unpack_type is not None:
  1802. origin = get_origin(unpack_type) or unpack_type
  1803. if not is_typeddict(origin):
  1804. raise PydanticUserError(
  1805. f'Expected a `TypedDict` class inside `Unpack[...]`, got {unpack_type!r}',
  1806. code='unpack-typed-dict',
  1807. )
  1808. non_pos_only_param_names = {
  1809. name for name, p in sig.parameters.items() if p.kind != Parameter.POSITIONAL_ONLY
  1810. }
  1811. overlapping_params = non_pos_only_param_names.intersection(origin.__annotations__)
  1812. if overlapping_params:
  1813. raise PydanticUserError(
  1814. f'Typed dictionary {origin.__name__!r} overlaps with parameter'
  1815. f'{"s" if len(overlapping_params) >= 2 else ""} '
  1816. f'{", ".join(repr(p) for p in sorted(overlapping_params))}',
  1817. code='overlapping-unpack-typed-dict',
  1818. )
  1819. parameter_mode = 'var_kwargs_unpacked_typed_dict'
  1820. annotation = unpack_type
  1821. else:
  1822. parameter_mode = 'var_kwargs_uniform'
  1823. parameters_list.append(
  1824. self._generate_parameter_v3_schema(
  1825. name, annotation, AnnotationSource.FUNCTION, parameter_mode, default=p.default
  1826. )
  1827. )
  1828. return core_schema.arguments_v3_schema(
  1829. parameters_list,
  1830. validate_by_name=self._config_wrapper.validate_by_name,
  1831. )
  1832. def _unsubstituted_typevar_schema(self, typevar: typing.TypeVar) -> core_schema.CoreSchema:
  1833. try:
  1834. has_default = typevar.has_default() # pyright: ignore[reportAttributeAccessIssue]
  1835. except AttributeError:
  1836. # Happens if using `typing.TypeVar` (and not `typing_extensions`) on Python < 3.13
  1837. pass
  1838. else:
  1839. if has_default:
  1840. return self.generate_schema(typevar.__default__) # pyright: ignore[reportAttributeAccessIssue]
  1841. if constraints := typevar.__constraints__:
  1842. return self._union_schema(typing.Union[constraints])
  1843. if bound := typevar.__bound__:
  1844. schema = self.generate_schema(bound)
  1845. schema['serialization'] = core_schema.simple_ser_schema('any')
  1846. return schema
  1847. return core_schema.any_schema()
  1848. def _computed_field_schema(
  1849. self,
  1850. d: Decorator[ComputedFieldInfo],
  1851. field_serializers: dict[str, Decorator[FieldSerializerDecoratorInfo]],
  1852. ) -> core_schema.ComputedField:
  1853. if d.info.return_type is not PydanticUndefined:
  1854. return_type = d.info.return_type
  1855. else:
  1856. try:
  1857. # Do not pass in globals as the function could be defined in a different module.
  1858. # Instead, let `get_callable_return_type` infer the globals to use, but still pass
  1859. # in locals that may contain a parent/rebuild namespace:
  1860. return_type = _decorators.get_callable_return_type(d.func, localns=self._types_namespace.locals)
  1861. except NameError as e:
  1862. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  1863. if return_type is PydanticUndefined:
  1864. raise PydanticUserError(
  1865. 'Computed field is missing return type annotation or specifying `return_type`'
  1866. ' to the `@computed_field` decorator (e.g. `@computed_field(return_type=int | str)`)',
  1867. code='model-field-missing-annotation',
  1868. )
  1869. return_type = replace_types(return_type, self._typevars_map)
  1870. # Create a new ComputedFieldInfo so that different type parametrizations of the same
  1871. # generic model's computed field can have different return types.
  1872. d.info = dataclasses.replace(d.info, return_type=return_type)
  1873. return_type_schema = self.generate_schema(return_type)
  1874. # Apply serializers to computed field if there exist
  1875. return_type_schema = self._apply_field_serializers(
  1876. return_type_schema,
  1877. filter_field_decorator_info_by_field(field_serializers.values(), d.cls_var_name),
  1878. )
  1879. pydantic_js_updates, pydantic_js_extra = _extract_json_schema_info_from_field_info(d.info)
  1880. core_metadata: dict[str, Any] = {}
  1881. update_core_metadata(
  1882. core_metadata,
  1883. pydantic_js_updates={'readOnly': True, **(pydantic_js_updates if pydantic_js_updates else {})},
  1884. pydantic_js_extra=pydantic_js_extra,
  1885. )
  1886. return core_schema.computed_field(
  1887. d.cls_var_name, return_schema=return_type_schema, alias=d.info.alias, metadata=core_metadata
  1888. )
  1889. def _annotated_schema(self, annotated_type: Any) -> core_schema.CoreSchema:
  1890. """Generate schema for an Annotated type, e.g. `Annotated[int, Field(...)]` or `Annotated[int, Gt(0)]`."""
  1891. FieldInfo = import_cached_field_info()
  1892. source_type, *annotations = self._get_args_resolving_forward_refs(
  1893. annotated_type,
  1894. required=True,
  1895. )
  1896. schema = self._apply_annotations(source_type, annotations)
  1897. # put the default validator last so that TypeAdapter.get_default_value() works
  1898. # even if there are function validators involved
  1899. for annotation in annotations:
  1900. if isinstance(annotation, FieldInfo):
  1901. schema = wrap_default(annotation, schema)
  1902. return schema
  1903. def _apply_annotations(
  1904. self,
  1905. source_type: Any,
  1906. annotations: list[Any],
  1907. transform_inner_schema: Callable[[CoreSchema], CoreSchema] = lambda x: x,
  1908. check_unsupported_field_info_attributes: bool = True,
  1909. ) -> CoreSchema:
  1910. """Apply arguments from `Annotated` or from `FieldInfo` to a schema.
  1911. This gets called by `GenerateSchema._annotated_schema` but differs from it in that it does
  1912. not expect `source_type` to be an `Annotated` object, it expects it to be the first argument of that
  1913. (in other words, `GenerateSchema._annotated_schema` just unpacks `Annotated`, this process it).
  1914. """
  1915. annotations = list(_known_annotated_metadata.expand_grouped_metadata(annotations))
  1916. pydantic_js_annotation_functions: list[GetJsonSchemaFunction] = []
  1917. def inner_handler(obj: Any) -> CoreSchema:
  1918. schema = self._generate_schema_from_get_schema_method(obj, source_type)
  1919. if schema is None:
  1920. schema = self._generate_schema_inner(obj)
  1921. metadata_js_function = _extract_get_pydantic_json_schema(obj)
  1922. if metadata_js_function is not None:
  1923. metadata_schema = resolve_original_schema(schema, self.defs)
  1924. if metadata_schema is not None:
  1925. self._add_js_function(metadata_schema, metadata_js_function)
  1926. return transform_inner_schema(schema)
  1927. get_inner_schema = CallbackGetCoreSchemaHandler(inner_handler, self)
  1928. for annotation in annotations:
  1929. if annotation is None:
  1930. continue
  1931. get_inner_schema = self._get_wrapped_inner_schema(
  1932. get_inner_schema,
  1933. annotation,
  1934. pydantic_js_annotation_functions,
  1935. check_unsupported_field_info_attributes=check_unsupported_field_info_attributes,
  1936. )
  1937. schema = get_inner_schema(source_type)
  1938. if pydantic_js_annotation_functions:
  1939. core_metadata = schema.setdefault('metadata', {})
  1940. update_core_metadata(core_metadata, pydantic_js_annotation_functions=pydantic_js_annotation_functions)
  1941. return _add_custom_serialization_from_json_encoders(self._config_wrapper.json_encoders, source_type, schema)
  1942. def _apply_single_annotation(
  1943. self,
  1944. schema: core_schema.CoreSchema,
  1945. metadata: Any,
  1946. check_unsupported_field_info_attributes: bool = True,
  1947. ) -> core_schema.CoreSchema:
  1948. FieldInfo = import_cached_field_info()
  1949. if isinstance(metadata, FieldInfo):
  1950. if (
  1951. check_unsupported_field_info_attributes
  1952. # HACK: we don't want to emit the warning for `FieldInfo` subclasses, because FastAPI does weird manipulations
  1953. # with its subclasses and their annotations:
  1954. and type(metadata) is FieldInfo
  1955. ):
  1956. for attr, value in (unsupported_attributes := self._get_unsupported_field_info_attributes(metadata)):
  1957. warnings.warn(
  1958. f'The {attr!r} attribute with value {value!r} was provided to the `Field()` function, '
  1959. f'which has no effect in the context it was used. {attr!r} is field-specific metadata, '
  1960. 'and can only be attached to a model field using `Annotated` metadata or by assignment. '
  1961. 'This may have happened because an `Annotated` type alias using the `type` statement was '
  1962. 'used, or if the `Field()` function was attached to a single member of a union type.',
  1963. category=UnsupportedFieldAttributeWarning,
  1964. )
  1965. if (
  1966. metadata.default_factory_takes_validated_data
  1967. and self.model_type_stack.get() is None
  1968. and 'defaut_factory' not in unsupported_attributes
  1969. ):
  1970. warnings.warn(
  1971. "A 'default_factory' taking validated data as an argument was provided to the `Field()` function, "
  1972. 'but no validated data is available in the context it was used.',
  1973. category=UnsupportedFieldAttributeWarning,
  1974. )
  1975. for field_metadata in metadata.metadata:
  1976. schema = self._apply_single_annotation(schema, field_metadata)
  1977. if metadata.discriminator is not None:
  1978. schema = self._apply_discriminator_to_union(schema, metadata.discriminator)
  1979. return schema
  1980. if schema['type'] == 'nullable':
  1981. # for nullable schemas, metadata is automatically applied to the inner schema
  1982. inner = schema.get('schema', core_schema.any_schema())
  1983. inner = self._apply_single_annotation(inner, metadata)
  1984. if inner:
  1985. schema['schema'] = inner
  1986. return schema
  1987. original_schema = schema
  1988. ref = schema.get('ref')
  1989. if ref is not None:
  1990. schema = schema.copy()
  1991. new_ref = ref + f'_{repr(metadata)}'
  1992. if (existing := self.defs.get_schema_from_ref(new_ref)) is not None:
  1993. return existing
  1994. schema['ref'] = new_ref # pyright: ignore[reportGeneralTypeIssues]
  1995. elif schema['type'] == 'definition-ref':
  1996. ref = schema['schema_ref']
  1997. if (referenced_schema := self.defs.get_schema_from_ref(ref)) is not None:
  1998. schema = referenced_schema.copy()
  1999. new_ref = ref + f'_{repr(metadata)}'
  2000. if (existing := self.defs.get_schema_from_ref(new_ref)) is not None:
  2001. return existing
  2002. schema['ref'] = new_ref # pyright: ignore[reportGeneralTypeIssues]
  2003. maybe_updated_schema = _known_annotated_metadata.apply_known_metadata(metadata, schema)
  2004. if maybe_updated_schema is not None:
  2005. return maybe_updated_schema
  2006. return original_schema
  2007. def _apply_single_annotation_json_schema(
  2008. self, schema: core_schema.CoreSchema, metadata: Any
  2009. ) -> core_schema.CoreSchema:
  2010. FieldInfo = import_cached_field_info()
  2011. if isinstance(metadata, FieldInfo):
  2012. for field_metadata in metadata.metadata:
  2013. schema = self._apply_single_annotation_json_schema(schema, field_metadata)
  2014. pydantic_js_updates, pydantic_js_extra = _extract_json_schema_info_from_field_info(metadata)
  2015. core_metadata = schema.setdefault('metadata', {})
  2016. update_core_metadata(
  2017. core_metadata, pydantic_js_updates=pydantic_js_updates, pydantic_js_extra=pydantic_js_extra
  2018. )
  2019. return schema
  2020. def _get_unsupported_field_info_attributes(self, field_info: FieldInfo) -> list[tuple[str, Any]]:
  2021. """Get the list of unsupported `FieldInfo` attributes when not directly used in `Annotated` for field annotations."""
  2022. unused_metadata: list[tuple[str, Any]] = []
  2023. for unused_metadata_name, unset_value in UNSUPPORTED_STANDALONE_FIELDINFO_ATTRIBUTES:
  2024. if (
  2025. (unused_metadata_value := getattr(field_info, unused_metadata_name)) is not unset_value
  2026. # `default` and `default_factory` can still be used with a type adapter, so only include them
  2027. # if used with a model-like class:
  2028. and (
  2029. unused_metadata_name not in ('default', 'default_factory')
  2030. or self.model_type_stack.get() is not None
  2031. )
  2032. # Setting `alias` will set `validation/serialization_alias` as well, so we want to avoid duplicate warnings:
  2033. and (
  2034. unused_metadata_name not in ('validation_alias', 'serialization_alias')
  2035. or 'alias' not in field_info._attributes_set
  2036. )
  2037. ):
  2038. unused_metadata.append((unused_metadata_name, unused_metadata_value))
  2039. return unused_metadata
  2040. def _get_wrapped_inner_schema(
  2041. self,
  2042. get_inner_schema: GetCoreSchemaHandler,
  2043. annotation: Any,
  2044. pydantic_js_annotation_functions: list[GetJsonSchemaFunction],
  2045. check_unsupported_field_info_attributes: bool = False,
  2046. ) -> CallbackGetCoreSchemaHandler:
  2047. annotation_get_schema: GetCoreSchemaFunction | None = getattr(annotation, '__get_pydantic_core_schema__', None)
  2048. def new_handler(source: Any) -> core_schema.CoreSchema:
  2049. if annotation_get_schema is not None:
  2050. schema = annotation_get_schema(source, get_inner_schema)
  2051. else:
  2052. schema = get_inner_schema(source)
  2053. schema = self._apply_single_annotation(
  2054. schema,
  2055. annotation,
  2056. check_unsupported_field_info_attributes=check_unsupported_field_info_attributes,
  2057. )
  2058. schema = self._apply_single_annotation_json_schema(schema, annotation)
  2059. metadata_js_function = _extract_get_pydantic_json_schema(annotation)
  2060. if metadata_js_function is not None:
  2061. pydantic_js_annotation_functions.append(metadata_js_function)
  2062. return schema
  2063. return CallbackGetCoreSchemaHandler(new_handler, self)
  2064. def _apply_field_serializers(
  2065. self,
  2066. schema: core_schema.CoreSchema,
  2067. serializers: list[Decorator[FieldSerializerDecoratorInfo]],
  2068. ) -> core_schema.CoreSchema:
  2069. """Apply field serializers to a schema."""
  2070. if serializers:
  2071. schema = copy(schema)
  2072. if schema['type'] == 'definitions':
  2073. inner_schema = schema['schema']
  2074. schema['schema'] = self._apply_field_serializers(inner_schema, serializers)
  2075. return schema
  2076. elif 'ref' in schema:
  2077. schema = self.defs.create_definition_reference_schema(schema)
  2078. # use the last serializer to make it easy to override a serializer set on a parent model
  2079. serializer = serializers[-1]
  2080. is_field_serializer, info_arg = inspect_field_serializer(serializer.func, serializer.info.mode)
  2081. if serializer.info.return_type is not PydanticUndefined:
  2082. return_type = serializer.info.return_type
  2083. else:
  2084. try:
  2085. # Do not pass in globals as the function could be defined in a different module.
  2086. # Instead, let `get_callable_return_type` infer the globals to use, but still pass
  2087. # in locals that may contain a parent/rebuild namespace:
  2088. return_type = _decorators.get_callable_return_type(
  2089. serializer.func, localns=self._types_namespace.locals
  2090. )
  2091. except NameError as e:
  2092. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  2093. if return_type is PydanticUndefined:
  2094. return_schema = None
  2095. else:
  2096. return_schema = self.generate_schema(return_type)
  2097. if serializer.info.mode == 'wrap':
  2098. schema['serialization'] = core_schema.wrap_serializer_function_ser_schema(
  2099. serializer.func,
  2100. is_field_serializer=is_field_serializer,
  2101. info_arg=info_arg,
  2102. return_schema=return_schema,
  2103. when_used=serializer.info.when_used,
  2104. )
  2105. else:
  2106. assert serializer.info.mode == 'plain'
  2107. schema['serialization'] = core_schema.plain_serializer_function_ser_schema(
  2108. serializer.func,
  2109. is_field_serializer=is_field_serializer,
  2110. info_arg=info_arg,
  2111. return_schema=return_schema,
  2112. when_used=serializer.info.when_used,
  2113. )
  2114. return schema
  2115. def _apply_model_serializers(
  2116. self, schema: core_schema.CoreSchema, serializers: Iterable[Decorator[ModelSerializerDecoratorInfo]]
  2117. ) -> core_schema.CoreSchema:
  2118. """Apply model serializers to a schema."""
  2119. ref: str | None = schema.pop('ref', None) # type: ignore
  2120. if serializers:
  2121. serializer = list(serializers)[-1]
  2122. info_arg = inspect_model_serializer(serializer.func, serializer.info.mode)
  2123. if serializer.info.return_type is not PydanticUndefined:
  2124. return_type = serializer.info.return_type
  2125. else:
  2126. try:
  2127. # Do not pass in globals as the function could be defined in a different module.
  2128. # Instead, let `get_callable_return_type` infer the globals to use, but still pass
  2129. # in locals that may contain a parent/rebuild namespace:
  2130. return_type = _decorators.get_callable_return_type(
  2131. serializer.func, localns=self._types_namespace.locals
  2132. )
  2133. except NameError as e:
  2134. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  2135. if return_type is PydanticUndefined:
  2136. return_schema = None
  2137. else:
  2138. return_schema = self.generate_schema(return_type)
  2139. if serializer.info.mode == 'wrap':
  2140. ser_schema: core_schema.SerSchema = core_schema.wrap_serializer_function_ser_schema(
  2141. serializer.func,
  2142. info_arg=info_arg,
  2143. return_schema=return_schema,
  2144. when_used=serializer.info.when_used,
  2145. )
  2146. else:
  2147. # plain
  2148. ser_schema = core_schema.plain_serializer_function_ser_schema(
  2149. serializer.func,
  2150. info_arg=info_arg,
  2151. return_schema=return_schema,
  2152. when_used=serializer.info.when_used,
  2153. )
  2154. schema['serialization'] = ser_schema
  2155. if ref:
  2156. schema['ref'] = ref # type: ignore
  2157. return schema
  2158. _VALIDATOR_F_MATCH: Mapping[
  2159. tuple[FieldValidatorModes, Literal['no-info', 'with-info']],
  2160. Callable[[Callable[..., Any], core_schema.CoreSchema], core_schema.CoreSchema],
  2161. ] = {
  2162. ('before', 'no-info'): lambda f, schema: core_schema.no_info_before_validator_function(f, schema),
  2163. ('after', 'no-info'): lambda f, schema: core_schema.no_info_after_validator_function(f, schema),
  2164. ('plain', 'no-info'): lambda f, _: core_schema.no_info_plain_validator_function(f),
  2165. ('wrap', 'no-info'): lambda f, schema: core_schema.no_info_wrap_validator_function(f, schema),
  2166. ('before', 'with-info'): lambda f, schema: core_schema.with_info_before_validator_function(f, schema),
  2167. ('after', 'with-info'): lambda f, schema: core_schema.with_info_after_validator_function(f, schema),
  2168. ('plain', 'with-info'): lambda f, _: core_schema.with_info_plain_validator_function(f),
  2169. ('wrap', 'with-info'): lambda f, schema: core_schema.with_info_wrap_validator_function(f, schema),
  2170. }
  2171. # TODO V3: this function is only used for deprecated decorators. It should
  2172. # be removed once we drop support for those.
  2173. def apply_validators(
  2174. schema: core_schema.CoreSchema,
  2175. validators: Iterable[Decorator[RootValidatorDecoratorInfo]]
  2176. | Iterable[Decorator[ValidatorDecoratorInfo]]
  2177. | Iterable[Decorator[FieldValidatorDecoratorInfo]],
  2178. ) -> core_schema.CoreSchema:
  2179. """Apply validators to a schema.
  2180. Args:
  2181. schema: The schema to apply validators on.
  2182. validators: An iterable of validators.
  2183. field_name: The name of the field if validators are being applied to a model field.
  2184. Returns:
  2185. The updated schema.
  2186. """
  2187. for validator in validators:
  2188. # Actually, type could be 'field' or 'model', but this is only used for deprecated
  2189. # decorators, so let's not worry about it.
  2190. info_arg = inspect_validator(validator.func, mode=validator.info.mode, type='field')
  2191. val_type = 'with-info' if info_arg else 'no-info'
  2192. schema = _VALIDATOR_F_MATCH[(validator.info.mode, val_type)](validator.func, schema)
  2193. return schema
  2194. def _validators_require_validate_default(validators: Iterable[Decorator[ValidatorDecoratorInfo]]) -> bool:
  2195. """In v1, if any of the validators for a field had `always=True`, the default value would be validated.
  2196. This serves as an auxiliary function for re-implementing that logic, by looping over a provided
  2197. collection of (v1-style) ValidatorDecoratorInfo's and checking if any of them have `always=True`.
  2198. We should be able to drop this function and the associated logic calling it once we drop support
  2199. for v1-style validator decorators. (Or we can extend it and keep it if we add something equivalent
  2200. to the v1-validator `always` kwarg to `field_validator`.)
  2201. """
  2202. for validator in validators:
  2203. if validator.info.always:
  2204. return True
  2205. return False
  2206. def _convert_to_aliases(
  2207. alias: str | AliasChoices | AliasPath | None,
  2208. ) -> str | list[str | int] | list[list[str | int]] | None:
  2209. if isinstance(alias, (AliasChoices, AliasPath)):
  2210. return alias.convert_to_aliases()
  2211. else:
  2212. return alias
  2213. def apply_model_validators(
  2214. schema: core_schema.CoreSchema,
  2215. validators: Iterable[Decorator[ModelValidatorDecoratorInfo]],
  2216. mode: Literal['inner', 'outer', 'all'],
  2217. ) -> core_schema.CoreSchema:
  2218. """Apply model validators to a schema.
  2219. If mode == 'inner', only "before" validators are applied
  2220. If mode == 'outer', validators other than "before" are applied
  2221. If mode == 'all', all validators are applied
  2222. Args:
  2223. schema: The schema to apply validators on.
  2224. validators: An iterable of validators.
  2225. mode: The validator mode.
  2226. Returns:
  2227. The updated schema.
  2228. """
  2229. ref: str | None = schema.pop('ref', None) # type: ignore
  2230. for validator in validators:
  2231. if mode == 'inner' and validator.info.mode != 'before':
  2232. continue
  2233. if mode == 'outer' and validator.info.mode == 'before':
  2234. continue
  2235. info_arg = inspect_validator(validator.func, mode=validator.info.mode, type='model')
  2236. if validator.info.mode == 'wrap':
  2237. if info_arg:
  2238. schema = core_schema.with_info_wrap_validator_function(function=validator.func, schema=schema)
  2239. else:
  2240. schema = core_schema.no_info_wrap_validator_function(function=validator.func, schema=schema)
  2241. elif validator.info.mode == 'before':
  2242. if info_arg:
  2243. schema = core_schema.with_info_before_validator_function(function=validator.func, schema=schema)
  2244. else:
  2245. schema = core_schema.no_info_before_validator_function(function=validator.func, schema=schema)
  2246. else:
  2247. assert validator.info.mode == 'after'
  2248. if info_arg:
  2249. schema = core_schema.with_info_after_validator_function(function=validator.func, schema=schema)
  2250. else:
  2251. schema = core_schema.no_info_after_validator_function(function=validator.func, schema=schema)
  2252. if ref:
  2253. schema['ref'] = ref # type: ignore
  2254. return schema
  2255. def wrap_default(field_info: FieldInfo, schema: core_schema.CoreSchema) -> core_schema.CoreSchema:
  2256. """Wrap schema with default schema if default value or `default_factory` are available.
  2257. Args:
  2258. field_info: The field info object.
  2259. schema: The schema to apply default on.
  2260. Returns:
  2261. Updated schema by default value or `default_factory`.
  2262. """
  2263. if field_info.default_factory:
  2264. return core_schema.with_default_schema(
  2265. schema,
  2266. default_factory=field_info.default_factory,
  2267. default_factory_takes_data=takes_validated_data_argument(field_info.default_factory),
  2268. validate_default=field_info.validate_default,
  2269. )
  2270. elif field_info.default is not PydanticUndefined:
  2271. return core_schema.with_default_schema(
  2272. schema, default=field_info.default, validate_default=field_info.validate_default
  2273. )
  2274. else:
  2275. return schema
  2276. def _extract_get_pydantic_json_schema(tp: Any) -> GetJsonSchemaFunction | None:
  2277. """Extract `__get_pydantic_json_schema__` from a type, handling the deprecated `__modify_schema__`."""
  2278. js_modify_function = getattr(tp, '__get_pydantic_json_schema__', None)
  2279. if hasattr(tp, '__modify_schema__'):
  2280. BaseModel = import_cached_base_model()
  2281. has_custom_v2_modify_js_func = (
  2282. js_modify_function is not None
  2283. and BaseModel.__get_pydantic_json_schema__.__func__ # type: ignore
  2284. not in (js_modify_function, getattr(js_modify_function, '__func__', None))
  2285. )
  2286. if not has_custom_v2_modify_js_func:
  2287. cls_name = getattr(tp, '__name__', None)
  2288. raise PydanticUserError(
  2289. f'The `__modify_schema__` method is not supported in Pydantic v2. '
  2290. f'Use `__get_pydantic_json_schema__` instead{f" in class `{cls_name}`" if cls_name else ""}.',
  2291. code='custom-json-schema',
  2292. )
  2293. if (origin := get_origin(tp)) is not None:
  2294. # Generic aliases proxy attribute access to the origin, *except* dunder attributes,
  2295. # such as `__get_pydantic_json_schema__`, hence the explicit check.
  2296. return _extract_get_pydantic_json_schema(origin)
  2297. if js_modify_function is None:
  2298. return None
  2299. return js_modify_function
  2300. def resolve_original_schema(schema: CoreSchema, definitions: _Definitions) -> CoreSchema | None:
  2301. if schema['type'] == 'definition-ref':
  2302. return definitions.get_schema_from_ref(schema['schema_ref'])
  2303. elif schema['type'] == 'definitions':
  2304. return schema['schema']
  2305. else:
  2306. return schema
  2307. def _inlining_behavior(
  2308. def_ref: core_schema.DefinitionReferenceSchema,
  2309. ) -> Literal['inline', 'keep', 'preserve_metadata']:
  2310. """Determine the inlining behavior of the `'definition-ref'` schema.
  2311. - If no `'serialization'` schema and no metadata is attached, the schema can safely be inlined.
  2312. - If it has metadata but only related to the deferred discriminator application, it can be inlined
  2313. provided that such metadata is kept.
  2314. - Otherwise, the schema should not be inlined. Doing so would remove the `'serialization'` schema or metadata.
  2315. """
  2316. if 'serialization' in def_ref:
  2317. return 'keep'
  2318. metadata = def_ref.get('metadata')
  2319. if not metadata:
  2320. return 'inline'
  2321. if len(metadata) == 1 and 'pydantic_internal_union_discriminator' in metadata:
  2322. return 'preserve_metadata'
  2323. return 'keep'
  2324. class _Definitions:
  2325. """Keeps track of references and definitions."""
  2326. _recursively_seen: set[str]
  2327. """A set of recursively seen references.
  2328. When a referenceable type is encountered, the `get_schema_or_ref` context manager is
  2329. entered to compute the reference. If the type references itself by some way (e.g. for
  2330. a dataclass a Pydantic model, the class can be referenced as a field annotation),
  2331. entering the context manager again will yield a `'definition-ref'` schema that should
  2332. short-circuit the normal generation process, as the reference was already in this set.
  2333. """
  2334. _definitions: dict[str, core_schema.CoreSchema]
  2335. """A mapping of references to their corresponding schema.
  2336. When a schema for a referenceable type is generated, it is stored in this mapping. If the
  2337. same type is encountered again, the reference is yielded by the `get_schema_or_ref` context
  2338. manager.
  2339. """
  2340. def __init__(self) -> None:
  2341. self._recursively_seen = set()
  2342. self._definitions = {}
  2343. @contextmanager
  2344. def get_schema_or_ref(self, tp: Any, /) -> Generator[tuple[str, core_schema.DefinitionReferenceSchema | None]]:
  2345. """Get a definition for `tp` if one exists.
  2346. If a definition exists, a tuple of `(ref_string, CoreSchema)` is returned.
  2347. If no definition exists yet, a tuple of `(ref_string, None)` is returned.
  2348. Note that the returned `CoreSchema` will always be a `DefinitionReferenceSchema`,
  2349. not the actual definition itself.
  2350. This should be called for any type that can be identified by reference.
  2351. This includes any recursive types.
  2352. At present the following types can be named/recursive:
  2353. - Pydantic model
  2354. - Pydantic and stdlib dataclasses
  2355. - Typed dictionaries
  2356. - Named tuples
  2357. - `TypeAliasType` instances
  2358. - Enums
  2359. """
  2360. ref = get_type_ref(tp)
  2361. # return the reference if we're either (1) in a cycle or (2) it the reference was already encountered:
  2362. if ref in self._recursively_seen or ref in self._definitions:
  2363. yield (ref, core_schema.definition_reference_schema(ref))
  2364. else:
  2365. self._recursively_seen.add(ref)
  2366. try:
  2367. yield (ref, None)
  2368. finally:
  2369. self._recursively_seen.discard(ref)
  2370. def get_schema_from_ref(self, ref: str) -> CoreSchema | None:
  2371. """Resolve the schema from the given reference."""
  2372. return self._definitions.get(ref)
  2373. def create_definition_reference_schema(self, schema: CoreSchema) -> core_schema.DefinitionReferenceSchema:
  2374. """Store the schema as a definition and return a `'definition-reference'` schema pointing to it.
  2375. The schema must have a reference attached to it.
  2376. """
  2377. ref = schema['ref'] # pyright: ignore
  2378. self._definitions[ref] = schema
  2379. return core_schema.definition_reference_schema(ref)
  2380. def unpack_definitions(self, schema: core_schema.DefinitionsSchema) -> CoreSchema:
  2381. """Store the definitions of the `'definitions'` core schema and return the inner core schema."""
  2382. for def_schema in schema['definitions']:
  2383. self._definitions[def_schema['ref']] = def_schema # pyright: ignore
  2384. return schema['schema']
  2385. def finalize_schema(self, schema: CoreSchema) -> CoreSchema:
  2386. """Finalize the core schema.
  2387. This traverses the core schema and referenced definitions, replaces `'definition-ref'` schemas
  2388. by the referenced definition if possible, and applies deferred discriminators.
  2389. """
  2390. definitions = self._definitions
  2391. try:
  2392. gather_result = gather_schemas_for_cleaning(
  2393. schema,
  2394. definitions=definitions,
  2395. )
  2396. except MissingDefinitionError as e:
  2397. raise InvalidSchemaError from e
  2398. remaining_defs: dict[str, CoreSchema] = {}
  2399. # Note: this logic doesn't play well when core schemas with deferred discriminator metadata
  2400. # and references are encountered. See the `test_deferred_discriminated_union_and_references()` test.
  2401. for ref, inlinable_def_ref in gather_result['collected_references'].items():
  2402. if inlinable_def_ref is not None and (inlining_behavior := _inlining_behavior(inlinable_def_ref)) != 'keep':
  2403. if inlining_behavior == 'inline':
  2404. # `ref` was encountered, and only once:
  2405. # - `inlinable_def_ref` is a `'definition-ref'` schema and is guaranteed to be
  2406. # the only one. Transform it into the definition it points to.
  2407. # - Do not store the definition in the `remaining_defs`.
  2408. inlinable_def_ref.clear() # pyright: ignore[reportAttributeAccessIssue]
  2409. inlinable_def_ref.update(self._resolve_definition(ref, definitions)) # pyright: ignore
  2410. elif inlining_behavior == 'preserve_metadata':
  2411. # `ref` was encountered, and only once, but contains discriminator metadata.
  2412. # We will do the same thing as if `inlining_behavior` was `'inline'`, but make
  2413. # sure to keep the metadata for the deferred discriminator application logic below.
  2414. meta = inlinable_def_ref.pop('metadata')
  2415. inlinable_def_ref.clear() # pyright: ignore[reportAttributeAccessIssue]
  2416. inlinable_def_ref.update(self._resolve_definition(ref, definitions)) # pyright: ignore
  2417. inlinable_def_ref['metadata'] = meta
  2418. else:
  2419. # `ref` was encountered, at least two times (or only once, but with metadata or a serialization schema):
  2420. # - Do not inline the `'definition-ref'` schemas (they are not provided in the gather result anyway).
  2421. # - Store the the definition in the `remaining_defs`
  2422. remaining_defs[ref] = self._resolve_definition(ref, definitions)
  2423. for cs in gather_result['deferred_discriminator_schemas']:
  2424. discriminator: str | None = cs['metadata'].pop('pydantic_internal_union_discriminator', None) # pyright: ignore[reportTypedDictNotRequiredAccess]
  2425. if discriminator is None:
  2426. # This can happen in rare scenarios, when a deferred schema is present multiple times in the
  2427. # gather result (e.g. when using the `Sequence` type -- see `test_sequence_discriminated_union()`).
  2428. # In this case, a previous loop iteration applied the discriminator and so we can just skip it here.
  2429. continue
  2430. applied = _discriminated_union.apply_discriminator(cs.copy(), discriminator, remaining_defs)
  2431. # Mutate the schema directly to have the discriminator applied
  2432. cs.clear() # pyright: ignore[reportAttributeAccessIssue]
  2433. cs.update(applied) # pyright: ignore
  2434. if remaining_defs:
  2435. schema = core_schema.definitions_schema(schema=schema, definitions=[*remaining_defs.values()])
  2436. return schema
  2437. def _resolve_definition(self, ref: str, definitions: dict[str, CoreSchema]) -> CoreSchema:
  2438. definition = definitions[ref]
  2439. if definition['type'] != 'definition-ref':
  2440. return definition
  2441. # Some `'definition-ref'` schemas might act as "intermediate" references (e.g. when using
  2442. # a PEP 695 type alias (which is referenceable) that references another PEP 695 type alias):
  2443. visited: set[str] = set()
  2444. while definition['type'] == 'definition-ref' and _inlining_behavior(definition) == 'inline':
  2445. schema_ref = definition['schema_ref']
  2446. if schema_ref in visited:
  2447. raise PydanticUserError(
  2448. f'{ref} contains a circular reference to itself.', code='circular-reference-schema'
  2449. )
  2450. visited.add(schema_ref)
  2451. definition = definitions[schema_ref]
  2452. return {**definition, 'ref': ref} # pyright: ignore[reportReturnType]
  2453. class _FieldNameStack:
  2454. __slots__ = ('_stack',)
  2455. def __init__(self) -> None:
  2456. self._stack: list[str] = []
  2457. @contextmanager
  2458. def push(self, field_name: str) -> Iterator[None]:
  2459. self._stack.append(field_name)
  2460. yield
  2461. self._stack.pop()
  2462. def get(self) -> str | None:
  2463. if self._stack:
  2464. return self._stack[-1]
  2465. else:
  2466. return None
  2467. class _ModelTypeStack:
  2468. __slots__ = ('_stack',)
  2469. def __init__(self) -> None:
  2470. self._stack: list[type] = []
  2471. @contextmanager
  2472. def push(self, type_obj: type) -> Iterator[None]:
  2473. self._stack.append(type_obj)
  2474. yield
  2475. self._stack.pop()
  2476. def get(self) -> type | None:
  2477. if self._stack:
  2478. return self._stack[-1]
  2479. else:
  2480. return None