
    h)]                       % S SK Jr  S SKrS SKrS SKrS SKJr  S SKJrJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJr  S S	KJrJrJrJr  S S
KJr  S SKrS SKJr  S SKJr  SSKJr  SSKJr  SSK J!r!  SSK"J#r#J$r$  \RJ                  S:  a  S SKJ&r&  \(       a  SSK'J(r(  \)\\\)\S4   4   r*\" S5      r+\" S5      r,Sr- " S S\.\+\,4   5      r/\\*S4   r0\(       a   " S S\\+\,4   5      r1O " S S\5      r1\" S SS!9r2S"\3S '    " S# S$\Rh                  5      r5          S:S% jr6S;S<S& jjr70 Rq                  5       Rr                  r:S'\3S('   S=S) jr;S>S* jr<S>S+ jr=S?S, jr>S@S- jr?SAS. jr@SBS/ jrA\" S0SS!9rBS1\3S0'   \      SCS2 j5       rCSDS3 jrDSES4 jrE          SFS5 jrF  SG           SHS6 jjrGSIS7 jrHSJS8 jrISKS9 jrJg)L    )annotationsN)ChainMap)IteratorMapping)contextmanager)
ContextVar)zip_longest)prepare_class)TYPE_CHECKING	AnnotatedAnyTypeVar)WeakValueDictionary)typing_objects)is_union_origin   )_typing_extra)get_type_ref)PydanticRecursiveRef)all_identicalis_model_class   
   )_UnionGenericAlias   )	BaseModel.KTVTd   c                  B   ^  \ rS rSr\4SU 4S jjjrSU 4S jjrSrU =r$ )LimitedDict+   c                .   > Xl         [        TU ]	  5         g N)
size_limitsuper__init__)selfr&   	__class__s     N/var/www/html/env/lib/python3.13/site-packages/pydantic/_internal/_generics.pyr(   LimitedDict.__init__,   s    $    c                  > [         TU ]  X5        [        U 5      U R                  :  aP  [        U 5      U R                  -
  U R                  S-  -   n[	        U R                  5       5      S U nU H  nX	 M     g g )Nr   )r'   __setitem__lenr&   listkeys)r)   keyvalueexcess	to_removekr*   s         r+   r/   LimitedDict.__setitem__0   sk    C't9t&Y04??b3HHFTYY[)'62IG  'r-   )r&   )r&   intreturnNoner3   r   r4   r   r:   r;   )	__name__
__module____qualname____firstlineno___LIMITED_DICT_SIZEr(   r/   __static_attributes____classcell__)r*   s   @r+   r"   r"   +   s    );   r-   r"   type[BaseModel]c                      \ rS rSrSrg)DeepChainMap?    N)r=   r>   r?   r@   rB   rH   r-   r+   rF   rF   ?   s    r-   rF   c                  6    \ rS rSrSrSS jrS	S jrS
S jrSrg)rF   D   zVariant of ChainMap that allows direct updates to inner scopes.

Taken from https://docs.python.org/3/library/collections.html#collections.ChainMap,
with some light modifications for this use case.
c                J    U R                    H  nUR                  5         M     g r%   )mapsclear)r)   mappings     r+   rM   DeepChainMap.clearK   s    99 %r-   c                2    U R                    H  nX#U'   M	     g r%   )rL   )r)   r3   r4   rN   s       r+   r/   DeepChainMap.__setitem__O   s    99$ %r-   c                h    SnU R                    H  nX;   d  M
  X1	 SnM     U(       d  [        U5      eg )NFT)rL   KeyError)r)   r3   hitrN   s       r+   __delitem__DeepChainMap.__delitem__S   s8    C99>C % sm# r-   rH   N)r:   r;   r<   )r3   r   r:   r;   )	r=   r>   r?   r@   __doc__rM   r/   rU   rB   rH   r-   r+   rF   rF   D   s    		 	%	$r-   _GENERIC_TYPES_CACHE)defaultz$ContextVar[GenericTypesCache | None]c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg	)
PydanticGenericMetadatar    type[BaseModel] | Noneorigintuple[Any, ...]argsztuple[TypeVar, ...]
parametersrH   N)r=   r>   r?   r@   __annotations__rB   rH   r-   r+   r[   r[   d   s    ""
##r-   r[   c                D   SUR                   0nU4n[        X5      u  pgnUR                  U5        U" U UU4UUUS.SS.UD6n	[        SS9u  pU(       aK  SnU n[        R
                  U	R                      R                  nXLa  UR                  X5      nUS-  nXLa  M  U	$ )	a"  Dynamically create a submodel of a provided (generic) BaseModel.

This is used when producing concrete parametrizations of generic models. This function
only *creates* the new subclass; the schema/validators/serialization must be updated to
reflect a concrete parametrization elsewhere.

Args:
    model_name: The name of the newly created model.
    origin: The base class for the new model to inherit from.
    args: A tuple of generic metadata arguments.
    params: A tuple of generic metadata parameters.

Returns:
    The created submodel.
r>   )r]   r_   r`   F)__pydantic_generic_metadata__#__pydantic_reset_parent_namespace__r   )depthN_)r>   r
   update_get_caller_frame_infosysmodules__dict__
setdefault)
model_namer]   r_   params	namespacebasesmetanskwdscreated_modelmodel_modulecalled_globallyobject_by_referencereference_namereference_module_globalss                  r+   create_generic_submodelrz   j   s    $ ".v/@/@ AIIE":5NDdR
  '

 -2 M %;$C!L"##&;;}/G/G#H#Q#Q !6":"E"En"dc!N "6 r-   c                     [         R                  " U 5      nUR
                  nUR                  S5      UR                  UL 4$ ! [         a  n[        S5      UeSnAf[         a     gf = f)zUsed inside a function to check whether it was called globally.

Args:
    depth: The depth to get the frame.

Returns:
    A tuple contains `module_name` and `called_globally`.

Raises:
    RuntimeError: If the function is not called inside a function.
z2This function must be used inside another functionN)NFr=   )ri   	_getframe
ValueErrorRuntimeErrorAttributeError	f_globalsgetf_locals)re   previous_caller_frameeframe_globalss       r+   rh   rh      sr     #e 4
 *33MZ(*?*H*HM*YYY  XOPVWW s   A 
A)AA)(A)z	type[Any]
DictValuesc              #  Z  #    [        U [        5      (       a  U v   g[        U 5      (       a  U R                  S    Sh  vN   g[        U [        [
        45      (       a  U  H  n[        U5       Sh  vN   M     g[        U 5      nU H  n[        U5       Sh  vN   M     g Ne N3 N7f)a+  Recursively iterate through all subtypes and type args of `v` and yield any typevars that are found.

This is inspired as an alternative to directly accessing the `__parameters__` attribute of a GenericAlias,
since __parameters__ of (nested) generic BaseModel subclasses won't show up in that list.
r`   N)
isinstancer   r   rc   r   r1   iter_contained_typevarsget_args)vvarr_   args       r+   r   r      s      !W			22<@@@	A
D)	*	*C.s333  {C.s333  	A 4 4s4   =B+B% 3B+3B'4'B+B)
B+'B+)B+c                x    [        U SS 5      nU(       a  UR                  S5      $ [        R                  " U 5      $ )Nrc   r_   )getattrr   typing_extensionsr   r   pydantic_generic_metadatas     r+   r   r      s8    @GKjlp@q (,,V44%%a((r-   c                x    [        U SS 5      nU(       a  UR                  S5      $ [        R                  " U 5      $ )Nrc   r]   )r   r   r   
get_originr   s     r+   r   r      s8    @GKjlp@q (,,X66''**r-   c                    [        U 5      nUc  g[        US5      (       d  gU R                  nUR                  n[	        [        X25      5      $ )zPackage a generic type's typevars and parametrization (if present) into a dictionary compatible with the
`replace_types` function. Specifically, this works with standard typing generics and typing._GenericAlias.
N__parameters__)r   hasattr__args__r   dictzip)clsr]   r_   r`   s       r+   get_standard_typevars_mapr      sL     _F~6+,,  LLD&,&;&;JJ%&&r-   c                |    U R                   nUS   nUS   nU(       d  0 $ [        [        [        U5      U5      5      $ )aU  Package a generic BaseModel's typevars and concrete parametrization (if present) into a dictionary compatible
with the `replace_types` function.

Since BaseModel.__class_getitem__ does not produce a typing._GenericAlias, and the BaseModel generic info is
stored in the __pydantic_generic_metadata__ attribute, we need special handling here.
r]   r_   )rc   r   r   r   )r   generic_metadatar]   r_   s       r+   get_model_typevars_mapr      sD     88h'FF#D	+F3T:;;r-   c                n  ^ T(       d  U $ [        U 5      n[        U 5      n[        R                  " U5      (       a  UtpE[	        UT5      n[
        U/UQ7   $ U(       Ga"  [        U4S jU 5       5      n[        X&5      (       a  U $ Ubf  [        U [        R                  5      (       aG  [        U[        R                  5      (       d(  [        U SS5      b  [        [        U R                  5      nUc   e[        U5      (       a0  [        S U 5       5      (       a  [         4n[        S U 5       5      n["        R$                  S:  a  U[&        R(                  L a  [+        X65      $ U[-        U5      S:X  a  US      $ U   $ U(       dS  [/        U 5      (       aC  U R0                  S	   nU(       d  U $ [        U4S
 jU 5       5      n[        Xv5      (       a  U $ X   $ [        U [2        5      (       a0  U  Vs/ s H  n[	        UT5      PM     n	n[        X	5      (       a  U $ U	$ TR5                  X 5      $ s  snf )a4  Return type with all occurrences of `type_map` keys recursively replaced with their values.

Args:
    type_: The class or generic alias.
    type_map: Mapping from `TypeVar` instance to concrete types.

Returns:
    A new type representing the basic structure of `type_` with all
    `typevar_map` keys recursively replaced.

Example:
    ```python
    from typing import List, Union

    from pydantic._internal._generics import replace_types

    replace_types(tuple[str, Union[List[str], float]], {str: int})
    #> tuple[int, Union[List[int], float]]
    ```
c              3  <   >#    U  H  n[        UT5      v   M     g 7fr%   replace_types).0r   type_maps     r+   	<genexpr> replace_types.<locals>.<genexpr>  s     "U9C=h#?#?9   N_namec              3  N   #    U  H  n[         R                  " U5      v   M     g 7fr%   )r   is_anyr   r   s     r+   r   r   -  s      L9K#>((--9Ks   #%c              3     #    U  HA  n[         R                  " U5      (       a  M   [         R                  " U5      (       a  M=  Uv   MC     g 7fr%   )r   is_noreturnis_neverr   s     r+   r   r   1  s;      '-C&2237 ;I;R;RSV;W -s   AA	Ar   r   r   r`   c              3  <   >#    U  H  n[        UT5      v   M     g 7fr%   r   )r   tr   s     r+   r   r   E  s     "Rz!=H#=#=zr   )r   r   r   is_annotatedr   r   tupler   r   r   typing_baser   typingr   r   anyr   ri   version_infotypes	UnionTyper   r0   r   rc   r1   r   )
type_r   	type_argsorigin_typeannotated_typer   resolved_type_argsr`   elementresolved_lists
    `        r+   r   r      s   * IU#K"";//'0$&~x@.7;788 ""U9"UU77 L #5-";";<<{M,E,EFFw-9
 "&%++6K&&&;''L9KLLL&)V"!& '-' " w&;%//+I%kFFC8J4Kq4P-a0iiVhii
 >%0088F
L""Rz"RR88L(( %INOgw9O..L <<%% Ps   5H2c           	     "   U R                   S   n[        U5      n0 n[        5       n[        X!US9 H  u  pgXeL a  [	        SU  S[        U5       SU 35      eXuL a  [
        R                  " [        U5      n UR                  5       n	U	(       a  [        UR                  U5      XH'   Mw  U[        S U 5       5      -  n[	        SU  S[        U5       S	U 35      e[
        R                  " [        U5      nXtU'   M     U$ ! [         a    Sn	 Nf = f)
aF  Return a mapping between the parameters of a generic model and the provided arguments during parameterization.

Raises:
    TypeError: If the number of arguments does not match the parameters (i.e. if providing too few or too many arguments).

Example:
    ```python {test="skip" lint="skip"}
    class Model[T, U, V = int](BaseModel): ...

    map_generic_model_arguments(Model, (str, bytes))
    #> {T: str, U: bytes, V: int}

    map_generic_model_arguments(Model, (str,))
    #> TypeError: Too few arguments for <class '__main__.Model'>; actual 1, expected at least 2

    map_generic_model_arguments(Model, (str, bytes, int, complex))
    #> TypeError: Too many arguments for <class '__main__.Model'>; actual 4, expected 3
    ```

Note:
    This function is analogous to the private `typing._check_generic_specialization` function.
r`   )	fillvaluezToo many arguments for z	; actual z, expected Fc              3  h   #    U  H(  n[        US 5      =(       a    UR                  5       v   M*     g7f)has_defaultN)r   r   )r   ps     r+   r   .map_generic_model_arguments.<locals>.<genexpr>  s(     #f[eVWGA}$=$Q!--/$Q[es   02zToo few arguments for z, expected at least )rc   r0   objectr	   	TypeErrorr   castr   r   r   r   __default__sum)
r   r_   r`   expected_lentypevars_map_missing	parameterargumentparamr   s
             r+   map_generic_model_argumentsr   W  s    . 22<@Jz?L')LxH*:xP	 5cU)CI;kZfYghiiKK3E$#//1  '4E4E4E|&T##f[e#f ff"8Ys4ykQefres tuuKK3E"*)  Q,  " $#$s   7C??DD_generic_recursion_cachezContextVar[set[str] | None]c              #    #    [         R                  5       nUc   [        5       n[         R                  U5      nOSn [        XS9nXB;   a  [	        US9nUv   O&UR                  U5        Sv   UR                  U5        U(       a  [         R                  U5        gg! U(       a  [         R                  U5        f f = f7f)a  This contextmanager should be placed around the recursive calls used to build a generic type,
and accept as arguments the generic origin type and the type arguments being passed to it.

If the same origin and arguments are observed twice, it implies that a self-reference placeholder
can be used while building the core schema, and will produce a schema_ref that will be valid in the
final parent schema.
N)args_override)type_ref)r   r   setr   r   addremovereset)r]   r_   previously_seen_type_refstokenr   	self_types         r+   generic_recursion_self_typer     s      !9 < < > ($'E!(,,-FG2;0,h?IO%))(3%,,X6$**51 5$**51 s   :B?AB ?B?B<<B?c                 l    [         R                  5       n U (       d
  [        5       $ U R                  5       $ r%   )r   r   r   copy)visiteds    r+   recursively_defined_type_refsr     s&    &**,Gu<<>r-   c                    [         R                  5       nUc  [        5       n[         R                  U5        UR                  [	        X5      5      $ )a  The use of a two-stage cache lookup approach was necessary to have the highest performance possible for
repeated calls to `__class_getitem__` on generic types (which may happen in tighter loops during runtime),
while still ensuring that certain alternative parametrizations ultimately resolve to the same type.

As a concrete example, this approach was necessary to make Model[List[T]][int] equal to Model[List[int]].
The approach could be modified to not use two different cache keys at different points, but the
_early_cache_key is optimized to be as quick to compute as possible (for repeated-access speed), and the
_late_cache_key is optimized to be as "correct" as possible, so that two types that will ultimately be the
same after resolving the type arguments will always produce cache hits.

If we wanted to move to only using a single cache key per type, we would either need to always use the
slower/more computationally intensive logic associated with _late_cache_key, or would need to accept
that Model[List[T]][int] is a different type than Model[List[T]][int]. Because we rely on subclass relationships
during validation, I think it is worthwhile to ensure that types that are functionally equivalent are actually
equal.
)rX   r   GenericTypesCacher   _early_cache_key)parenttypevar_valuesgeneric_types_caches      r+   get_cached_generic_type_earlyr     sH    " /224"/1  !45""#3F#KLLr-   c                    [         R                  5       nUc  [        5       n[         R                  U5        UR                  [	        X#U5      5      nUb  [        XXRU5        U$ )zkSee the docstring of `get_cached_generic_type_early` for more information about the two-stage cache lookup.)rX   r   r   r   _late_cache_keyset_cached_generic_type)r   r   r]   r_   r   cacheds         r+   get_cached_generic_type_later     s_     /224#/1  !45 $$_V>%RSFMMr-   c                   [         R                  5       nUc  [        5       n[         R                  U5        X%[	        X5      '   [        U5      S:X  a  X%[	        XS   5      '   U(       a  U(       a  X%[        X4U5      '   ggg)z~See the docstring of `get_cached_generic_type_early` for more information about why items are cached with
two different keys.
Nr   r   )rX   r   r   r   r   r0   r   )r   r   r   r]   r_   r   s         r+   r   r     sz     /224#/1  !45DI(@A
>aKP,VA5FGH$MROF.IJ vr-   c                   [        U [        5      (       a0  / nU  H  nUR                  [        U5      5        M     [        U5      $ [        R
                  " [        R                  " U 5      5      (       a  [        U 5      $ g)ak  This is intended to help differentiate between Union types with the same arguments in different order.

Thanks to caching internal to the `typing` module, it is not possible to distinguish between
List[Union[int, float]] and List[Union[float, int]] (and similarly for other "parent" origins besides List)
because `typing` considers Union[int, float] to be equal to Union[float, int].

However, you _can_ distinguish between (top-level) Union[int, float] vs. Union[float, int].
Because we parse items as the first Union type that is successful, we get slightly more consistent behavior
if we make an effort to distinguish the ordering of items in a union. It would be best if we could _always_
get the exact-correct order of items in the union, but that would require a change to the `typing` module itself.
(See https://github.com/python/cpython/issues/86483 for reference.)
rH   )	r   r   append_union_orderings_keyr   is_unionr   r   r   )r   	args_datar4   s      r+   r   r     sj     .%((	#E1%89 $Y		 	 !2!=!=n!M	N	N''r-   c                    X[        U5      4$ )a  This is intended for minimal computational overhead during lookups of cached types.

Note that this is overly simplistic, and it's possible that two different cls/typevar_values
inputs would ultimately result in the same type being created in BaseModel.__class_getitem__.
To handle this, we have a fallback _late_cache_key that is checked later if the _early_cache_key
lookup fails, and should result in a cache hit _precisely_ when the inputs to __class_getitem__
would result in the same type.
r   )r   r   s     r+   r   r     s      4^ DDDr-   c                    [        U5      X4$ )aq  This is intended for use later in the process of creating a new type, when we have more information
about the exact args that will be passed. If it turns out that a different set of inputs to
__class_getitem__ resulted in the same inputs to the generic type creation process, we can still
return the cached type, and update the cache with the _early_cache_key as well.
r   )r]   r_   r   s      r+   r   r     s      /==r-   )
rm   strr]   rD   r_   r^   rn   r^   r:   rD   )r   )re   r9   r:   ztuple[str | None, bool])r   r   r:   zIterator[TypeVar])r   r   r:   r   )r   r   r:   zdict[TypeVar, Any] | None)r   rD   r:   dict[TypeVar, Any])r   r   r   zMapping[TypeVar, Any] | Noner:   r   )r   rD   r_   r^   r:   r   )r]   rD   r_   r^   r:   z%Iterator[PydanticRecursiveRef | None])r:   zset[str])r   rD   r   r   r:   r\   )
r   rD   r   r   r]   rD   r_   r^   r:   r\   )NN)r   rD   r   r^   r   rD   r]   r\   r_   ztuple[Any, ...] | Noner:   r;   )r   r   r:   r   )r   rD   r   r   r:   GenericTypesCacheKey)r]   rD   r_   r^   r   r   r:   r   )K
__future__r   ri   r   r   collectionsr   collections.abcr   r   
contextlibr   contextvarsr   	itertoolsr	   r
   r   r   r   r   weakrefr   r   typing_inspectionr   typing_inspection.introspectionr    r   _core_utilsr   _forward_refr   _utilsr   r   r   r   mainr   r   r   r   r   rA   r   r"   r   rF   rX   ra   	TypedDictr[   rz   rh   valuesr*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   r-   r+   <module>r     sM   " 
     - % " !  9 9 '  , ;  % . 1w) S#uS#X67  T]T] $r2v,   ((<>O(OP xB' 
$x $: >HH^hl=m : m$/99 $,,,,4C,M\,,^Z, 		--
I -4&)+'"<$^&B2j 9CC]gk8l 5 l 22#22*2 2>M0-0:IQ`( &*#'SS#S S #	S
 !S 
S.0	E	>r-   