U
    nËjÏE  ã                   @   sJ  U 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 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 Z/ee.ee f Z0e j+dk�rtee/ee f Z1eee e0f Z2neZ1eZ2e1ƒ Z3e2ƒ Z4G dd„ deƒZ5eeeef edœdd„Z6ee5 eedf ddœdd„Z7i  8¡ j9Z:ee e;d< ee	e. dœdd„Z<eee= e>f dœd d!„Z?ee5 ee=ef ee=e@f eee@f dd"œd#d$„ZAd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©é   é
   )Ú_UnionGenericAlias©r"   é   )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k	r.|S ˆ jrFtˆ jkrFtdƒ‚t|t	ƒsV|f}ˆ t
k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"}||k	�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.

        )Ú_paramsr0   c                    s@   t | ƒ}t|ƒdkr6t|d tƒr6t|d ƒ|d f}ˆ | |fS )Né   r   é   )r   ÚlenÚ
isinstanceÚlistÚtuple)r1   Úargs©r.   r+   ú8/tmp/pip-unpacked-wheel-zvl9l234/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)r5   r   ©Ú.0Úparamr+   r+   r:   Ú	<genexpr>e   s     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k	r||“qS r+   )r   r   )r>   ÚkÚvr+   r+   r:   Ú
<dictcomp>u   s       z2GenericModel.__class_getitem__.<locals>.<dictcomp>c                    s*   i | ]"}|ˆ j kr|tƒ ˆ j | jf“qS r+   )Ú
__fields__r   Z
field_info)r>   rA   r9   r+   r:   rC   w   s     
  )Ú
__module__Ú__base__Z
__config__Z__validators__Z__cls_kwargs__Ú_c                 S   s   i | ]
}|d “qS r<   r+   r=   r+   r+   r:   rC   ˜   s      r3   r   )(r   ÚCacheKeyÚ_generic_types_cacheÚgetr,   r   Ú	__bases__Ú	TypeErrorr5   r7   r(   ÚanyÚhasattrÚ__name__Úcheck_parameters_countÚdictÚzipr-   r   ÚkeysÚvaluesÚ__concrete_name__r   r   ÚitemsÚget_caller_frame_infor   r   r   rE   Ú__parameterized_bases__Ú_assigned_parametersÚsysÚmodulesÚ__dict__Ú
setdefaultZConfigÚiter_contained_typevarsr4   Ú_prepare_model_fields)r.   r/   r;   Ú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+   r9   r:   Ú__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+   r:   Ú
<listcomp>¶   s     z2GenericModel.__concrete_name__.<locals>.<listcomp>z, ú[ú])ÚjoinrO   )r.   r/   Zparam_namesZparams_componentr+   r+   r:   rU   ª   s    
zGenericModel.__concrete_name__)ra   r0   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krŠ|tk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_typesr0   c                 3   s@   t ‡ fdd„| jD ƒƒ}|  |¡}|| ks2|ˆkr6d S |V  d S )Nc                 3   s   | ]}ˆ | V  qd S r<   r+   r=   ©rl   r+   r:   r@   Õ   s     zQGenericModel.__parameterized_bases__.<locals>.build_base_model.<locals>.<genexpr>)r7   r-   rf   )rk   rl   Zbase_parametersZparameterized_baser9   rm   r:   Úbuild_base_modelÒ   s
    
z>GenericModel.__parameterized_bases__.<locals>.build_base_modelr-   Nc                    s   i | ]\}}|ˆ   ||¡“qS r+   )rJ   )r>   ÚkeyÚvalue)ra   r+   r:   rC   ñ   s     z8GenericModel.__parameterized_bases__.<locals>.<dictcomp>)	r   r(   ÚParametrizationr   rK   Ú
issubclassÚgetattrrY   rV   )r.   ra   rn   rk   rl   r+   )r.   ra   r:   rX   º   s"     
þ




ÿz$GenericModel.__parameterized_bases__N)rO   rE   Ú__qualname__Ú	__slots__r,   r   ÚboolÚ__annotations__r   r   ÚTypeVarTyper   r'   r   r   rf   ÚclassmethodÚstrrU   rq   r   rX   r+   r+   r+   r:   r(   @   s   
2_$)Útype_Útype_mapr0   c           	         sÊ  ˆ s| S t | ƒ}t| ƒ}|tkr>|^}}tt|ˆ ƒt|ƒf S |tksXtjdkrd|tkrdˆ  	| | ¡S |röt‡ fdd„|D ƒƒ}t
||ƒrŒ| S |dk	rÄt| tƒrÄt|tƒsÄt| ddƒdk	rÄtt| jƒ}|dk	sÐt‚tjdkrî|tjkrît||ƒS || S |�sDt| tƒ�rD| j�sD| j}t‡ fdd„|D ƒƒ}t
||ƒ�r<| S | | S t| ttfƒ�r~t‡ fdd„| D ƒƒ}t
| |ƒ�rz| 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 r<   ©Úreplace_types)r>   Úarg©r|   r+   r:   r@     s     z replace_types.<locals>.<genexpr>NÚ_namer!   c                 3   s   | ]}t |ˆ ƒV  qd S r<   r}   )r>   Útr€   r+   r:   r@   0  s     c                 3   s   | ]}t |ˆ ƒV  qd S r<   r}   )r>   Úelementr€   r+   r:   r@   8  s     )r   r   r   r~   r7   Ú
ExtLiteralrZ   Úversion_infor   rJ   r   r5   r   rs   Útypingr�   ÚAssertionErrorÚtypesZ	UnionTyper$   r    r(   r,   r-   r	   r6   r   Z
inner_typerz   r   )	r{   r|   Z	type_argsZorigin_typeÚannotated_typeÚannotationsZresolved_type_argsZresolved_listÚnew_typer+   r€   r:   r~   û   sX    
ÿþýü	
r~   )r.   Ú
parametersr0   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 )r4   r-   rL   rO   )r.   rŒ   ÚactualÚexpectedÚdescriptionr+   r+   r:   rP   M  s
    
rP   Ú
DictValues)rB   r0   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)r5   r   rN   r   r    r(   r-   r’   r6   r^   r   )rB   Úvarr8   r   r+   r+   r:   r^   X  s    
r^   )r0   c               
   C   sj   zt  d¡} W n@ tk
r: } ztdƒ|‚W 5 d}~X Y n tk
rN   Y dS X | j}| d¡| j|k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]
    r2   z2This function must be used inside another functionN)NFrO   )rZ   Ú	_getframeÚ
ValueErrorÚRuntimeErrorÚAttributeErrorÚ	f_globalsrJ   Úf_locals)Zprevious_caller_frameÚeZframe_globalsr+   r+   r:   rW   g  s    rW   )rd   rc   rb   ra   r0   c                 C   sz   | j  ¡ D ]j\}}||kr,|jjtk	s
t‚q
|jjtksDt|jjƒ‚|| }t||ƒ}||_||_| ¡  || j	|< q
dS )zP
    Replace DeferredType fields with concrete type hints and prepare them.
    N)
rD   rV   r{   Ú	__class__r   r‡   r~   Zouter_type_Úpreparerw   )rd   rc   rb   ra   ro   ÚfieldZfield_type_hintZconcrete_typer+   r+   r:   r_   y  s    

r_   )BrZ   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$   r'   rx   rH   rq   ZGenericTypesCacheZAssignedParametersrI   rY   r(   r~   rP   rT   r›   r’   rw   r^   rz   rv   rW   Útyper_   r+   r+   r+   r:   Ú<module>   sP    D
 <R


û