U
    nËjP9  ã                   @   s   U d dl Z d dlmZ d dlmZmZ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r²d d
lmZ G dd„ dƒZ e�r4d 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Z0dZ1dddddddœe)e2e2e2e2ee2 e2eegdf dœdd„Z3eeddœdd„ƒZ4eddddœe2e2e2eegdf dœd d„ƒZ4dFddddœee e2e2e2edeegdf f d!œd"d„Z4ee2dd#œd$d%„Z5G d&d'„ d'ƒZ6ee)ef ee)ee  f d(œd)d*„Z7ee)ef eee eee2ef  f d(œd+d,„Z8d-d-d-d.œd/d0„Z9ed1d2œd3d4„Z:ee d5d6œd7d8„Z;d9d:d;hZ<ed<ee) d1d=œd>d?„Z=ed<ee) d1d=œd@dA„Z>dBee)df dCœdDdE„Z?dS )Gé    N)ÚChainMap)ÚpartialÚpartialmethodÚ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#   ú@/tmp/pip-unpacked-wheel-zvl9l234/pydantic/v1/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   r0   r1   Úreturnc                    sŒ   ˆst dƒ‚n2tˆd tƒr&t dƒ‚ntdd„ ˆD ƒƒs@t dƒ‚|dk	rjt dt¡ ˆd	ksdt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   s     zvalidator.<locals>.<genexpr>z…validator 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   ©Úfr3   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©r1   r   r   r   r2   r   r#   r$   Údec\   s    
þýzvalidator.<locals>.dec)	r   r4   r   ÚallÚwarningsÚwarnÚDeprecationWarningÚAssertionErrorr   )r   r   r   r   r0   r1   r2   rB   r#   rA   r$   Ú	validator4   s&    
ÿÿþrH   )Ú_funcr3   c                 C   s   d S r!   r#   )rI   r#   r#   r$   Úroot_validatork   s    rJ   ©r   r1   r    )r   r1   r    r3   c                 C   s   d S r!   r#   rK   r#   r#   r$   rJ   p   s    )rI   r   r1   r    r3   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 )NrL   )r;   r<   ÚROOT_VALIDATOR_CONFIG_KEYr   r>   r?   ©r1   r   r    r#   r$   rB   …   s    
  ÿzroot_validator.<locals>.dec)r;   r<   rM   r   r>   r   )rI   r   r1   r    r@   rB   r#   rN   r$   rJ   w   s    
  ÿ)Úfunctionr1   r3   c                 C   st   t | tƒr| nt| ƒ}tƒ sp|spt|jddƒd t|jddt|jƒ› d�ƒ }|tkrft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.
    r'   z<No __module__>Ú.r(   z<No __qualname__: id:ú>zduplicate validator function "z."; if this is intended, set `allow_reuse=True`)	r4   Úclassmethodr   Úgetattrr>   Úidr/   r   Úadd)rO   r1   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)Ú
validatorsr3   c                 C   s   || _ dh| _d S )NÚ*)rY   Úused_validators)r"   rY   r#   r#   r$   r%   ¢   s    zValidatorGroup.__init__)Únamer3   c                 C   sN   | j  |¡ | j |g ¡}|tkr4|| j dg ¡7 }|rFdd„ |D ƒS d S d S )NrZ   c                 S   s*   i | ]"}t |jd dt|jƒ› d�ƒ|“qS )r&   ú<No __name__: id:rQ   )rS   r   rT   ©r6   Úvr#   r#   r$   Ú
<dictcomp>¬   s      z1ValidatorGroup.get_validators.<locals>.<dictcomp>)r[   rU   rY   Úgetr   )r"   r\   rY   r#   r#   r$   Úget_validators¦   s    zValidatorGroup.get_validators)r3   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   s0   | ](}|j rt|jd dt|jƒ› d�ƒV  qdS )r&   r]   rQ   N)r   rS   r   rT   r^   r#   r#   r$   r8   ³   s   þz<ValidatorGroup.check_for_unused.<locals>.<genexpr>.<genexpr>N)rY   ©r6   r:   ©r"   r#   r$   r8   ²   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_iterablerY   Úkeysr[   Újoinr   )r"   Zunused_validatorsÚfnr#   rd   r$   Úcheck_for_unused°   s    úÿ


ÿzValidatorGroup.check_for_unused)
r&   r'   r(   r%   r5   r   r   r   rb   rj   r#   r#   r#   r$   rW   ¡   s   
rW   )Ú	namespacer3   c                 C   s^   i }|   ¡ D ]L\}}t|td ƒ}|r|\}}|D ]&}||krL||  |¡ q0|g||< q0q|S r!   )ÚitemsrS   r=   Úappend)rk   rY   Úvar_nameÚvalueÚvalidator_configr2   r_   r7   r#   r#   r$   Úextract_validatorsÃ   s    rq   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r�t	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).)Úinspectrs   rl   rS   rM   r   ÚlistÚ
parametersrg   r   Úlenr   rm   r    )	rk   rs   Zpre_validatorsZpost_validatorsr\   ro   rp   ÚsigÚargsr#   r#   r$   Úextract_root_validatorsÑ   s$    
ÿr|   rX   )Úbase_validatorsrY   r3   c                 C   s6   |   ¡ D ](\}}||kr g ||< ||  |7  < q|S r!   )rl   )r}   rY   r7   Zfield_validatorsr#   r#   r$   Úinherit_validatorsê   s
    r~   ÚValidatorCallable)rH   r3   c                    sÈ   ddl m} tˆ ttfƒs2|ˆ ƒ}t|j ¡ ƒ}n(|ˆ jƒ}‡ fdd„|ˆ jƒj ¡ D ƒ}| 	d¡}|dkr„t
dˆ › d|› d�ƒ‚n@|d	kr¬tˆ ƒ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   rr   c                    s$   g | ]}|ˆ j ˆ j ¡ B kr|‘qS r#   )r{   Úkeywordsrg   )r6   Úk©rH   r#   r$   Ú
<listcomp>  s   þz*make_generic_validator.<locals>.<listcomp>r"   ú Invalid signature for validator rt   zŠ, "self" not permitted as first argument, should be: (cls, value, values, config, field), "values", "config" and "field" are all optional.Úclsé   N)rv   rs   r4   r   r   rw   rx   rg   r   Úpopr   r   Ú_generic_validator_clsre   Ú_generic_validator_basic)rH   rs   rz   r{   Z	first_argr#   r‚   r$   Úmake_generic_validatorò   s     


þ
ÿ rŠ   ÚValidatorsList)Úv_funcsr3   c                 C   s   dd„ | D ƒS )Nc                 S   s   g | ]}|rt |ƒ‘qS r#   )rŠ   rc   r#   r#   r$   rƒ     s      z#prep_validators.<locals>.<listcomp>r#   )rŒ   r#   r#   r$   Úprep_validators  s    r�   Úvaluesr7   Úconfigr+   )rH   rz   r{   r3   c                    sú   d}d|k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„   rt   zb, should be: (cls, value, values, config, field), "values", "config" and "field" are all optional.c                    s   ˆ | ||||d�S ©N)rŽ   r7   r�   r#   ©r…   r_   rŽ   r7   r�   r‚   r#   r$   Ú<lambda>.  ó    z(_generic_validator_cls.<locals>.<lambda>c                    s
   ˆ | |ƒS r!   r#   r’   r‚   r#   r$   r“   0  r”   rŽ   c                    s   ˆ | ||d�S ©N)rŽ   r#   r’   r‚   r#   r$   r“   2  r”   r7   c                    s   ˆ | ||d�S ©N)r7   r#   r’   r‚   r#   r$   r“   4  r”   r�   c                    s   ˆ | ||d�S ©N)r�   r#   r’   r‚   r#   r$   r“   6  r”   c                    s   ˆ | |||d�S ©N)rŽ   r7   r#   r’   r‚   r#   r$   r“   8  r”   c                    s   ˆ | |||d�S ©N)rŽ   r�   r#   r’   r‚   r#   r$   r“   :  r”   c                    s   ˆ | |||d�S ©N)r7   r�   r#   r’   r‚   r#   r$   r“   <  r”   c                    s   ˆ | ||||d�S r‘   r#   r’   r‚   r#   r$   r“   ?  r”   ©ÚissubsetÚ
all_kwargsr   re   ©rH   rz   r{   Z
has_kwargsr#   r‚   r$   rˆ      s2    

ÿ



rˆ   c                    sú   d}d|k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„   rt   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“   O  r”   z*_generic_validator_basic.<locals>.<lambda>c                    s   ˆ |ƒS r!   r#   r’   r‚   r#   r$   r“   Q  r”   rŽ   c                    s   ˆ ||d�S r•   r#   r’   r‚   r#   r$   r“   S  r”   r7   c                    s   ˆ ||d�S r–   r#   r’   r‚   r#   r$   r“   U  r”   r�   c                    s   ˆ ||d�S r—   r#   r’   r‚   r#   r$   r“   W  r”   c                    s   ˆ |||d�S r˜   r#   r’   r‚   r#   r$   r“   Y  r”   c                    s   ˆ |||d�S r™   r#   r’   r‚   r#   r$   r“   [  r”   c                    s   ˆ |||d�S rš   r#   r’   r‚   r#   r$   r“   ]  r”   c                    s   ˆ ||||d�S r‘   r#   r’   r‚   r#   r$   r“   `  r”   r›   rž   r#   r‚   r$   r‰   B  s2    

ÿ



r‰   r.   )Útype_r3   c                 C   s&   t dd„ | jD ƒŽ }dd„ | ¡ D ƒS )Nc                 S   s   g | ]
}|j ‘qS r#   )Ú__dict__)r6   r…   r#   r#   r$   rƒ   d  s     z)gather_all_validators.<locals>.<listcomp>c                 S   s*   i | ]"\}}t |tƒs t |tƒr||“qS r#   )Úhasattrr=   rM   )r6   r�   r_   r#   r#   r$   r`   e  s
   
 
þ z)gather_all_validators.<locals>.<dictcomp>)r   Ú__mro__rl   )rŸ   Zall_attributesr#   r#   r$   Úgather_all_validatorsc  s    þr£   )N)@rD   Úcollectionsr   Ú	functoolsr   r   r   Ú	itertoolsr   Útypesr   Útypingr   r	   r
   r   r   r   r   r   r   r   r   r   Zpydantic.v1.errorsr   Zpydantic.v1.typingr   Zpydantic.v1.utilsr   r   r   r   rv   r+   Zpydantic.v1.configr,   Zpydantic.v1.fieldsr-   Zpydantic.v1.typesr.   r5   r   r‹   rX   re   r/   Ú__annotations__r=   rM   r*   rH   rJ   r;   rW   rq   r|   r~   rŠ   r�   r�   rˆ   r‰   r£   r#   r#   r#   r$   Ú<module>   s”    8&ùø7  ÿ  þ ÿ  ÿ   þ"$0'
"!