o
    im                     @  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 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)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0m1Z1 erddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z: ddl;m<Z< G dd de.Z=dkd#d$Z>e	dld&d'Z?dmd0d1Z@dndod9d:ZAdpd@dAZBdqdDdEZCdrdHdIZDh dJZEh dKZFddLdsdSdTZGdtdWdXZHdddYdud\d]ZIdvd_d`ZJdwdbdcZKdwdddeZLdxdidjZMdS )yzmPrivate logic related to fields (the `Field()` function and `FieldInfo` class), and arguments to `Annotated`.    )annotationsN)Mapping)cache)	Parameterismethoddescriptor	signature)Pattern)TYPE_CHECKINGAnyCallableTypeVar)PydanticUndefined)TypeIs)AnnotationSource)PydanticDeprecatedSince211)PydanticUserError   )AliasGenerator   )	_generics_typing_extra)ConfigWrapper)extract_docstrings_from_cls)import_cached_base_modelimport_cached_field_info)
NsResolver)Representation)can_be_positionalget_first_not_noneBaseMetadata)	FieldInfo)	BaseModel)PydanticDataclass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'   T/home/kim/smarthome/.venv/lib/python3.10/site-packages/pydantic/_internal/_fields.pyr&   &   s    r&   metadatar
   returnr    c                  K  s
   t  | S )zCreate 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   
	r1   type[BaseMetadata]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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)   r*   r+   r6   r'   r'   r'   r-   _PydanticGeneralMetadata=   s    r7   )annotated_typesr    r&   )r    r7   r'   r'   r-   r0   8   s   r0   protected_namespacestuple[str | Pattern[str], ...]ann_namestrbasestuple[type[Any], ...]cls_nameNonec                 C  s<  t  }| D ]}d}t|tr||d u}n
t|tr ||}|r|D ]%}t||rIt||r8|t|di v sIt	d|dt|| d|dq$g }| D ]%}	t|	tre|	|sd|
d|	jd qN||	ss|
d	|	 d	 qNd
d| t|dkrdnd }
tjd|d|d|d|
 d	tdd qd S )NF__pydantic_fields__Field z conflicts with member z of protected namespace .zre.compile()'(z, r   z,)z in z$ conflicts with protected namespace zX.

You may be able to solve this by setting the 'protected_namespaces' configuration to    
stacklevel)r   
isinstancer   matchr<   
startswithhasattr
issubclassgetattr
ValueErrorappendpatternjoinlenwarningswarnUserWarning)r9   r;   r=   r?   r"   Zprotected_namespaceZns_violationbZvalid_namespacesZpnZvalid_namespaces_strr'   r'   r-   _check_protected_namespacesF   sH   






$rY   Fcls	type[Any]fieldsdict[str, FieldInfo]use_inspectboolc                 C  s>   t | |d}| D ]\}}|jd u r||v r|| |_q
d S )Nr^   )r   itemsdescription)rZ   r\   r^   Zfields_docsr;   
field_infor'   r'   r-   _update_fields_from_docstringsq   s   
rd   title_generatorCallable[[str, FieldInfo], str]
field_namerc   r!   c                 C  sB   |j d u r| ||}t|tstd|  d|j ||_ d S d S )Nzfield_title_generator  must return str, not )titlerJ   r<   	TypeError	__class__)re   rg   rc   ri   r'   r'   r-   *_apply_field_title_generator_to_field_infox   s   



rl   alias_generator%Callable[[str], str] | AliasGeneratorc                 C  s  |j du s|j dks|jdu s|jdu s|jdu rd\}}}t| tr,| |\}}}nt| rD| |}t|tsDt	d|  d|j
 |j du sN|j dkrQd|_ |j dkret|||_t|||_||_|jdu rm||_|jdu rxt|||_|jdu rt|||_dS dS dS )av  Apply an alias generator to aliases on a `FieldInfo` instance if appropriate.

    Args:
        alias_generator: A callable that takes a string and returns a string, or an `AliasGenerator` instance.
        field_name: The name of the field from which to generate the alias.
        field_info: The `FieldInfo` instance to which the alias generator is (maybe) applied.
    Nr   )NNNzalias_generator rh   )Zalias_priorityaliasvalidation_aliasserialization_aliasrJ   r   Zgenerate_aliasescallabler<   rj   rk   r   )rm   rg   rc   ro   rp   rq   r'   r'   r-   $_apply_alias_generator_to_field_info   s4   











rs   config_wrapperr   c                 C  s@   |j p| j }|durt||| | jdurt| j|| dS dS )ap  Update the `FieldInfo` instance from the configuration set on the model it belongs to.

    This will apply the title and alias generators from the configuration.

    Args:
        config_wrapper: The configuration from the model.
        field_name: The field name the `FieldInfo` instance is attached to.
        field_info: The `FieldInfo` instance to update.
    N)field_title_generatorrl   rm   rs   )rt   rg   rc   ru   r'   r'   r-   update_field_from_config   s   

rv   >   dictjsonZ_iterZ_copy_and_set_valuescopyZ_calculate_keys>
   Z
parse_fileZupdate_forward_refsZschemaZfrom_orm
_get_valuevalidateZ	parse_rawZschema_jsonZ	parse_objZ	construct)typevars_maptype[BaseModel]ns_resolverNsResolver | Noner|   Mapping[TypeVar, Any] | None%tuple[dict[str, FieldInfo], set[str]]c             
     sT  t  }t  | j}i }t|D ]}t|dd }r|| qtj| |d}	t| }
i }t	 }|	
 D ]S\}\}}|dkr@q4t|j||| jd t|rU|| q4t| |ttur~t fddtD s|tdr~t fd	dtD r~tt|sq4| jr|d
krtd|dt| di d}|D ];}dd t|rt|ndD }t||r||u rq||v rq||
vrqtjd| d| j d|j dtdd qtu r||
v s||vr|j |t!j"d}|sd|_#||_$nm|| % }nft&|rt'j(rj()d| }|_(|j*d< |j+|t!j"d}|_,|s.d|_#||_$n#d|j-v rQ|. sQtjd|d|d|dt/dd  || q4zt0| | W n
 t1yb   Y nw | j2d! }||j3v rzt4d"|d#| jd$|||< |j#rt5||| q4|r|6 D ]}|j#r|7| q|j8rt9| | ||fS )%a*  Collect the fields and class variables names of a nascent Pydantic model.

    The fields collection process is *lenient*, meaning it won't error if string annotations
    fail to evaluate. If this happens, the original annotation (and assigned value, if any)
    is stored on the created `FieldInfo` instance.

    The `rebuild_model_fields()` should be called at a later point (e.g. when rebuilding the model),
    and will make use of these stored attributes.

    Args:
        cls: BaseModel or dataclass.
        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 two-tuple containing model fields and class variables names.

    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.
    rA   N)r~   Zmodel_config)r9   r;   r=   r?   c                 3  s     | ]}t  |d u V  qd S r3   )rO   .0Z	depr_nameZ
BaseModel_Zassigned_valuer'   r-   	<genexpr>  s    z'collect_model_fields.<locals>.<genexpr>__func__c                 3  s*    | ]}t t  |d dd ju V  qd S )Nr   )rO   r   r   r   r'   r-   r   "  s
    
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)r   fieldr'   r'   r-   	<setcomp>6  s    z'collect_model_fields.<locals>.<setcomp>r'   zField name "z" in "z"" shadows an attribute in parent ""   rH   _sourceFdefaultfinalzAnnotation z= is marked as final and has a default value. Pydantic treats zz as a class variable, but it will be considered as a normal field in V3 to be aligned with dataclasses. If you still want zV to be considered as a class variable, annotate it as: `ClassVar[<type>] = <default>.`)categoryrI   Z__pydantic_decorators__rB   z
 of class zf overrides symbol of same name in a parent class. This override with a computed_field is incompatible.):r   r   	__bases__reversedrO   updater   Zget_model_type_hintssafe_get_annotationssetra   rY   r9   r(   is_classvar_annotationaddr   any_deprecated_method_namesrM   _deprecated_classmethod_namesis_valid_field_nameZ__pydantic_root_model__	NameErrorgetdataclassesis_dataclassr\   rU   rV   r*   rW   from_annotationr   CLASS	_complete_original_annotationZ_copyrJ   r   r   __get__Z_attributes_setfrom_annotated_attribute_original_assignmentZ_qualifiersZis_requiredr   delattrAttributeErrorr4   Zcomputed_fieldsrj   rv   valuesapply_typevars_mapuse_attribute_docstringsrd   )rZ   rt   r~   r|   
FieldInfo_r=   Zparent_fields_lookupbaseZmodel_fieldsZ
type_hintsr   r\   Z
class_varsr;   ann_type	evaluatedZgeneric_origindataclass_fieldsrc   r   Z
decoratorsr   r'   r   r-   collect_model_fields   s   










r   r   Mapping[TypeVar, Any]c                C  s   t  }i }|| ] | j D ]N\}}|jr|||< q|j}tj|jg|j	R  }	t
|	|}	|j }
tu r@|j|	tjd}n	|j|	|
tjd}|jdurQ|jn||_t||| |||< qW d   |S 1 sjw   Y  |S )a  Rebuild the (already present) model fields by trying to reevaluate annotations.

    This function should be called whenever a model with incomplete fields is encountered.

    Raises:
        NameError: If one of the annotations failed to evaluate.

    Note:
        This function *doesn't* mutate the model fields in place, as it can be called during
        schema generation, where you don't want to mutate other model's fields.
    r   N)r   pushrA   ra   r   rb   r   	eval_typer   types_namespacer   replace_typesr   r   r   r   r   r   rv   )rZ   rt   r~   r|   r   rebuilt_fieldsf_namerc   existing_descannZassign	new_fieldr'   r'   r-   rebuild_model_fields  s0   


r   )r~   r|   type[StandardDataclass]dict[Any, Any] | Nonec             	   C  s  t  }i }|p	t }| j}t| jD ]}t|sq|| | D ]\}}	t	
|}
||
vr2q$|j\}}t	|	j||\}}t	|rGq$|	jsW|	jtju rW|	jtju rWq$t|	j|r|	jjrr|	jjdu rqtd| dddq$|j||	jtjd}|	j|_n|j||	tjd}|	|_|sd|_||_|||< t||| |jturtt| |||rt| ||j q$W d   n1 sw   Y  q|r|  D ]}|!| q|j"rt#| |t$| d d	 |S )
a|  Collect the fields of a dataclass.

    Args:
        cls: dataclass.
        config_wrapper: The config wrapper instance.
        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.

    Returns:
        The dataclass fields.
    FzDataclass field zD has init=False and init_var=True, but these are mutually exclusive.zclashing-init-and-init-var)coder   NZ__is_pydantic_dataclass__r`   )%r   r   __dataclass_fields__r   __mro__r   r   r   ra   r   r   r   Ztry_eval_typetyper   initr   MISSINGdefault_factoryrJ   Zinit_varr   r   r   	DATACLASSr   r   r   rv   r   rO   setattrr   r   r   rd   rM   )rZ   rt   r~   r|   r   r\   r   r   r;   Zdataclass_fieldZ	base_annsglobalnslocalnsr   r   rc   r   r'   r'   r-   collect_dataclass_fields  sx   







9
r   type[PydanticDataclass]c                C  s   t  }i }|| N | j D ]?\}}|jr|||< q|j}tj|jg|j	R  }	t
|	|}	|j|	|jtjd}
|
jdurB|
jn||
_t|||
 |
||< qW d   |S 1 s[w   Y  |S )a  Rebuild the (already present) dataclass fields by trying to reevaluate annotations.

    This function should be called whenever a dataclass with incomplete fields is encountered.

    Raises:
        NameError: If one of the annotations failed to evaluate.

    Note:
        This function *doesn't* mutate the dataclass fields in place, as it can be called during
        schema generation, where you don't want to mutate other dataclass's fields.
    r   N)r   r   rA   ra   r   rb   r   r   r   r   r   r   r   r   r   r   rv   )rZ   rt   r~   r|   r   r   r   rc   r   r   r   r'   r'   r-   rebuild_dataclass_fields8  s4   


r   r   c                 C  s   |  d S )N_rL   r   r'   r'   r-   r   f  s   r   c                 C  s   |  do
|  d S )Nr   __r   r   r'   r'   r-   is_valid_privateattr_namej  s   r   r   3Callable[[], Any] | Callable[[dict[str, Any]], Any]'TypeIs[Callable[[dict[str, Any]], Any]]c              	   C  s\   zt | }W n ttfy   Y dS w t|j }t|dko-t|d o-|d jt	j
u S )zMWhether the provided default factory callable has a validated data parameter.Fr   r   )r   rP   rj   list
parametersr   rT   r   r   r   empty)r   sigr   r'   r'   r-   takes_validated_data_argumentn  s   (r   )r.   r
   r/   r    )r/   r2   )
r9   r:   r;   r<   r=   r>   r?   r<   r/   r@   )F)rZ   r[   r\   r]   r^   r_   r/   r@   )re   rf   rg   r<   rc   r!   )rm   rn   rg   r<   rc   r!   )rt   r   rg   r<   rc   r!   r/   r@   )
rZ   r}   rt   r   r~   r   r|   r   r/   r   )
rZ   r}   rt   r   r~   r   r|   r   r/   r]   )
rZ   r   rt   r   r~   r   r|   r   r/   r]   )
rZ   r   rt   r   r~   r   r|   r   r/   r]   )r   r<   r/   r_   )r   r   r/   r   )Nr+   
__future__r   Z_annotationsr   rU   collections.abcr   	functoolsr   inspectr   r   r   rer   typingr	   r
   r   r   Zpydantic_corer   Ztyping_extensionsr   Ztyping_inspection.introspectionr   Zpydanticr   Zpydantic.errorsr   aliasesr    r   r   _configr   Z_docs_extractionr   Z_import_utilsr   r   Z_namespace_utilsr   _reprr   Z_utilsr   r   r8   r    r\   r!   mainr"   Z_dataclassesr#   r$   Z_decoratorsr%   r&   r1   r0   rY   rd   rl   rs   rv   r   r   r   r   r   r   r   r   r   r'   r'   r'   r-   <module>   sd    

+


2 
H1
l
.
