a
    f=ic?                     @   s  U 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 d dl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"m#Z# edddZ$eZ%e	e%ee f Z&e! Z'e!eee eeeedf f f ee f e(d< e! Z)e!ee e&f e(d< G dd deZ*ee	eef edddZ+ee* eedf ddddZ,i - j.Z/ee e(d< eee% dddZ0ee
e1 e2f dddZ3ee* e	e1ef e	e1e4f e	ee4f ddd d!Z5dS )"    N)TYPE_CHECKINGAnyClassVarDictGenericIteratorListMappingOptionalTupleTypeTypeVarUnioncast)	Annotated   )gather_all_validators)DeferredType)	BaseModelcreate_model)JsonWrapper)display_as_typeget_all_type_hintsget_args
get_origintyping_base)LimitedDictall_identicallenient_issubclassGenericModelTGenericModel)bound._generic_types_cache_assigned_parametersc                   @   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tt t tt df f d fdd}t||}|durD|S  jr\t jvr\t	dt
|tsl|f} tu rtdd |D rt	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#}||ur|$||}|d7 }q 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 | fS N)r   )r*   r'   r$   b/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/pydantic/generics.py
_cache_keyB   s    z2GenericModel.__class_getitem__.<locals>._cache_keyNz?Cannot parameterize a concrete instantiation of a generic modelc                 s   s   | ]}t |tV  qd S r+   )
isinstancer   .0paramr$   r$   r-   	<genexpr>L       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   )r1   kvr$   r$   r-   
<dictcomp>\   r4   z2GenericModel.__class_getitem__.<locals>.<dictcomp>c                    s*   i | ]"}| j v r|t  j | jfqS r$   )
__fields__r   Z
field_info)r1   r5   r,   r$   r-   r7   ^   r4   )
__module____base__Z
__config__Z__validators__Z__cls_kwargs___c                 S   s   i | ]
}|d qS r+   r$   r0   r$   r$   r-   r7      r4   r   r   ))r   r   r   r   r"   getr%   r   	__bases__	TypeErrorr/   tupler    anyhasattr__name__check_parameters_countdictzipr&   r   keysvalues__concrete_name__r   r   itemsget_caller_frame_infor   r   r9   __parameterized_bases__r#   sysmodules__dict__
setdefaultZConfigiter_contained_typevarslen_prepare_model_fields)r'   r(   r.   cachedtypevars_mapZ
model_name
validatorsZ
type_hintsinstance_type_hintsfieldsZmodel_moduleZcalled_globallycreated_modelZobject_by_referenceZreference_nameZreference_module_globals
new_paramsr$   r,   r-   __class_getitem__6   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   r0   r$   r$   r-   
<listcomp>   r4   z2GenericModel.__concrete_name__.<locals>.<listcomp>z, [])joinrB   )r'   r(   Zparam_namesZparams_componentr$   r$   r-   rH      s    
zGenericModel.__concrete_name__)rT   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 r+   r$   r0   r`   r$   r-   r3      r4   zQGenericModel.__parameterized_bases__.<locals>.build_base_model.<locals>.<genexpr>)r?   r&   rZ   )r_   r`   Zbase_parametersZparameterized_baser,   ra   r-   build_base_model   s
    
z>GenericModel.__parameterized_bases__.<locals>.build_base_modelr&   Nc                    s   i | ]\}}|  ||qS r$   )r<   )r1   keyvalue)rT   r$   r-   r7      s   z8GenericModel.__parameterized_bases__.<locals>.<dictcomp>)	r   r    Parametrizationr   r=   
issubclassgetattrr#   rI   )r'   rT   rb   r_   r`   r$   )r'   rT   r-   rK      s     





z$GenericModel.__parameterized_bases__N)rB   r9   __qualname__	__slots__r%   r   bool__annotations__r   r   TypeVarTyper   r   r   r   rZ   classmethodstrrH   re   r   rK   r$   r$   r$   r-   r    +   s   
2[$)type_type_mapr)   c                    sb   s| S t | }t| }|tu r>|^}}tt| t|f S |rt fdd|D }t||rf| S |durt| trt|tst| dddurtt	| j
}|dusJ || S |st| tr| js| j}t fdd|D }t||r| S | | S t| ttfr2t fdd| D }t| |r.| S |S |sVt| trVt| j | _| 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]]

    c                 3   s   | ]}t | V  qd S r+   replace_types)r1   argrp   r$   r-   r3      r4   z replace_types.<locals>.<genexpr>N_namec                 3   s   | ]}t | V  qd S r+   rq   )r1   trt   r$   r-   r3     r4   c                 3   s   | ]}t | V  qd S r+   rq   )r1   elementrt   r$   r-   r3     r4   )r   r   r   rr   r?   r   r/   r   rg   typingru   r   r    r%   r&   r   listr   Z
inner_typer<   )ro   rp   Z	type_argsZorigin_typeannotated_typeannotationsZresolved_type_argsZresolved_listr$   rt   r-   rr      sJ    
	
rr   )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 )rQ   r&   r>   rB   )r'   r|   actualexpecteddescriptionr$   r$   r-   rC   )  s
    
rC   
DictValues)r6   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)r/   r   rA   r   r   r    r&   r   ry   rP   r   )r6   varargsrs   r$   r$   r-   rP   4  s    
rP   )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]
       z2This function must be used inside another functionN)NFrB   )rL   	_getframe
ValueErrorRuntimeErrorAttributeError	f_globalsr<   f_locals)Zprevious_caller_frameeZframe_globalsr$   r$   r-   rJ   C  s    rJ   )rX   rW   rV   rT   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)	r8   rI   ro   	__class__r   rr   Zouter_type_preparerk   )rX   rW   rV   rT   rc   fieldZfield_type_hintZconcrete_typer$   r$   r-   rR   U  s    

rR   )6rL   rx   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   Ztyping_extensionsr   Zclass_validatorsr   rW   r   mainr   r   typesr   r   r   r   r   r   utilsr   r   r   r   rl   re   r"   rk   r#   r    rr   rC   rG   r   r   rP   rn   rj   rJ   typerR   r$   r$   r$   r-   <module>   s6   
@6 8G


