_generate_schema.py 133 KB

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