a
    “˜ëj¥E  ã                   @   sD  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�rnee0ee  f Z2eee  e1f Z3neZ2eZ3e2ƒ Z4e3ƒ Z5G dd„ de ƒZ6eeeef edœdd„Z7ee6 eedf ddœdd„Z8i  9¡ j:Z;ee e<d< eee/ dœdd„Z=eee> e?f dœdd„Z@ee6 ee>ef ee>eAf eeeAf dd œ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r6ee	e
df  ed< ee eee e	ee df f ee dœdd„Zeee e	ee df edœd	d
„ƒZeeeee  dœdd„ƒZdS )r%   © FÚ__concrete__.Ú__parameters__)ÚclsÚparamsÚreturnc              
      s  t tdœ‡ fdd„}t ||ƒ¡}|dur.|S ˆ jrFtˆ jvrFtdƒ‚t|t	ƒsV|f}ˆ t
u rxtdd„ |D ƒƒrxtdƒ‚tˆ d	ƒs”td
ˆ j› d�ƒ‚tˆ |ƒ ttˆ j|ƒƒ}t| ¡ | ¡ ƒrÈ|rÈˆ 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 }�qrˆ 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r6t|d tƒr6t|d ƒ|d f}ˆ | |fS )Né   r   é   )r   ÚlenÚ
isinstanceÚlistÚtuple)r.   Úargs©r+   r(   úS/mnt/bigstor/www/usecases/.venv/lib/python3.9/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   r>   z2GenericModel.__class_getitem__.<locals>.<dictcomp>c                    s*   i | ]"}|ˆ j v r|tƒ ˆ j | jf“qS r(   )Ú
__fields__r   Z
field_info)r;   r?   r6   r(   r7   rA   w   r>   )Ú
__module__Ú__base__Z
__config__Z__validators__Z__cls_kwargs__Ú_c                 S   s   i | ]
}|d “qS r9   r(   r:   r(   r(   r7   rA   ˜   r>   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   rC   Ú__parameterized_bases__Ú_assigned_parametersÚsysÚmodulesÚ__dict__Ú
setdefaultZConfigÚiter_contained_typevarsr1   Ú_prepare_model_fields)r+   r,   r8   ÚcachedÚtypevars_mapZ
model_nameZ
validatorsZ
type_hintsÚinstance_type_hintsÚfieldsZmodel_moduleZcalled_globallyÚcreated_modelZobject_by_referenceZreference_nameZreference_module_globalsÚ
new_paramsr(   r6   r7   Ú__class_getitem__K   sp    




ÿ
úùþ
ÿ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>¶   r>   z2GenericModel.__concrete_name__.<locals>.<listcomp>z, ú[ú])ÚjoinrM   )r+   r,   Zparam_namesZparams_componentr(   r(   r7   rS   ª   s    
zGenericModel.__concrete_name__)r_   r-   c                 #   s    t t ttt t  dœ‡ fdd„}ˆ jD ]r}t|tƒs:q(q(t|ddƒsJq(q(ˆ tv rŠ|tv r^q(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   s@   t ‡ fdd„| jD ƒƒ}|  |¡}|| u s2|ˆu r6d S |V  d S )Nc                 3   s   | ]}ˆ | V  qd S r9   r(   r:   ©rj   r(   r7   r=   Õ   r>   zQGenericModel.__parameterized_bases__.<locals>.build_base_model.<locals>.<genexpr>)r4   r*   rd   )ri   rj   Zbase_parametersZparameterized_baser6   rk   r7   Úbuild_base_modelÒ   s
    
z>GenericModel.__parameterized_bases__.<locals>.build_base_modelr*   Nc                    s   i | ]\}}|ˆ   ||¡“qS r(   )rH   )r;   ÚkeyÚvalue)r_   r(   r7   rA   ñ   s   z8GenericModel.__parameterized_bases__.<locals>.<dictcomp>)	r   r%   ÚParametrizationr   rI   Ú
issubclassÚgetattrrW   rT   )r+   r_   rl   ri   rj   r(   )r+   r_   r7   rV   º   s     
þ




ÿz$GenericModel.__parameterized_bases__N)rM   rC   Ú__qualname__Ú	__slots__r)   r   ÚboolÚ__annotations__r   r   ÚTypeVarTyper   r$   r   r   rd   ÚclassmethodÚstrrS   ro   r   rV   r(   r(   r(   r7   r%   @   s   
2_$)Útype_Útype_mapr-   c           	         sÎ  ˆ s| S t | ƒ}t| ƒ}|tu r>|^}}tt|ˆ ƒt|ƒf S |tu sXtjdkrd|tu rdˆ  	| | ¡S |rút‡ fdd„|D ƒƒ}t
||ƒrŒ| S |durÄt| tƒrÄt|tƒsÄt| ddƒdurÄtt| jƒ}|dusÐJ ‚tjdkrò|tju ròt tj|¡S || S |�sHt| tƒ�rH| j�sH| 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 d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   s   | ]}t |ˆ ƒV  qd S r9   ©Úreplace_types)r;   Úarg©rz   r(   r7   r=     r>   z replace_types.<locals>.<genexpr>NÚ_name)r"   é
   c                 3   s   | ]}t |ˆ ƒV  qd S r9   r{   )r;   Útr~   r(   r7   r=   0  r>   c                 3   s   | ]}t |ˆ ƒV  qd S r9   r{   )r;   Úelementr~   r(   r7   r=   8  r>   ) r   r   r   r|   r4   Ú
ExtLiteralrX   Úversion_infor   rH   r   r2   r   rq   Útypingr   ÚtypesZ	UnionTypeÚ	functoolsÚreduceÚoperatorÚor_r    r%   r)   r*   r	   r3   r   Z
inner_typerx   r   )	ry   rz   Z	type_argsZorigin_typeÚannotated_typeÚannotationsZresolved_type_argsZresolved_listÚnew_typer(   r~   r7   r|   û   sX    
ÿþýü	r|   )r+   Ú
parametersr-   c              	   C   sP   t |ƒ}t | jƒ}||krL||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*   rJ   rM   )r+   rŽ   ÚactualÚexpectedÚdescriptionr(   r(   r7   rN   M  s
    
rN   Ú
DictValues)r@   r-   c                 c   sˆ   t | tƒr| V  nrt| dƒr<t| ƒs<t| tƒr<| jE dH  nHt | ttfƒrd| D ]}t	|ƒE dH  qNn t
| ƒ}|D ]}t	|ƒE dH  qpdS )zdRecursively iterate through all subtypes and type args of `v` and yield any typevars that are found.r*   N)r2   r   rL   r   r    r%   r*   r”   r3   r\   r   )r@   Úvarr5   r}   r(   r(   r7   r\   X  s    
r\   )r-   c               
   C   sj   zt  d¡} W n@ ty< } ztdƒ|‚W Y d}~nd}~0  tyN   Y dS 0 | 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)NFrM   )rX   Ú	_getframeÚ
ValueErrorÚRuntimeErrorÚAttributeErrorÚ	f_globalsrH   Úf_locals)Zprevious_caller_frameÚeZframe_globalsr(   r(   r7   rU   g  s    rU   )rb   ra   r`   r_   r-   c                 C   sz   | j  ¡ D ]j\}}||vr,|jjtus
J ‚q
|jjtu sDJ |jjƒ‚|| }t||ƒ}||_||_| ¡  || j|< q
dS )zP
    Replace DeferredType fields with concrete type hints and prepare them.
    N)	rB   rT   ry   Ú	__class__r   r|   Zouter_type_Úprepareru   )rb   ra   r`   r_   rm   ÚfieldZfield_type_hintZconcrete_typer(   r(   r7   r]   y  s    

r]   )Cr‡   r‰   rX   r†   r…   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   Úweakrefr   r   Ztyping_extensionsr   r   rƒ   Zpydantic.v1.class_validatorsr   Zpydantic.v1.fieldsr   Zpydantic.v1.mainr   r   Zpydantic.v1.typesr   Zpydantic.v1.typingr   r   r   r   r   Zpydantic.v1.utilsr   r    r„   r$   rv   rF   ro   ZGenericTypesCacheZAssignedParametersrG   rW   r%   r|   rN   rR   r�   r”   ru   r\   rx   rt   rU   Útyper]   r(   r(   r(   r7   Ú<module>   sN   
D <R


û