U
    eE                     @   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rd 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 Z.e j)dkrtee-ee f Z/eee e.f Z0neZ/eZ0e/ Z1e0 Z2G dd deZ3eeeef edddZ4ee3 eedf ddddZ5i 6 j7Z8ee e9d< ee	e, dddZ:eee; e<f d d!d"Z=ee3 ee;ef ee;e>f eee>f dd#d$d%Z?d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	st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|< |rd}|}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.

        )_paramsr1   c                    s@   t | }t|dkr6t|d tr6t|d |d f} | |fS )N   r   r   )r   len
isinstancelisttuple)r2   argsr/   r,   R/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/pydantic/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      r   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/   r0   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/   r0   Zparam_namesZparams_componentr,   r,   r:   rU      s    
zGenericModel.__concrete_name__)ra   r1   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_typesr1   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_mapr1   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 |rt fdd|D }t
||r| S |dk	rt| trt|tst| dddk	rtt| j}|dk	sttjdkr|tjkrt||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 |st| trt| j | _| S  	| | }t|trt|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/   
parametersr1   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   r1   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^   )r1   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]
    r3   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   r1   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_   )@rZ   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class_validatorsr   rc   r   mainr   r   r   r   r   r   r   r   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


