o
    í6Wj¥E  ã                
   @   sR  U d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ d dlmZmZ d dlmZmZ d dlmZ d dlmZ d dlm Z m!Z! d dl"m#Z# d d	l$m%Z%m&Z&m'Z'm(Z(m)Z) d d
l*m+Z+m,Z, ej-dkr‚d dlmZ eddd�Z.eZ/eee eeedf f Z0ee/ee f Z1ej-dkrµee0ee  f Z2eee  e1f Z3neZ2eZ3e2ƒ Z4e3ƒ Z5G dd„ de ƒZ6dedeeef defdd„Z7dee6 deedf ddfdd„Z8i  9¡ j:Z;ee e<d< dedee/ fdd„Z=deee> e?f fd d!„Z@d"ee6 d#ee>ef d$ee>eAf d%eeeAf ddf
d&d'„ZBdS )(é    N)ÚTYPE_CHECKINGÚAnyÚClassVarÚDictÚ
ForwardRefÚGenericÚIteratorÚListÚMappingÚOptionalÚTupleÚTypeÚTypeVarÚUnionÚcast)ÚWeakKeyDictionaryÚWeakValueDictionary)Ú	AnnotatedÚLiteral)Úgather_all_validators)ÚDeferredType)Ú	BaseModelÚcreate_model)ÚJsonWrapper)Údisplay_as_typeÚget_all_type_hintsÚget_argsÚ
get_originÚtyping_base)Úall_identicalÚlenient_issubclass©é   é   )r   ÚGenericModelTÚGenericModel)Úbound.)r"   é	   c                   @   s¸   e Zd ZU dZdZee ed< eree	e
df  ed< dee deee e	ee df f dee fd	d
„Zedee de	ee df defdd„ƒZededeee  fdd„ƒZdS )r%   © FÚ__concrete__.Ú__parameters__ÚclsÚparamsÚreturnc              
      s  dt dtf‡ fdd„}t ||ƒ¡}|dur|S ˆ jr$tˆ jvr$tdƒ‚t|t	ƒs,|f}ˆ t
u r=tdd„ |D ƒƒr=td	ƒ‚tˆ d
ƒsKtdˆ j› d�ƒ‚tˆ |ƒ ttˆ j|ƒƒ}t| ¡ | ¡ ƒre|reˆ S ˆ  |¡}tˆ ƒ}tˆ ƒ ¡ }dd„ |D ƒ}‡ fdd„|D ƒ}	tƒ \}
}ttt
 t|f|
p”ˆ jˆ ft	ˆ  |¡ƒ d|ddœ|	¤Žƒ}|t|< |rÊd}|}t j!|j j"}||urÊ| #||¡}|d7 }||us¼ˆ j$|_$t	dd„ t%| ¡ ƒD ƒƒ}| |_|rä||_|t||ƒ< t&|ƒdkrø|t||d ƒ< t'||	||ƒ |S )aè  Instantiates a new class from a generic class `cls` and type variables `params`.

        :param params: Tuple of types the class . Given a generic class
            `Model` with 2 type variables and a concrete model `Model[str, int]`,
            the value `(str, int)` would be passed to `params`.
        :return: New model class inheriting from `cls` with instantiated
            types described by `params`. If no parameters are given, `cls` is
            returned as is.

        Ú_paramsr-   c                    s@   t | ƒ}t|ƒdkrt|d tƒrt|d ƒ|d f}ˆ | |fS )Né   r   é   )r   ÚlenÚ
isinstanceÚlistÚtuple)r.   Úargs©r+   r(   úc/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/pydantic/v1/generics.pyÚ
_cache_keyW   s   
z2GenericModel.__class_getitem__.<locals>._cache_keyNz?Cannot parameterize a concrete instantiation of a generic modelc                 s   s   � | ]}t |tƒV  qd S ©N)r2   r   ©Ú.0Úparamr(   r(   r7   Ú	<genexpr>e   ó   € z1GenericModel.__class_getitem__.<locals>.<genexpr>zDType parameters should be placed on typing.Generic, not GenericModelr*   zType z< must inherit from typing.Generic before being parameterizedc                 S   s"   i | ]\}}t |ƒtur||“qS r(   )r   r   )r;   ÚkÚvr(   r(   r7   Ú
<dictcomp>u   s   " z2GenericModel.__class_getitem__.<locals>.<dictcomp>c                    s*   i | ]}|ˆ j v r|tƒ ˆ j | jf“qS r(   )Ú
__fields__r   Ú
field_info)r;   r?   r6   r(   r7   rA   w   s   * )Ú
__module__Ú__base__Ú
__config__Ú__validators__Ú__cls_kwargs__Ú_c                 S   s   i | ]}|d “qS r9   r(   r:   r(   r(   r7   rA   ˜   s    r0   r   )(r   ÚCacheKeyÚ_generic_types_cacheÚgetr)   r   Ú	__bases__Ú	TypeErrorr2   r4   r%   ÚanyÚhasattrÚ__name__Úcheck_parameters_countÚdictÚzipr*   r   ÚkeysÚvaluesÚ__concrete_name__r   r   ÚitemsÚget_caller_frame_infor   r   r   rD   Ú__parameterized_bases__Ú_assigned_parametersÚsysÚmodulesÚ__dict__Ú
setdefaultÚConfigÚiter_contained_typevarsr1   Ú_prepare_model_fields)r+   r,   r8   ÚcachedÚtypevars_mapÚ
model_nameÚ
validatorsÚ
type_hintsÚinstance_type_hintsÚfieldsÚmodel_moduleÚcalled_globallyÚcreated_modelÚobject_by_referenceÚreference_nameÚreference_module_globalsÚ
new_paramsr(   r6   r7   Ú__class_getitem__K   sr   




ÿúùþþÿzGenericModel.__class_getitem__c                 C   s*   dd„ |D ƒ}d  |¡}| j› d|› d�S )aÜ  Compute class name for child classes.

        :param params: Tuple of types the class . Given a generic class
            `Model` with 2 type variables and a concrete model `Model[str, int]`,
            the value `(str, int)` would be passed to `params`.
        :return: String representing a the new class where `params` are
            passed to `cls` as type variables.

        This method can be overridden to achieve a custom naming scheme for GenericModels.
        c                 S   s   g | ]}t |ƒ‘qS r(   )r   r:   r(   r(   r7   Ú
<listcomp>¶   s    z2GenericModel.__concrete_name__.<locals>.<listcomp>z, ú[ú])ÚjoinrQ   )r+   r,   Úparam_namesÚparams_componentr(   r(   r7   rW   ª   s   
zGenericModel.__concrete_name__rd   c                 #   s    � dt t dtdtt t  f‡ fdd„}ˆ jD ]6}t|tƒsqt|ddƒs&qˆ tv rE|tv r/q‡fdd	„tˆ   ¡ D ƒ}|||ƒE dH  q||ˆƒE dH  qdS )
aÍ  
        Returns unbound bases of cls parameterised to given type variables

        :param typevars_map: Dictionary of type applications for binding subclasses.
            Given a generic class `Model` with 2 type variables [S, T]
            and a concrete model `Model[str, int]`,
            the value `{S: str, T: int}` would be passed to `typevars_map`.
        :return: an iterator of generic sub classes, parameterised by `typevars_map`
            and other assigned parameters of `cls`

        e.g.:
        ```
        class A(GenericModel, Generic[T]):
            ...

        class B(A[V], Generic[V]):
            ...

        assert A[int] in B.__parameterized_bases__({V: int})
        ```
        Ú
base_modelÚmapped_typesr-   c                 3   sB   � t ‡ fdd„| jD ƒƒ}|  |¡}|| u s|ˆu rd S |V  d S )Nc                 3   s   � | ]}ˆ | V  qd S r9   r(   r:   ©ry   r(   r7   r=   Õ   s   € zQGenericModel.__parameterized_bases__.<locals>.build_base_model.<locals>.<genexpr>)r4   r*   rq   )rx   ry   Úbase_parametersÚparameterized_baser6   rz   r7   Úbuild_base_modelÒ   s   €

z>GenericModel.__parameterized_bases__.<locals>.build_base_modelr*   Nc                    s   i | ]\}}|ˆ   ||¡“qS r(   )rL   )r;   ÚkeyÚvalue)rd   r(   r7   rA   ñ   s    ÿz8GenericModel.__parameterized_bases__.<locals>.<dictcomp>)	r   r%   ÚParametrizationr   rM   Ú
issubclassÚgetattrr[   rX   )r+   rd   r}   rx   ry   r(   )r+   rd   r7   rZ   º   s,   €ÿÿ
þ




ÿäz$GenericModel.__parameterized_bases__N)rQ   rD   Ú__qualname__Ú	__slots__r)   r   ÚboolÚ__annotations__r   r   ÚTypeVarTyper   r$   r   r   rq   ÚclassmethodÚstrrW   r€   r   rZ   r(   r(   r(   r7   r%   @   s   
 6_( Útype_Útype_mapr-   c           	         s¸  ˆ s| S t | ƒ}t| ƒ}|tu r|^}}tt|ˆ ƒt|ƒf S |tu s,tjdkr2|tu r2ˆ  	| | ¡S |r}t‡ fdd„|D ƒƒ}t
||ƒrF| S |durbt| tƒrbt|tƒsbt| ddƒdurbtt| jƒ}|dushJ ‚tjdkry|tju ryt tj|¡S || S |s t| tƒr | js | j}t‡ fdd„|D ƒƒ}t
||ƒrœ| S | | S t| ttfƒr»t‡ fdd„| D ƒƒ}t
| |ƒr¹| S |S |sËt| tƒrËt| jˆ ƒ| _| S ˆ  	| | ¡}t|tƒrÚt|ƒS |S )	aÉ  Return type with all occurrences of `type_map` keys recursively replaced with their values.

    :param type_: Any type, class or generic alias
    :param type_map: Mapping from `TypeVar` instance to concrete types.
    :return: New type representing the basic structure of `type_` with all
        `typevar_map` keys recursively replaced.

    >>> replace_types(Tuple[str, Union[List[str], float]], {str: int})
    Tuple[int, Union[List[int], float]]

    r!   c                 3   ó   � | ]}t |ˆ ƒV  qd S r9   ©Úreplace_types)r;   Úarg©r‹   r(   r7   r=     r>   z replace_types.<locals>.<genexpr>NÚ_name)r"   é
   c                 3   rŒ   r9   r�   )r;   Útr�   r(   r7   r=   0  r>   c                 3   rŒ   r9   r�   )r;   Úelementr�   r(   r7   r=   8  r>   ) r   r   r   rŽ   r4   Ú
ExtLiteralr\   Úversion_infor   rL   r   r2   r   r‚   Útypingr‘   ÚtypesÚ	UnionTypeÚ	functoolsÚreduceÚoperatorÚor_r    r%   r)   r*   r	   r3   r   Ú
inner_typer‰   r   )	rŠ   r‹   Ú	type_argsÚorigin_typeÚannotated_typeÚannotationsÚresolved_type_argsÚresolved_listÚnew_typer(   r�   r7   rŽ   û   sT   
ÿþ


rŽ   r+   Ú
parametersc              	   C   sP   t |ƒ}t | jƒ}||kr&||krdnd}td|› d| j› d|› d|› �ƒ‚d S )NÚmanyÚfewzToo z parameters for z	; actual z, expected )r1   r*   rN   rQ   )r+   r¦   ÚactualÚexpectedÚdescriptionr(   r(   r7   rR   M  s   
"þrR   Ú
DictValuesr@   c                 c   s�   � t | tƒr| V  dS t| dƒr!t| ƒs!t| tƒr!| jE dH  dS t | ttfƒr6| D ]	}t	|ƒE dH  q*dS t
| ƒ}|D ]	}t	|ƒE dH  q<dS )zdRecursively iterate through all subtypes and type args of `v` and yield any typevars that are found.r*   N)r2   r   rP   r   r    r%   r*   r¬   r3   ra   r   )r@   Úvarr5   r�   r(   r(   r7   ra   X  s   €

ÿÿra   c               
   C   s^   zt  d¡} W n ty } ztdƒ|‚d}~w ty!   Y dS w | j}| d¡| j|u fS )zÒ
    Used inside a function to check whether it was called globally

    Will only work against non-compiled code, therefore used only in pydantic.generics

    :returns Tuple[module_name, called_globally]
    r/   z2This function must be used inside another functionN)NFrQ   )r\   Ú	_getframeÚ
ValueErrorÚRuntimeErrorÚAttributeErrorÚ	f_globalsrL   Úf_locals)Úprevious_caller_frameÚeÚframe_globalsr(   r(   r7   rY   g  s   
€ÿrY   rl   ri   rh   rd   c                 C   sz   | j  ¡ D ]5\}}||vr|jjtusJ ‚q|jjtu s"J |jjƒ‚|| }t||ƒ}||_||_| ¡  || j|< qdS )zP
    Replace DeferredType fields with concrete type hints and prepare them.
    N)	rB   rX   rŠ   Ú	__class__r   rŽ   Úouter_type_Úpreparer†   )rl   ri   rh   rd   r~   ÚfieldÚfield_type_hintÚconcrete_typer(   r(   r7   rb   y  s   

órb   )Crš   rœ   r\   r˜   r—   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   Úweakrefr   r   Útyping_extensionsr   r   r•   Úpydantic.v1.class_validatorsr   Úpydantic.v1.fieldsr   Úpydantic.v1.mainr   r   Úpydantic.v1.typesr   Úpydantic.v1.typingr   r   r   r   r   Úpydantic.v1.utilsr   r    r–   r$   r‡   rJ   r€   ÚGenericTypesCacheÚAssignedParametersrK   r[   r%   rŽ   rR   rV   r·   r¬   r†   ra   r‰   r…   rY   Útyperb   r(   r(   r(   r7   Ú<module>   sZ   
 D

 <"Rÿ
þ
ý
üû