
    hc                        S SK Jr  S SKJrJr  S SKJrJrJ	r	  S SK
JrJr  SSKJr  SSKJr  SS	KJr  \(       a  SS
KJr  SSKJr   " S S\5      rSS jr S       SS jjr " S S5      rg)    )annotations)HashableSequence)TYPE_CHECKINGAnycast)
CoreSchemacore_schema   )PydanticUserError   )_core_utils)CoreSchemaFieldDiscriminator)CoreMetadatac                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )MissingDefinitionForUnionRef   zsRaised when applying a discriminated union discriminator to a schema
requires a definition that is not yet defined
c                L   > Xl         [        TU ]	  SU R                   < 35        g )NzMissing definition for ref )refsuper__init__)selfr   	__class__s     Y/var/www/html/env/lib/python3.13/site-packages/pydantic/_internal/_discriminated_union.pyr   %MissingDefinitionForUnionRef.__init__   s!    6txxlCD    )r   )r   strreturnNone)__name__
__module____qualname____firstlineno____doc__r   __static_attributes____classcell__)r   s   @r   r   r      s    E Er   r   c                D    [        SU R                  S0 5      5      nXS'   g )Nr   metadata%pydantic_internal_union_discriminator)r   
setdefault)schemadiscriminatorr*   s      r   set_discriminator_in_metadatar/      s$    NF$5$5j"$EFH8E45r   Nc                    SSK Jn  [        X5      (       a=  [        UR                  [        5      (       a  UR                  nOUR                  U 5      $ [        X=(       d    0 5      R                  U 5      $ )a:  Applies the discriminator and returns a new core schema.

Args:
    schema: The input schema.
    discriminator: The name of the field which will serve as the discriminator.
    definitions: A mapping of schema ref to schema.

Returns:
    The new core schema.

Raises:
    TypeError:
        - If `discriminator` is used with invalid union variant.
        - If `discriminator` is used with `Union` type with one variant.
        - If `discriminator` value mapped to multiple choices.
    MissingDefinitionForUnionRef:
        If the definition for ref is missing.
    PydanticUserError:
        - If a model in union doesn't have a discriminator field.
        - If discriminator field has a non-string alias.
        - If discriminator fields have different aliases.
        - If discriminator field not of type `Literal`.
r   r   )typesr   
isinstancer.   r   _convert_schema_ApplyInferredDiscriminatorapply)r-   r.   definitionsr   s       r   apply_discriminatorr7   "   s\    8 &-//m11377)77M 0088&}6GRHNNvVVr   c                      \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	      SS jr
 S     SS
 jjr S     SS jjr S     SS jjrSS jr      SS jrSS jrSrg	)r4   I   a  This class is used to convert an input schema containing a union schema into one where that union is
replaced with a tagged-union, with all the associated debugging and performance benefits.

This is done by:
* Validating that the input schema is compatible with the provided discriminator
* Introspecting the schema to determine which discriminator values should map to which union choices
* Handling various edge cases such as 'definitions', 'default', 'nullable' schemas, and more

I have chosen to implement the conversion algorithm in this class, rather than a function,
to make it easier to maintain state while recursively walking the provided CoreSchema.
c                p    Xl         X l        S U l        SU l        SU l        / U l        0 U l        SU l        g )NF)r.   r6   _discriminator_alias_should_be_nullable_is_nullable_choices_to_handle_tagged_union_choices_used)r   r.   r6   s      r   r   $_ApplyInferredDiscriminator.__init__V   sQ    
 + ' 15! $)  "
 AC NP" 
r   c                    U R                   (       a   eU R                  U5      nU R                  (       a'  U R                  (       d  [        R
                  " U5      nSU l         U$ )a  Return a new CoreSchema based on `schema` that uses a tagged-union with the discriminator provided
to this class.

Args:
    schema: The input schema.

Returns:
    The new core schema.

Raises:
    TypeError:
        - If `discriminator` is used with invalid union variant.
        - If `discriminator` is used with `Union` type with one variant.
        - If `discriminator` value mapped to multiple choices.
    ValueError:
        If the definition for ref is missing.
    PydanticUserError:
        - If a model in union doesn't have a discriminator field.
        - If discriminator field has a non-string alias.
        - If discriminator fields have different aliases.
        - If discriminator field not of type `Literal`.
T)r@   _apply_to_rootr<   r=   r
   nullable_schema)r   r-   s     r   r5   !_ApplyInferredDiscriminator.apply   sM    . ::~$$V,##D,=,= 008F
r   c                   US   S:X  a1  SU l         U R                  US   5      nUR                  5       nX#S'   U$ US   S:X  a*  U R                  US   5      nUR                  5       nX$S'   U$ US   S:w  a  [        R                  " U/5      nUS   SSS	2    Vs/ s H  n[        U[        5      (       a  US
   OUPM!     nnU R                  R                  U5        U R                  (       a>  U R                  R                  5       nU R                  U5        U R                  (       a  M>  U R                  b5  U R                  U R                  :w  a  U R                  /U R                  //nOU R                  n[        R                  " U R                  UUR                  S5      UR                  S5      UR                  S5      SSUR                  S5      UR                  S5      UR                  S5      S9
$ s  snf )zThis method handles the outer-most stage of recursion over the input schema:
unwrapping nullable or definitions schemas, and calling the `_handle_choice`
method iteratively on the choices extracted (recursively) from the possibly-wrapped union.
typenullableTr-   r6   unionchoicesNr   custom_error_typecustom_error_messagecustom_error_contextFr   r*   serialization)
rJ   r.   rL   rM   rN   strictfrom_attributesr   r*   rO   )r=   rC   copyr
   union_schemar2   tupler>   extendpop_handle_choicer;   r.   tagged_union_schemar?   get)	r   r-   wrappednullable_wrapperdefinitions_wrappervchoices_schemaschoicer.   s	            r   rC   *_ApplyInferredDiscriminator._apply_to_root   s   
 &>Z' $D))&*:;G%{{})0X&##&>]*))&*:;G"(++-,3)&&&>W$
 !--vh7F IOyHYZ^\^Z^H_`H_1:a#7#71Q4Q>H_`&&7%%,,002F' %%% $$0T5N5NRVRdRd5d <@;M;M:NQUQjQjPk9lM ..M....'$jj)<=!',B!C!',B!C 

5!ZZ
+ **_5
 	
# as   &G<c                   US   S:X  a!  US   U R                   ;  a  [        US   5      eUS   S:X  a  SU l        gUS   S:X  a  U R                  US   5        gUS   S:X  a  SU l        U R                  US   5        gUS   S	:X  aQ  US
   SSS2    Vs/ s H  n[	        U[
        5      (       a  US   OUPM!     nnU R                  R                  U5        gUS   S;  a>  [        R                  " U5      (       d#  SUS   < S3nUS   S:X  a  US-  n[        U5      eUS   S:X  ax  U R                  U5      (       ab  US
   R                  5        Vs/ s H"  n[	        U[        [        45      (       a  M   UPM$     nnU R                  R                  USSS2   5        gU R                  USS9nU R!                  X5        gs  snf s  snf )a+  This method handles the "middle" stage of recursion over the input schema.
Specifically, it is responsible for handling each choice of the outermost union
(and any "coalesced" choices obtained from inner unions).

Here, "handling" entails:
* Coalescing nested unions and compatible tagged-unions
* Tracking the presence of 'none' and 'nullable' schemas occurring as choices
* Validating that each allowed discriminator value maps to a unique choice
* Updating the _tagged_union_choices mapping that will ultimately be used to build the TaggedUnionSchema.
rG   definition-ref
schema_refnoneTr6   r-   rH   rI   rJ   NrK   r   >   model	dataclass
typed-dicttagged-unionlax-or-strictdataclass-argsrb   The core schema type , is not a valid discriminated union variant.list If you are making use of a list of union types, make sure the discriminator is applied to the union type and not the list (e.g. `list[Annotated[<T> | <U>, Field(discriminator=...)]]`).rh   source_name)r6   r   r<   rW   r2   rT   r>   rU   r   is_function_with_inner_schema	TypeError_is_discriminator_sharedvaluesr   int&_infer_discriminator_values_for_choice_set_unique_choice_for_values)r   r_   r]   r^   err_strx
subchoicesinferred_discriminator_valuess           r   rW   *_ApplyInferredDiscriminator._handle_choice   s    &>--l#4+;+;;26,3GHH&>V#'+D$F^},x 01F^z)'+D$x 01F^w&LRS\L]^b`b^bLcdLcqz!U';';qtBLcOd##**?;F^ $
 
 ;;FCC-fVn-??klGf~'q G$$f~/D4Q4QRX4Y4Y *0	):)A)A)Ce)CA:VWZ]_bYcKda)C
e''..z$B$/?@,0,W,WX^lp,W,q)..vU; e. fs   &GG7Gc                    US   nX R                   :H  =(       d=    [        U[        5      =(       a&    U R                   U;   =(       d    U R                   /U;   $ )a<  This method returns a boolean indicating whether the discriminator for the `choice`
is the same as that being used for the outermost tagged union. This is used to
determine whether this TaggedUnionSchema choice should be "coalesced" into the top level,
or whether it should be treated as a separate (nested) choice.
r.   )r.   r2   rm   )r   r_   inner_discriminators      r   rs   4_ApplyInferredDiscriminator._is_discriminator_shared  sY     %_5"&8&88 
*D1 k##'::it?Q?Q>RVi>i	
r   c           
        US   S:X  a  U R                  US   US9$ [        R                  " U5      (       a  U R                  US   US9$ US   S:X  a9  [        [	        U R                  US   SS9U R                  US   SS9-   5      5      $ US   S	:X  an  / nUS
   R                  5        Vs/ s H"  n[        U[        [        45      (       a  M   UPM$     nnU H$  nU R                  USS9nUR                  U5        M&     U$ US   S:X  aM  / nUS
    H@  n[        U[        5      (       a  US   OUnU R                  USS9nUR                  U5        MB     U$ US   S:X  a  SU l        U R                  US   SS9$ US   S:X  a   U R                  US   US   R                  S9$ US   S:X  a   U R                  US   US   R                  S9$ US   S:X  a  U R                  XS9$ US   S:X  a  U R                  XS9$ US   S:X  a  U R                  XS9$ US   S:X  a<  US   n	XR                   ;  a  [#        U	5      eU R                  U R                   U	   US9$ SUS   < S3n
US   S:X  a  U
S-  n
[%        U
5      es  snf )zThis function recurses over `choice`, extracting all discriminator values that should map to this choice.

`model_name` is accepted for the purpose of producing useful error messages.
rG   r6   r-   ro   ri   
lax_schemaNstrict_schemarh   rJ   rI   r   rH   Tre   clsrf   zmodel-fieldsrj   rg   rb   rc   rk   rl   rm   rn   )rv   r   rq   sortedsetrt   r2   r   ru   rU   rT   r<   r"   ,_infer_discriminator_values_for_model_choice0_infer_discriminator_values_for_dataclass_choice1_infer_discriminator_values_for_typed_dict_choicer6   r   rr   )r   r_   rp   rt   ry   rz   	subchoicesubchoice_valuessubchoice_schemarc   rx   s              r   rv   B_ApplyInferredDiscriminator._infer_discriminator_values_for_choice%  s"    &>]*>>vh?O]h>ii66v>>>>vh?O]h>iiF^.??|@Tbf?gAA&BYgkAlm  F^~-&(F%+I%6%=%=%?a%?zRSVY[^U_G`!%?Ja'	#'#N#Nyfj#N#k ./ ( MF^w&F#I.	3=i3O3O9Q<U^ #'#N#NO_mq#N#r ./ / MF^z)'+D$>>vh?O]a>bbF^w&>>vh?O]cdi]j]s]s>ttF^{*>>vh?O]cdi]j]s]s>ttF^~-DDVDeeF^//HHHiiF^|+II&IjjF^//-J!1!112:>>>>t?O?OPZ?[it>uu-fVn-??klGf~'q G$$[ bs   -I?I?Nc                    Uc  SOSU< 3nUS   R                  U R                  5      nUc  [        U SU R                  < 3SS9eU R                  XC5      $ )zThis method just extracts the _infer_discriminator_values_for_choice logic specific to TypedDictSchema
for the sake of readability.
	TypedDictz
TypedDict fields% needs a discriminator field for key discriminator-no-fieldcoderY   r.   r   %_infer_discriminator_values_for_fieldr   r_   rp   sourcefields        r   r   M_ApplyInferredDiscriminator._infer_discriminator_values_for_typed_dict_choicel  sp     !, 3:k_9Ux $$T%7%78=#(?@R@R?UV]u  99%HHr   c                    Uc  SOSU< 3nUS   R                  U R                  5      nUc  [        U SU R                  < 3SS9eU R                  XC5      $ )NModelFieldszModel r   r   r   r   r   r   s        r   r   H_ApplyInferredDiscriminator._infer_discriminator_values_for_model_choicez  sp     #."5VK?;Sx $$T%7%78=#(?@R@R?UV]u  99%HHr   c                    Uc  SOSU< 3nUS    H  nUS   U R                   :X  d  M    O   [        U SU R                   < 3SS9eU R                  XC5      $ )NDataclassArgsz
Dataclass r   namer   r   r   )r.   r   r   r   s        r   r   L_ApplyInferredDiscriminator._infer_discriminator_values_for_dataclass_choice  sv     %0$7z+=YH%EV} 2 22 & $(?@R@R?UV]u  99%HHr   c           	     \   US   S:X  a  / $ UR                  SU R                  5      n[        U[        5      (       d  [	        SU< S3SS9eU R
                  c  X0l        O9U R
                  U:w  a)  [	        SU R                  < S	U S
U R
                   S3SS9eU R                  US   U5      $ )NrG   zcomputed-fieldvalidation_aliaszAlias z* is not supported in a discriminated unionzdiscriminator-alias-typer   zAliases for discriminator z must be the same (got z, )zdiscriminator-aliasr-   )rY   r.   r2   r   r   r;   ,_infer_discriminator_values_for_inner_schema)r   r   r   aliass       r   r   A_ApplyInferredDiscriminator._infer_discriminator_values_for_field  s    =,,I		,d.@.@A%%%#	!KLSm  $$,(-%&&%/#,T-?-?,B Cwb!:!: ;1>* 
 @@xRXYYr   c                   US   S:X  a  US   $ US   S:X  aN  / nUS    HA  n[        U[        5      (       a  US   OUnU R                  XR5      nUR                  U5        MC     U$ US   S:X  a  U R                  US   U5      $ US   S	:X  a  U R                  US   U5      $ US   S
;   a>  [	        US   R                  S5      S   5      n[        SU SU R                  < SU 3SS9e[        U SU R                  < S3SS9e)zWhen inferring discriminator values for a field, we typically extract the expected values from a literal
schema. This function does that, but also handles nested unions and defaults.
rG   literalexpectedrI   rJ   r   defaultr-   zfunction-after>   function-wrapfunction-plainfunction-before-r   zCannot use a mode=z& validator in the discriminator field z of zdiscriminator-validatorr   z needs field z to be of type `Literal`zdiscriminator-needs-literal)r2   rT   r   rU   reprsplitr   r.   )r   r-   r   rt   r_   choice_schemachoice_valuesvalidator_types           r   r   H_ApplyInferredDiscriminator._infer_discriminator_values_for_inner_schema  sM    &>Y&*%%F^w& !#F +-7-F-Fq	F $ Q QR_ hm, , MF^y(DDVHEUW]^^F^//DDVHEUW]^^F^UU!&."6"6s";A">?N#$^$4 5((,(:(:'=T&K.  $(-(:(:'==UV2 r   c                    U HT  nX0R                   ;   a4  U R                   U   nXA:w  a  [        SU< SU R                  < S35      eMF  XR                   U'   MV     g)zThis method updates `self.tagged_union_choices` so that all provided (discriminator) `values` map to the
provided `choice`, validating that none of these values already map to another (different) choice.
zValue z for discriminator z mapped to multiple choicesN)r?   rr   r.   )r   r_   rt   discriminator_valueexisting_choices        r   rw   9_ApplyInferredDiscriminator._set_unique_choice_for_values  sz     $*"&@&@@ #'"<"<=P"Q",# !4 77J--00KM  - CI**+>? $*r   )r>   r;   r=   r<   r?   r@   r6   r.   )r.   r   r6   z!dict[str, core_schema.CoreSchema])r-   core_schema.CoreSchemar    r   )r_   r   r    r!   )r_   zcore_schema.TaggedUnionSchemar    bool)r_   r   rp   
str | Noner    list[str | int]N)r_   zcore_schema.TypedDictSchemarp   r   r    r   )r_   zcore_schema.ModelFieldsSchemarp   r   r    r   )r_   zcore_schema.DataclassArgsSchemarp   r   r    r   )r   r   r   r   r    r   )r-   r   r   r   r    r   )r_   r   rt   zSequence[str | int]r    r!   )r"   r#   r$   r%   r&   r   r5   rC   rW   rs   rv   r   r   r   r   r   rw   r'    r   r   r4   r4   I   s    
4l<6
p5Vn

E%,E%;EE%	E%P NRI1I@JI	I PT	I3	IBL	I		I RVI5IDNI	IZ&(,(69(	(TIr   r4   )r-   r	   r.   r   r    r!   r   )r-   r   r.   zstr | Discriminatorr6   z(dict[str, core_schema.CoreSchema] | Noner    r   )
__future__r   _annotationscollections.abcr   r   typingr   r   r   pydantic_corer	   r
   errorsr    r   r   r1   r   _core_metadatar   	Exceptionr   r/   r7   r4   r   r   r   <module>r      s    2 . + + 1 &  %,E9 EF =A$W"$W&$W :$W 	$WNVI VIr   