U
    h:?                     @  s  U 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	m
Z
mZ d dlmZmZmZmZ d dlmZ dd	lmZ dd
lmZ e	rdhZdhZddhZddddhZdheZdhZeedddddhZeeZeeeZeeeZ eeeZ!eeZ"eeZ#eeZ$eeeZ%ddhe%Z&eeeZ'eZ(eZ)eeZ*eeZ+eeZ,eZ-eZ.eZ/dhZ0ddddd d!hZ1d"Z2d[e2Z3d(Z4ee5Z6d)e7d*< ee2fed+fed,fe d-fe!d.fe"d/fe#d0fe%d1fe'd2fe*d3fee2e3e4d\fe0d6fe1d7fe(d8fe)d9fe-d:fe.d;fe&d<fe/d=fgZ8d>e7d?< e8D ]$\Z9Z:e9D ]Z;e6e; <e: q$qd@d@dAdBdCZ=dDdDdEdFdGZ>edHdIdJdKZ?d@dLdMdNdOdPZ@dDdQdEdRdSZAdTdUd@dVdWdXdYZBdZS )]    )annotations)defaultdict)copy)	lru_cachepartial)TYPE_CHECKINGAnyIterable)
CoreSchemaPydanticCustomErrorValidationErrorto_jsonable_python)core_schema   )PydanticMetadata)import_cached_field_infostrictZ	fail_fast
min_length
max_lengthlegeltgtmultiple_ofallow_inf_nanstrip_whitespaceto_lowerto_upperpatterncoerce_numbers_to_strZ
max_digitsZdecimal_places
union_modeZallowed_schemesZhost_requiredZdefault_hostdefault_portZdefault_path)strbytesurlmulti-host-urllisttupleset	frozenset	generator)floatintdatetime	timedeltadatetimezdict[str, set[str]]CONSTRAINTS_TO_ALLOWED_SCHEMAS)r#   )r&   )r'   )r(   r)   )dict)r*   )r+   )r,   )r-   r.   r0   r/   
typed-dictmodel)union)r$   r%   )bool)uuid)zlax-or-strict)enum)decimal)complexz&list[tuple[set[str], tuple[str, ...]]]constraint_schema_pairingsr   vreturnc                 C  s(   t | tttttt d fkr$t| S | S N)typer,   r"   r+   r#   r6   r   )r=    rA   P/tmp/pip-unpacked-wheel-y076a95i/pydantic/_internal/_known_annotated_metadata.pyas_jsonable_valuec   s    rC   zIterable[Any])r   r>   c                 c  sf   ddl }t }| D ]N}t||jr.|E dH  qt||rZ|jE dH  t|}g |_|V  q|V  qdS )a  Expand the annotations.

    Args:
        annotations: An iterable of annotations.

    Returns:
        An iterable of expanded annotations.

    Example:
        ```python
        from annotated_types import Ge, Len

        from pydantic._internal._known_annotated_metadata import expand_grouped_metadata

        print(list(expand_grouped_metadata([Ge(4), Len(5)])))
        #> [Ge(ge=4), MinLen(min_length=5)]
        ```
    r   N)annotated_typesr   
isinstanceZGroupedMetadatametadatar   )r   atZ	FieldInfo
annotationrA   rA   rB   expand_grouped_metadatai   s    
rI   zdict[type, str])r>   c                  C  s6   ddl } | jd| jd| jd| jd| jd| jd| jd	iS )
a  Return a mapping of annotated types to constraints.

    Normally, we would define a mapping like this in the module scope, but we can't do that
    because we don't permit module level imports of `annotated_types`, in an attempt to speed up
    the import time of `pydantic`. We still only want to have this dictionary defined in one place,
    so we use this function to cache the result.
    r   Nr   r   r   r   r   r   r   )rD   ZGtZGeZLtZLeZ
MultipleOfZMinLenZMaxLen)rG   rA   rA   rB   _get_at_to_constraint_map   s     	       rJ   r
   zCoreSchema | None)rH   schemar>   c              
     s,  ddl ddlm}m} | }t g\}}|d dddd	d
h}g }| D ]\}tkrptd t }	dkrdkrt	 |d |d< |  S |	krΈdkrĈdkr||d< qP||< qP|krddddfdd}
|
t|
tjf |i qP|krtkr|}|d dkrB|d }q(|d }|dksp|dkr|d d dkrdkr~dnd}ndkrdnd}n}tt| f|i|}|di }|d  }dk	r||t|i|d < n|t|i|d < ||d< qPd!kr*|d"kr*t||}qPtd# d$ d%qP|D ]Ɖ t  }t  }kr|| |}|dkrtd tt|t i|}qFn\t jjfrt jd&rڈ jj nd'ddd( fd)d*}t||}n dS qF|r(|g| }t|S |S )+a{  Apply `annotation` to `schema` if it is an annotation we know about (Gt, Le, etc.).
    Otherwise return `None`.

    This does not handle all known annotations. If / when it does, it can always
    return a CoreSchema and return the unmodified schema if the annotation should be ignored.

    Assumes that GroupedMetadata has already been expanded via `expand_grouped_metadata`.

    Args:
        annotation: The annotation.
        schema: The schema.

    Returns:
        An updated schema with annotation if it is an annotation we know about, `None` otherwise.

    Raises:
        PydanticCustomError: If `Predicate` fails.
    r   Nr   )NUMERIC_VALIDATOR_LOOKUPforbid_inf_nan_checkr@   r   r   r   r   r   zUnknown constraint >   function-afterfunction-wrapfunction-beforer   rK   r    r5   moder   zcs.ValidatorFunctionWrapHandler)valuehandlerr>   c                   sh   z|| }W nV t k
rb } z8d| d d krNtd  d|  d d|W 5 d }~X Y nX |S )Nr@   r   Unable to apply constraint 'z' to supplied value z for schema of type '')r   errors	TypeError)rR   rS   xve)
constraintschema_typerA   rB   +_apply_constraint_with_incompatibility_info   s    zIapply_known_metadata.<locals>._apply_constraint_with_incompatibility_infor&   zjson-or-pythonZjson_schemar   ZminItemsZmaxItemsZ	minLengthZ	maxLengthrF   Zpydantic_js_updatesr   FrT   z' to schema of type 'rU   __qualname__ r<   c                   sH     | }t jr.|sDtdd dn|rDtdd d| S )NZpredicate_failedz
Predicate z failedZnot_operation_failedzNot of )funcrE   	Predicater   )r=   Zpredicate_satisfied)rH   rG   predicate_namerA   rB   val_func,  s    


z&apply_known_metadata.<locals>.val_func)rD   Z_validatorsrL   rM   r   collect_known_metadataitemsr1   
ValueErrorapply_known_metadataappendcsZno_info_wrap_validator_functionZ
str_schemaLENGTH_CONSTRAINTSZ no_info_after_validator_functionr   getrC   RuntimeErrorr@   rJ   getattrrE   r`   ZNothasattrr_   r]   Zchain_schema)rH   rK   rL   rM   Zschema_updateZother_metadataZchain_schema_constraintsZchain_schema_stepsrR   Zallowed_schemasr\   Zinner_schemaZinner_schema_typeZjs_constraint_keyrF   Zexisting_json_schema_updatesannotation_typeat_to_constraint_mapZ	validatorrb   rA   )rH   rG   rZ   ra   r[   rB   rf      s    

 


 




 


rf   z tuple[dict[str, Any], list[Any]]c                 C  s   t | } i }g }| D ]}t|tr0||j qt| }t  }kr^|| }t||||< qt|trt|tr|dd t	|
 D  q|| qdd |
 D }||fS )a  Split `annotations` into known metadata and unknown annotations.

    Args:
        annotations: An iterable of annotations.

    Returns:
        A tuple contains a dict of known metadata and a list of unknown annotations.

    Example:
        ```python
        from annotated_types import Gt, Len

        from pydantic._internal._known_annotated_metadata import collect_known_metadata

        print(collect_known_metadata([Gt(1), Len(42), ...]))
        #> ({'gt': 1, 'min_length': 42}, [Ellipsis])
        ```
    c                 S  s    i | ]\}}| d s||qS )_)
startswith.0kr=   rA   rA   rB   
<dictcomp>n  s     
  z*collect_known_metadata.<locals>.<dictcomp>c                 S  s   i | ]\}}|d k	r||qS r?   rA   rr   rA   rA   rB   ru   t  s       )rI   rE   r   update__dict__r@   rJ   rl   
issubclassvarsrd   rg   )r   res	remainingrH   rn   ro   rZ   rA   rA   rB   rc   K  s    
rc   zdict[str, Any]zIterable[str]None)rF   allowedsource_typer>   c                 C  s<   |   t| }|r8td|dddd |D  dS )a  A small utility function to validate that the given metadata can be applied to the target.
    More than saving lines of code, this gives us a consistent error message for all of our internal implementations.

    Args:
        metadata: A dict of metadata.
        allowed: An iterable of allowed metadata.
        source_type: The source type.

    Raises:
        TypeError: If there is metadatas that can't be applied on source type.
    z/The following constraints cannot be applied to z: z, c                 S  s   g | ]
}|qS rA   rA   )rs   rt   rA   rA   rB   
<listcomp>  s     z"check_metadata.<locals>.<listcomp>N)keysr(   rW   join)rF   r}   r~   unknownrA   rA   rB   check_metadatax  s
    r   N)r&   r'   r(   r)   r*   )r3   r4   )C
__future__r   collectionsr   r   	functoolsr   r   typingr   r   r	   Zpydantic_corer
   r   r   r   r   rh   _fieldsr   Z_import_utilsr   ZSTRICTZ	FAIL_FASTri   Z
INEQUALITYZNUMERIC_CONSTRAINTSZALLOW_INF_NANZSTR_CONSTRAINTSZBYTES_CONSTRAINTSZLIST_CONSTRAINTSZTUPLE_CONSTRAINTSZSET_CONSTRAINTSZDICT_CONSTRAINTSZGENERATOR_CONSTRAINTSZSEQUENCE_CONSTRAINTSZFLOAT_CONSTRAINTSZDECIMAL_CONSTRAINTSZINT_CONSTRAINTSZBOOL_CONSTRAINTSZUUID_CONSTRAINTSZDATE_TIME_CONSTRAINTSZTIMEDELTA_CONSTRAINTSZTIME_CONSTRAINTSZLAX_OR_STRICT_CONSTRAINTSZENUM_CONSTRAINTSZCOMPLEX_CONSTRAINTSZUNION_CONSTRAINTSZURL_CONSTRAINTSZTEXT_SCHEMA_TYPESZSEQUENCE_SCHEMA_TYPESZNUMERIC_SCHEMA_TYPESr(   r1   __annotations__r;   constraintsZschemascrv   rC   rI   rJ   rf   rc   r   rA   rA   rA   rB   <module>   s    
	




	( %-