o
    7ήc?                  
   @   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*dede	eef defddZ+dee* deedf ddfddZ,i - j.Z/ee e(d< dedee% fddZ0dee
e1 e2f fdd Z3d!ee* d"e	e1ef d#e	e1e4f d$e	ee4f ddf
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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tt t tt df f f fdd}t||}|dur#|S  jr/t jvr/t	dt
|ts7|f} tu rHtdd	 |D rHt	d
t dsVt	d j dt | tt j|}t| | rp|rp 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 }||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   )r   len
isinstancelisttuple)r*   argsr'   r$   8/tmp/pip-target-vg8gfxp4/lib/python/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 N)r-   r   .0paramr$   r$   r2   	<genexpr>P       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   )r6   kvr$   r$   r2   
<dictcomp>`   s   " z2GenericModel.__class_getitem__.<locals>.<dictcomp>c                    s*   i | ]}| j v r|t  j | jfqS r$   )
__fields__r   
field_info)r6   r:   r1   r$   r2   r<   b   s   * )
__module____base__
__config____validators____cls_kwargs___c                 S   s   i | ]}|d qS r4   r$   r5   r$   r$   r2   r<      s    r   r   ))r   r   r   r   r"   getr%   r   	__bases__	TypeErrorr-   r/   r    anyhasattr__name__check_parameters_countdictzipr&   r   keysvalues__concrete_name__r   r   itemsget_caller_frame_infor   r   r?   __parameterized_bases__r#   sysmodules__dict__
setdefaultConfigiter_contained_typevarsr,   _prepare_model_fields)r'   r(   r3   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$   r1   r2   __class_getitem__6   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   r5   r$   r$   r2   
<listcomp>   s    z2GenericModel.__concrete_name__.<locals>.<listcomp>z, [])joinrJ   )r'   r(   param_namesparams_componentr$   r$   r2   rP      s   
zGenericModel.__concrete_name__r\   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 r4   r$   r5   rq   r$   r2   r8      s    zQGenericModel.__parameterized_bases__.<locals>.build_base_model.<locals>.<genexpr>)r/   r&   ri   )rp   rq   base_parametersparameterized_baser1   rr   r2   build_base_model   s   

z>GenericModel.__parameterized_bases__.<locals>.build_base_modelr&   Nc                    s   i | ]\}}|  ||qS r$   )rE   )r6   keyvalue)r\   r$   r2   r<      s    z8GenericModel.__parameterized_bases__.<locals>.<dictcomp>)	r   r    Parametrizationr   rF   
issubclassgetattrr#   rQ   )r'   r\   ru   rp   rq   r$   )r'   r\   r2   rS      s,   





z$GenericModel.__parameterized_bases__N)rJ   r?   __qualname__	__slots__r%   r   bool__annotations__r   r   TypeVarTyper   r   r   r   ri   classmethodstrrP   rx   r   rS   r$   r$   r$   r2   r    +   s   
 6_( type_type_mapr)   c                    sZ   s| S t | }t| }|tu r|^}}tt| t|f S |rYt fdd|D }t||r3| S |durOt| trOt|tsOt| dddurOtt	| j
}|dusUJ || S |s|t| tr|| js|| j}t fdd|D }t||rx| S | | S t| ttfrt fdd| D }t| |r| S |S |st| trt| 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       | ]}t | V  qd S r4   replace_types)r6   argr   r$   r2   r8      r9   z replace_types.<locals>.<genexpr>N_namec                 3   r   r4   r   )r6   tr   r$   r2   r8     r9   c                 3   r   r4   r   )r6   elementr   r$   r2   r8     r9   )r   r   r   r   r/   r   r-   r   rz   typingr   r   r    r%   r&   r   r.   r   
inner_typerE   )r   r   	type_argsorigin_typeannotated_typeannotationsresolved_type_argsresolved_listr$   r   r2   r      sJ   
	

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 )r,   r&   rG   rJ   )r'   r   actualexpecteddescriptionr$   r$   r2   rK   -  s   
"rK   
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)r-   r   rI   r   r   r    r&   r   r.   rY   r   )r;   varr0   r   r$   r$   r2   rY   8  s   

rY   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)NFrJ   )rT   	_getframe
ValueErrorRuntimeErrorAttributeError	f_globalsrE   f_locals)previous_caller_frameeframe_globalsr$   r$   r2   rR   G  s   
rR   rd   ra   r`   r\   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)	r=   rQ   r   	__class__r   r   outer_type_preparer~   )rd   ra   r`   r\   rv   fieldfield_type_hintconcrete_typer$   r$   r2   rZ   Y  s   

rZ   )6rT   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   typing_extensionsr   class_validatorsr   ra   r   mainr   r   typesr   r   r   r   r   r   utilsr   r   r   r   r   rx   r"   r~   r#   r    r   rK   rO   r   r   rY   r   r}   rR   typerZ   r$   r$   r$   r2   <module>   sB   
 @6 <"G


