a
    g=ic6                     @   s  U d dl Z d dlmZ d dlmZ d dlmZ d dlm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 ddlmZ dd	l	mZ dd
lmZmZ e
rddl	mZ G dd dZe
r,d dlmZ ddl m!Z! ddl"m#Z# ddlm$Z$ eee$ eee%ef e#ee! gef Z&ee& Z'ee%ee f Z(e) Z*ee% e+d< dZ,dZ-ddddddde%e.e.e.e.e.e.eegdf dddZ/eeddddZ0edddde.e.e.eegdf d d!dZ0dEddddee e.e.e.edeegdf f d"d#dZ0ee.dd$d%d&Z1G d'd( d(Z2ee%ef ee%ee f d)d*d+Z3ee%ef eee eee.ef  f d)d,d-Z4d.d.d.d/d0d1Z5ed2d3d4d5Z6ee d6d7d8d9Z7h d:Z8ed;ee% d2d<d=d>Z9ed;ee% d2d<d?d@Z:dAee%df dBdCdDZ;dS )F    N)ChainMap)wraps)chain)FunctionType)TYPE_CHECKINGAnyCallableDictIterableListOptionalSetTupleTypeUnionoverload   )ConfigError)AnyCallable)ROOT_KEY
in_ipython)AnyClassMethodc                   @   s*   e Zd ZdZdeeeeeedddZdS )	Validatorfuncpre	each_itemalwayscheck_fieldsskip_on_failureFc                 C   s(   || _ || _|| _|| _|| _|| _d S Nr   )selfr   r   r   r   r   r    r"   j/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/pydantic/class_validators.py__init__   s    	zValidator.__init__N)FFFFF)__name__
__module____qualname__	__slots__r   boolr$   r"   r"   r"   r#   r      s        r   )	Signature)
BaseConfig)
ModelField)	ModelOrDc_FUNCSZ__validator_config__Z__root_validator_config__FT)r   r   r   r   wholeallow_reuser   )fieldsr   r   r   r   r/   r0   returnc                    s   st dn2td tr&t dntdd D s@t d|durjtdt d	u sdJ d
| tdd fdd}|S )a  
    Decorate methods on the class indicating that they should be used to validate fields
    :param fields: which field(s) the method should be called on
    :param pre: whether or not this validator should be called before the standard validators (else after)
    :param each_item: for complex objects (sets, lists etc.) whether to validate individual elements rather than the
      whole object
    :param always: whether this method and other validators should be called even if the value is missing
    :param check_fields: whether to check that the fields actually exist on the model
    :param allow_reuse: whether to track and raise an error if another validator refers to the decorated function
    z"validator with no fields specifiedr   z}validators should be used with fields and keyword arguments, not bare. E.g. usage should be `@validator('<field_name>', ...)`c                 s   s   | ]}t |tV  qd S r    )
isinstancestr).0fieldr"   r"   r#   	<genexpr>N       zvalidator.<locals>.<genexpr>zvalidator fields should be passed as separate string args. E.g. usage should be `@validator('<field_name_1>', '<field_name_2>', ...)`NzdThe "whole" keyword argument is deprecated, use "each_item" (inverse meaning, default False) insteadFz0"each_item" and "whole" conflict, remove "whole"r   fr2   c                    s.   t |  }t|tt|jdf |S )N)r   r   r   r   r   )_prepare_validatorsetattrVALIDATOR_CONFIG_KEYr   __func__r:   f_clsr0   r   r   r   r1   r   r"   r#   dec\   s    
zvalidator.<locals>.dec)r   r3   r   allwarningswarnDeprecationWarningr   )r   r   r   r   r/   r0   r1   rB   r"   rA   r#   	validator4   s&    
rG   )_funcr2   c                 C   s   d S r    r"   )rH   r"   r"   r#   root_validatork   s    rI   r   r0   r   )r   r0   r   r2   c                 C   s   d S r    r"   rJ   r"   r"   r#   rI   p   s    )rH   r   r0   r   r2   c                   sF   | r*t |  }t|tt|jd |S tdd fdd}|S )z
    Decorate methods on a model indicating that they should be used to validate (and perhaps modify) data either
    before or after standard model parsing/validation is performed.
    r   r   r   r   r9   c                    s&   t |  }t|tt|jd |S )NrK   )r;   r<   ROOT_VALIDATOR_CONFIG_KEYr   r>   r?   r0   r   r   r"   r#   rB      s
    
zroot_validator.<locals>.dec)r;   r<   rL   r   r>   r   )rH   r   r0   r   r@   rB   r"   rM   r#   rI   w   s    
)functionr0   r2   c                 C   sZ   t | tr| nt| }t sV|sV|jjd |jj }|tv rLtd| dt| |S )z
    Avoid validators with duplicated names since without this, validators can be overwritten silently
    which generally isn't the intended behaviour, don't run in ipython (see #312) or if allow_reuse is False.
    .zduplicate validator function "z."; if this is intended, set `allow_reuse=True`)	r3   classmethodr   r>   r&   r'   r.   r   add)rN   r0   r@   refr"   r"   r#   r;      s    

r;   c                   @   sF   e Zd ZdddddZeeeeef  dddZdd	d
dZ	dS )ValidatorGroupValidatorListDictN)
validatorsr2   c                 C   s   || _ dh| _d S )N*)rU   used_validators)r!   rU   r"   r"   r#   r$      s    zValidatorGroup.__init__)namer2   c                 C   sN   | j | | j|g }|tkr4|| jdg 7 }|rFdd |D S d S d S )NrV   c                 S   s   i | ]}|j j|qS r"   )r   r%   r5   vr"   r"   r#   
<dictcomp>   r8   z1ValidatorGroup.get_validators.<locals>.<dictcomp>)rW   rQ   rU   getr   )r!   rX   rU   r"   r"   r#   get_validators   s    zValidatorGroup.get_validators)r2   c                    sJ   t t fdd j  j D }|rFd|}td| dd S )Nc                 3   s"   | ]}d d  j | D V  qdS )c                 s   s   | ]}|j r|jjV  qd S r    )r   r   r%   rY   r"   r"   r#   r7      r8   z<ValidatorGroup.check_for_unused.<locals>.<genexpr>.<genexpr>N)rU   r5   r:   r!   r"   r#   r7      s   z2ValidatorGroup.check_for_unused.<locals>.<genexpr>z, z*Validators defined with incorrect fields: zO (use check_fields=False if you're inheriting from the model and intended this))setr   from_iterablerU   keysrW   joinr   )r!   Zunused_validatorsfnr"   r_   r#   check_for_unused   s    

zValidatorGroup.check_for_unused)
r%   r&   r'   r$   r4   r   r	   r   r]   re   r"   r"   r"   r#   rS      s   
rS   )	namespacer2   c                 C   s^   i }|   D ]L\}}t|td }|r|\}}|D ]&}||v rL|| | q0|g||< q0q|S r    )itemsgetattrr=   append)rf   rU   var_namevaluevalidator_configr1   rZ   r6   r"   r"   r#   extract_validators   s    rm   c           	      C   s   ddl m} g }g }|  D ]\}}t|td }|r||j}t|j }|d dkrnt	d| d| dt
|dkrt	d| d| d|jr||j q||j|jf q||fS )	Nr   	signaturer!   z%Invalid signature for root validator : zC, "self" not permitted as first argument, should be: (cls, values).   z, should be: (cls, values).)inspectro   rg   rh   rL   r   list
parametersrb   r   lenr   ri   r   )	rf   ro   Zpre_validatorsZpost_validatorsrX   rk   rl   sigargsr"   r"   r#   extract_root_validators   s$    
rx   rT   )base_validatorsrU   r2   c                 C   s6   |   D ](\}}||vr g ||< ||  |7  < q|S r    )rg   )ry   rU   r6   Zfield_validatorsr"   r"   r#   inherit_validators   s
    rz   ValidatorCallable)rG   r2   c                 C   s   ddl m} || }t|j }|d}|dkrLtd|  d| dn@|dkrtt| t| |t	|dd	 S t| t
| |t	|S d	S )
a  
    Make a generic function which calls a validator with the right arguments.

    Unfortunately other approaches (eg. return a partial of a function that builds the arguments) is slow,
    hence this laborious way of doing things.

    It's done like this so validators don't all need **kwargs in their signature, eg. any combination of
    the arguments "values", "fields" and/or "config" are permitted.
    r   rn   r!    Invalid signature for validator rp   z, "self" not permitted as first argument, should be: (cls, value, values, config, field), "values", "config" and "field" are all optional.clsr   N)rr   ro   rs   rt   rb   popr   r   _generic_validator_clsr`   _generic_validator_basic)rG   ro   rv   rw   Z	first_argr"   r"   r#   make_generic_validator   s    

 r   ValidatorsList)v_funcsr2   c                 C   s   dd | D S )Nc                 S   s   g | ]}|rt |qS r"   )r   r^   r"   r"   r#   
<listcomp>  r8   z#prep_validators.<locals>.<listcomp>r"   )r   r"   r"   r#   prep_validators  s    r   >   valuesconfigr6   r*   )rG   rv   rw   r2   c                    s   d}d|v rd}|dh8 }| ts:td  d| d|rJ fddS |t kr` fd	dS |d
hkrv fddS |dhkr fddS |dhkr fddS |d
dhkr fddS |d
dhkr҇ fddS |ddhkr fddS  fddS d S )NFkwargsTr|   rp   zb, should be: (cls, value, values, config, field), "values", "config" and "field" are all optional.c                    s    | ||||dS N)r   r6   r   r"   r}   rZ   r   r6   r   rG   r"   r#   <lambda>  r8   z(_generic_validator_cls.<locals>.<lambda>c                    s
    | |S r    r"   r   r   r"   r#   r     r8   r   c                    s    | ||dS N)r   r"   r   r   r"   r#   r     r8   r6   c                    s    | ||dS N)r6   r"   r   r   r"   r#   r   !  r8   r   c                    s    | ||dS N)r   r"   r   r   r"   r#   r   #  r8   c                    s    | |||dS N)r   r6   r"   r   r   r"   r#   r   %  r8   c                    s    | |||dS N)r   r   r"   r   r   r"   r#   r   '  r8   c                    s    | |||dS N)r6   r   r"   r   r   r"   r#   r   )  r8   c                    s    | ||||dS r   r"   r   r   r"   r#   r   ,  r8   issubset
all_kwargsr   r`   rG   rv   rw   Z
has_kwargsr"   r   r#   r     s2    





r   c                    s   d}d|v rd}|dh8 }| ts:td  d| d|rJ fddS |t kr` fd	dS |d
hkrv fddS |dhkr fddS |dhkr fddS |d
dhkr fddS |d
dhkr҇ fddS |ddhkr fddS  fddS d S )NFr   Tr|   rp   z], should be: (value, values, config, field), "values", "config" and "field" are all optional.c                    s    ||||dS r   r"   r   r   r"   r#   r   <  r8   z*_generic_validator_basic.<locals>.<lambda>c                    s    |S r    r"   r   r   r"   r#   r   >  r8   r   c                    s    ||dS r   r"   r   r   r"   r#   r   @  r8   r6   c                    s    ||dS r   r"   r   r   r"   r#   r   B  r8   r   c                    s    ||dS r   r"   r   r   r"   r#   r   D  r8   c                    s    |||dS r   r"   r   r   r"   r#   r   F  r8   c                    s    |||dS r   r"   r   r   r"   r#   r   H  r8   c                    s    |||dS r   r"   r   r   r"   r#   r   J  r8   c                    s    ||||dS r   r"   r   r   r"   r#   r   M  r8   r   r   r"   r   r#   r   /  s2    





r   r-   )type_r2   c                 C   s&   t dd | jD  }dd | D S )Nc                 S   s   g | ]
}|j qS r"   )__dict__)r5   r}   r"   r"   r#   r   Q  r8   z)gather_all_validators.<locals>.<listcomp>c                 S   s*   i | ]"\}}t |ts t |tr||qS r"   )hasattrr=   rL   )r5   krZ   r"   r"   r#   r[   R  s   z)gather_all_validators.<locals>.<dictcomp>)r   __mro__rg   )r   Zall_attributesr"   r"   r#   gather_all_validatorsP  s    r   )N)<rD   collectionsr   	functoolsr   	itertoolsr   typesr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   errorsr   r   utilsr   r   r   r   rr   r*   r   r+   r1   r,   r-   r4   r{   r   rT   r`   r.   __annotations__r=   rL   r)   rG   rI   r;   rS   rm   rx   rz   r   r   r   r   r   r   r"   r"   r"   r#   <module>   s   
8&7 $0"!