U
    nËj¤B  ã                   @  s  d Z ddlmZ ddlZddlZddlmZ ddl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 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$ ddl%m&Z& e�r"ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 G dd„ de$ƒZ1dddœdd„Z2edd �d!d"œd#d$„ƒZ3d%d&d'd(d)œd*d+„Z4dd,œd-d.d'd/d0d1d2œd3d4„Z5d%d5d(d6œd7d8„Z6d%dd9d:œd;d<„Z7dddd=œd>d/d0d?d&d@œdAdB„Z8d5d9dCœdDdE„Z9d5d9dCœdFdG„Z:dHdIdJœdKdL„Z;dS )MzmPrivate logic related to fields (the `Field()` function and `FieldInfo` class), and arguments to `Annotated`.é    )ÚannotationsN)Úcopy)Ú	lru_cache)Ú	ParameterÚismethoddescriptorÚ	signature)ÚTYPE_CHECKINGÚAnyÚCallableÚPattern)ÚPydanticUndefined)ÚTypeIs)ÚPydanticUserErroré   )Ú_typing_extra)ÚConfigWrapper)Úextract_docstrings_from_cls)Úimport_cached_base_modelÚimport_cached_field_info)Ú
NsResolver)ÚRepresentation)Úcan_be_positional©ÚBaseMetadataé   )Ú	FieldInfo)Ú	BaseModel)ÚStandardDataclass)ÚDecoratorInfosc                   @  s   e Zd ZdZdZdS )ÚPydanticMetadataz0Base class for annotation markers like `Strict`.© N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r    r    r    ú>/tmp/pip-unpacked-wheel-zvl9l234/pydantic/_internal/_fields.pyr   "   s   r   r	   r   )ÚmetadataÚreturnc                  K  s
   t ƒ | ƒS )z¿Create a new `_PydanticGeneralMetadata` class with the given metadata.

    Args:
        **metadata: The metadata to add.

    Returns:
        The new `_PydanticGeneralMetadata` class.
    )Ú_general_metadata_cls©r'   r    r    r&   Úpydantic_general_metadata(   s    	r+   )Úmaxsizeztype[BaseMetadata])r(   c                  C  s"   ddl m}  G dd„ dt| ƒ}|S )zCDo it this way to avoid importing `annotated_types` at import time.r   r   c                   @  s   e Zd ZdZddœdd„ZdS )z7_general_metadata_cls.<locals>._PydanticGeneralMetadataz,Pydantic general metadata like `max_digits`.r	   r*   c                 S  s
   || _ d S ©N)Ú__dict__)Úselfr'   r    r    r&   Ú__init__<   s    z@_general_metadata_cls.<locals>._PydanticGeneralMetadata.__init__N)r!   r"   r#   r$   r0   r    r    r    r&   Ú_PydanticGeneralMetadata9   s   r1   )Úannotated_typesr   r   )r   r1   r    r    r&   r)   4   s    r)   z	type[Any]zdict[str, FieldInfo]r   ÚNone)ÚclsÚfieldsÚconfig_wrapperr(   c                 C  s@   |j r<t| ƒ}| ¡ D ]$\}}|jd kr||kr|| |_qd S r-   )Zuse_attribute_docstringsr   ÚitemsÚdescription)r4   r5   r6   Zfields_docsÚann_nameÚ
field_infor    r    r&   Ú_update_fields_from_docstringsB   s
    r;   )Útypevars_mapztype[BaseModel]ztuple[type[Any], ...]zNsResolver | Nonezdict[Any, Any] | Nonez%tuple[dict[str, FieldInfo], set[str]])r4   Úbasesr6   Úns_resolverr<   r(   c                C  sÒ  t ƒ }tƒ }i }t|ƒD ]}t|ddƒ }	r| |	¡ qtj| |d�}
| j di ¡}i }t	ƒ }|
 
¡ D �]4\}\}}|dkr~qf|jD �]}d}t|tƒr¨| |¡dk	}nt|tƒr¼| |¡}|r„|D ]J}t||ƒrÄt||ƒrì|t|di ƒksÄtd|› dt||ƒ› d	|› d
�ƒ‚qÄd}|jD ]J}t|tƒ�rL| |¡�sb|d|j› d�f7 }n| |¡�s||f7 }�qt d|› d| j› d|› d|› d�	t¡ q„t |¡�r¨| |¡ qft|t| |tƒƒ�rÈ| |¡ qft|ƒ�sÔqf| j�rö|dk�rötd|›d�ƒ‚t| di ƒ d¡}|D ]„}dd„ t  |¡�r,t !|¡ndD ƒ}t||ƒ�r||k�rN�q||k�r\�q||k�rj�qt d|› d| j"› d|j"› d�t¡ �qzt| |tƒ}|tk�r®t#‚W nZ t#k
�r
   ||k�rÞ| $|¡}||_%n(||k�röt&|| ƒ}n| $|¡}||_%Y nlX t'||ƒ t||ƒ�r>t(|j)ƒ�r>|j) *d| ¡|_)| +||¡}||_%zt,| |ƒ W n t#k
�rt   Y nX | jd }||j-k�r”t.dƒ‚|||< qf|�r¾| /¡ D ]}| 0|¡ �q¬t1| ||ƒ ||fS )ab  Collect the fields of a nascent pydantic model.

    Also collect the names of any ClassVars present in the type hints.

    The returned value is a tuple of two items: the fields dict, and the set of ClassVar names.

    Args:
        cls: BaseModel or dataclass.
        bases: Parents of the class, generally `cls.__bases__`.
        config_wrapper: The config wrapper instance.
        ns_resolver: Namespace resolver to use when getting model annotations.
        typevars_map: A dictionary mapping type variables to their concrete types.

    Returns:
        A tuple contains fields and class variables.

    Raises:
        NameError:
            - If there is a conflict between a field name and protected namespaces.
            - If there is a field other than `root` in `RootModel`.
            - If a field shadows an attribute in the parent model.
    Z__pydantic_fields__N)r>   Ú__annotations__Zmodel_configFzField "z" conflicts with member z of protected namespace "z".r    zre.compile(ú)z" in z( has conflict with protected namespace "z_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = z`.ÚrootzUnexpected field with name z4; only 'root' is allowed as a field of a `RootModel`Z__pydantic_generic_metadata__Úoriginc                 S  s   h | ]
}|j ’qS r    ©Úname)Ú.0Úfieldr    r    r&   Ú	<setcomp>¯   s    z'collect_model_fields.<locals>.<setcomp>zField name "z" in "z"" shadows an attribute in parent "ú"Z__pydantic_decorators__z0you can't override a field with a computed field)2r   r   ÚreversedÚgetattrÚupdater   Zget_model_type_hintsr.   ÚgetÚsetr7   Zprotected_namespacesÚ
isinstancer   ÚmatchÚstrÚ
startswithÚhasattrÚ
issubclassÚ	NameErrorÚpatternÚwarningsÚwarnr!   ÚUserWarningÚis_classvar_annotationÚaddÚ_is_finalvar_with_default_valr   Úis_valid_field_nameZ__pydantic_root_model__ÚdataclassesÚis_dataclassr5   r#   ÚAttributeErrorZfrom_annotationÚ	evaluatedr   Ú#_warn_on_nested_alias_in_annotationr   ÚdefaultÚ__get__Úfrom_annotated_attributeÚdelattrZcomputed_fieldsÚ
ValueErrorÚvaluesÚapply_typevars_mapr;   )r4   r=   r6   r>   r<   r   Ú
FieldInfo_Zparent_fields_lookupÚbaseZmodel_fieldsZ
type_hintsr   r5   Z
class_varsr9   Úann_typer`   Zprotected_namespaceZns_violationÚbZvalid_namespacesZpnZgeneric_originÚdataclass_fieldsrb   r:   Z
decoratorsrF   r    r    r&   Úcollect_model_fieldsJ   s¾    



ÿ
ü



ÿÿ


ý







rn   rP   )rk   r9   r(   c                 C  sn   t ƒ }t| dd ƒ}|rj|D ]N}t |¡rt |¡D ]4}t||ƒr2|jd k	r2t d|› d�t	¡   d S q2qd S )NÚ__args__z `alias` specification on field "z5" must be set on outermost annotation to take effect.)
r   rJ   r   Zis_annotatedÚget_argsrN   ÚaliasrV   rW   rX   )rk   r9   r   ÚargsZanno_argZanno_type_argr    r    r&   ra   ý   s    

þra   Úbool)Útype_Úvalr(   c                 C  sJ   t ƒ }t | ¡sdS |tkr dS t||ƒrB|jtkrB|jd krBdS dS d S )NFT)r   r   Zis_finalvarr   rN   rb   Údefault_factory)rt   ru   r   r    r    r&   r[     s    
r[   )r>   r<   r6   ztype[StandardDataclass]zConfigWrapper | None)r4   r>   r<   r6   r(   c             
   C  s|  t ƒ }i }|ptƒ }| j}t| jƒD �]}t |¡s6q$| |¡�ú | ¡ D ]ê\}}	||j	 
di ¡krfqJ|j\}
}t |	j|
|¡\}}t |¡r�qJ|	js°|	jtjkr°|	jtjkr°qJt|	j|ƒrö|	jjræ|	jjdkrJtd|› d�dd�‚qJ| ||	j¡}n| ||	¡}|||< |jtk	rJtt| ||ƒ|ƒrJt| ||jƒ qJW 5 Q R X q$|�rb| ¡ D ]}| |¡ �qP|dk	�rxt| ||ƒ |S )a|  Collect the fields of a dataclass.

    Args:
        cls: dataclass.
        ns_resolver: Namespace resolver to use when getting dataclass annotations.
            Defaults to an empty instance.
        typevars_map: A dictionary mapping type variables to their concrete types.
        config_wrapper: The config wrapper instance.

    Returns:
        The dataclass fields.
    r?   FzDataclass field zD has init=False and init_var=True, but these are mutually exclusive.zclashing-init-and-init-var)ÚcodeN)r   r   Z__dataclass_fields__rI   Ú__mro__r]   r^   Úpushr7   r.   rL   Ztypes_namespacer   Ztry_eval_typeÚtyperY   Úinitrb   ÚMISSINGrv   rN   Zinit_varr   rd   r   rJ   Úsetattrrg   rh   r;   )r4   r>   r<   r6   ri   r5   rm   rj   r9   Zdataclass_fieldÚglobalnsÚlocalnsrk   Ú_r:   rF   r    r    r&   Úcollect_dataclass_fields  sX    



ÿ
þ
ý
þ
 ÿ
r�   )rD   r(   c                 C  s   |   d¡ S )Nr€   ©rQ   rC   r    r    r&   r\   s  s    r\   c                 C  s   |   d¡o|   d¡ S )Nr€   Ú__r‚   rC   r    r    r&   Úis_valid_privateattr_namew  s    r„   z3Callable[[], Any] | Callable[[dict[str, Any]], Any]z'TypeIs[Callable[[dict[str, Any]], Any]])rv   r(   c              	   C  s^   zt | ƒ}W n ttfk
r&   Y dS X t|j ¡ ƒ}t|ƒdko\t|d ƒo\|d jt	j
kS )zMWhether the provided default factory callable has a validated data parameter.Fr   r   )r   rf   Ú	TypeErrorÚlistÚ
parametersrg   Úlenr   rb   r   Úempty)rv   Úsigr‡   r    r    r&   Útakes_validated_data_argument{  s    r‹   )<r$   Ú
__future__r   Z_annotationsr]   rV   r   Ú	functoolsr   Úinspectr   r   r   Útypingr   r	   r
   r   Zpydantic_corer   Ztyping_extensionsr   Zpydantic.errorsr   Ú r   Ú_configr   Z_docs_extractionr   Z_import_utilsr   r   Z_namespace_utilsr   Ú_reprr   Ú_utilsr   r2   r   r5   r   Úmainr   Z_dataclassesr   Z_decoratorsr   r   r+   r)   r;   rn   ra   r[   r�   r\   r„   r‹   r    r    r    r&   Ú<module>   sP   ú 4ûY