o
    ¨Ýjç  ã                   @  sÆ   d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
 d dlmZ ddlmZ dd	lmZ e	r@d
dlmZ d
dlmZ G dd„ dƒZeƒ Zd+dd„Zd,dd„Zd-d#d$„Zd%d&œd.d)d*„ZdS )/é    )ÚannotationsN)ÚCallable)Ú	ParameterÚ	Signature)ÚTYPE_CHECKINGÚAny)ÚPydanticUndefinedé   )Úsignature_no_eval)Úis_valid_identifieré   )ÚExtraValues©Ú	FieldInfoc                   @  s   e Zd Zdd„ ZdS )Ú_HAS_DEFAULT_FACTORY_CLASSc                 C  s   dS )Nz	<factory>© )Úselfr   r   úX/var/www/html/chatgpt/venv/lib/python3.10/site-packages/pydantic/_internal/_signature.pyÚ__repr__   s   z#_HAS_DEFAULT_FACTORY_CLASS.__repr__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s    r   Ú
field_nameÚstrÚ
field_infor   Úvalidate_by_aliasÚboolÚreturnc                 C  s@   |rt |jtƒrt|jƒr|jS t |jtƒrt|jƒr|jS | S )a  Extract the correct name to use for the field when generating a signature.

    Assuming the field has a valid alias, this will return the alias. Otherwise, it will return the field name.
    First priority is given to the alias, then the validation_alias, then the field name.

    Args:
        field_name: The name of the field
        field_info: The corresponding FieldInfo object.
        validate_by_alias: The `validate_by_alias` value of the config.

    Returns:
        The correct name to use when generating a signature.
    )Ú
isinstanceÚaliasr   r   Zvalidation_alias)r   r   r   r   r   r   Ú_field_name_for_signature   s   r    Úparamr   c                 C  sr   ddl m} | j}t||ƒr7| j}|dkrt}|j}|tu r*|jdu r'tj	}nt
j}| j|t| j||ƒ|d�S | S )a%  Modify the signature for a parameter in a dataclass where the default value is a FieldInfo instance.

    Args:
        param (Parameter): The parameter
        validate_by_alias: The `validate_by_alias` value of the config.

    Returns:
        Parameter: The custom processed parameter
    r   r   r   N)Ú
annotationÚnameÚdefault)Úfieldsr   r$   r   r"   r   r   Údefault_factoryr   ÚemptyÚdataclassesÚ_HAS_DEFAULT_FACTORYÚreplacer    r#   )r!   r   r   Zparam_defaultr"   r$   r   r   r   Ú_process_param_defaults2   s"   


ýr+   ÚinitúCallable[..., None]r%   údict[str, FieldInfo]ÚextraúExtraValues | Noneúdict[str, Parameter]c                 C  sž  ddl m} t| ƒj ¡ }i }d}d}||ddƒD ];}	| |	j¡r;t||	j ddƒdu r-q|	jt	|	j||	j |ƒd�}	|	j
d	krF|	jtd
�}	|	j|	ju rO|	}q|	||	j< q|r–| ¡ D ]:\}
}t	|
||ƒ}|
|v sm||v rnq[t|ƒsud}q[| ¡ r}tj}n|jdur…t}n|j}t|tj| ¡ |d�||< q[|dkrœd}|rÍ|rÍdtjfdtjfg}dd„ |D ƒ|kr¶d}n|j}||v rÅ|d7 }||v s½|j|d�||< |S )zaGenerate a mapping of parameter names to Parameter objects for a pydantic BaseModel or dataclass.r   )ÚisliceNFr	   r,   T)r#   r   )r"   )r"   r$   Zallowr   Údatac                 S  s   g | ]}|j |jf‘qS r   )r#   Úkind)Ú.0Úpr   r   r   Ú
<listcomp>™   s    z2_generate_signature_parameters.<locals>.<listcomp>Ú
extra_dataÚ_)Ú	itertoolsr2   r
   Ú
parametersÚvaluesÚgetr#   Úgetattrr*   r    r"   r   r4   ÚVAR_KEYWORDÚitemsr   Zis_requiredr   r'   r&   r)   r$   ÚKEYWORD_ONLYZrebuild_annotationÚPOSITIONAL_ONLY)r,   r%   r/   r   r2   Zpresent_paramsÚmerged_paramsZvar_kwZ
use_var_kwr!   r   ÚfieldÚ
param_namer$   Zdefault_model_signatureZvar_kw_namer   r   r   Ú_generate_signature_parametersW   sb   

üþÿrF   F)Úis_dataclassrG   r   c                   s<   t | ||ˆ ƒ}|r‡ fdd„| ¡ D ƒ}tt| ¡ ƒdd�S )a{  Generate signature for a pydantic BaseModel or dataclass.

    Args:
        init: The class init.
        fields: The model fields.
        validate_by_alias: The `validate_by_alias` value of the config.
        extra: The `extra` value of the config.
        is_dataclass: Whether the model is a dataclass.

    Returns:
        The dataclass/BaseModel subclass signature.
    c                   s   i | ]
\}}|t |ˆ ƒ“qS r   )r+   )r5   ÚkÚv©r   r   r   Ú
<dictcomp>¿   s    z/generate_pydantic_signature.<locals>.<dictcomp>N)r;   Úreturn_annotation)rF   r@   r   Úlistr<   )r,   r%   r   r/   rG   rC   r   rJ   r   Úgenerate_pydantic_signature¨   s   rN   )r   r   r   r   r   r   r   r   )r!   r   r   r   r   r   )
r,   r-   r%   r.   r/   r0   r   r   r   r1   )r,   r-   r%   r.   r   r   r/   r0   rG   r   r   r   )Ú
__future__r   r(   Úcollections.abcr   Úinspectr   r   Útypingr   r   Zpydantic_corer   Z_typing_extrar
   Z_utilsr   Úconfigr   r%   r   r   r)   r    r+   rF   rN   r   r   r   r   Ú<module>   s$    


%Wú