o
    if3                     @  s  U d Z ddlmZ ddlZddlZddlZddlZddl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 ddlmZmZ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!m"Z" d
dlm#Z# ddl$m%Z%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l0m1Z1 ddl2m3Z3 ddl4m5Z5 erddl6m7Z8 d
dl9m:Z: G dd de8eZ;	d<d=d$d%Z<d&dd'd(d>d-d.Z=d?d0d1Z>d@d5d6Z?e@eAejBe f ZCd7eDd8< edAd:d;ZEdS )Bz0Private logic for creating pydantic dataclasses.    )annotationsN)	Generator)contextmanager)partial)TYPE_CHECKINGAnyClassVarProtocolcast)
ArgsKwargsSchemaSerializerSchemaValidatorcore_schema)	TypeAliasTypeIs   )PydanticUndefinedAnnotation)	FieldInfo)PluggableSchemaValidatorcreate_schema_validator)PydanticDeprecatedSince20   )_config_decorators)collect_dataclass_fields)GenerateSchemaInvalidSchemaError)get_standard_typevars_map)set_dataclass_mocks)
NsResolver)generate_pydantic_signature)LazyClassAttribute)DataclassInstance)
ConfigDictc                   @  sX   e Zd ZU dZded< ded< ded< ded	< d
ed< ded< ded< edddZdS )PydanticDataclassai  A protocol containing attributes only available once a class has been decorated as a Pydantic dataclass.

        Attributes:
            __pydantic_config__: Pydantic-specific configuration settings for the dataclass.
            __pydantic_complete__: Whether dataclass building is completed, or if there are still undefined fields.
            __pydantic_core_schema__: The pydantic-core schema used to build the SchemaValidator and SchemaSerializer.
            __pydantic_decorators__: Metadata containing the decorators defined on the dataclass.
            __pydantic_fields__: Metadata about the fields defined on the dataclass.
            __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the dataclass.
            __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the dataclass.
        zClassVar[ConfigDict]__pydantic_config__zClassVar[bool]__pydantic_complete__z ClassVar[core_schema.CoreSchema]__pydantic_core_schema__z$ClassVar[_decorators.DecoratorInfos]Z__pydantic_decorators__zClassVar[dict[str, FieldInfo]]__pydantic_fields__zClassVar[SchemaSerializer]__pydantic_serializer__z4ClassVar[SchemaValidator | PluggableSchemaValidator]__pydantic_validator__returnboolc                 C  s   d S N clsr.   r.   Y/home/kim/smarthome/.venv/lib/python3.10/site-packages/pydantic/_internal/_dataclasses.py__pydantic_fields_complete__=   s   z.PydanticDataclass.__pydantic_fields_complete__N)r+   r,   )__name__
__module____qualname____doc____annotations__classmethodr2   r.   r.   r.   r1   r$   (   s   
 r$   r0   type[StandardDataclass]config_wrapper_config.ConfigWrapperns_resolverNsResolver | Noner+   Nonec                 C  s"   t | }t| |||d}|| _dS )zCollect and set `cls.__pydantic_fields__`.

    Args:
        cls: The class.
        config_wrapper: The config wrapper instance.
        ns_resolver: Namespace resolver to use when getting dataclass annotations.
    )r<   typevars_mapr:   N)r   r   r(   )r0   r:   r<   r?   fieldsr.   r.   r1   set_dataclass_fieldsA   s
   
rA   TF)raise_errorsr<   _force_build	type[Any]rB   r,   rC   c             
   C  sj  | j }ddd	}| j d
|_|| _ |j| _t| ||d |s(|jr(t|  dS t| dr3t	dt
 t| }t|||d}tdtt|| j|j|jdd| _z|| }	W n  tyv }
 z|ra t| d|
j d W Y d}
~
dS d}
~
ww |j| jd}z||	}	W n ty   t|  Y dS w td| } |	| _t|	| | j| jd||j| _ t!|	|| _"d| _#dS )a  Finish building a pydantic dataclass.

    This logic is called on a class which has already been wrapped in `dataclasses.dataclass()`.

    This is somewhat analogous to `pydantic._internal._model_construction.complete_model_class`.

    Args:
        cls: The class.
        config_wrapper: The config wrapper instance.
        raise_errors: Whether to raise errors, defaults to `True`.
        ns_resolver: The namespace resolver instance to use when collecting dataclass fields
            and during schema building.
        _force_build: Whether to force building the dataclass, no matter if
            [`defer_build`][pydantic.config.ConfigDict.defer_build] is set.

    Returns:
        `True` if building a pydantic dataclass is successfully completed, `False` otherwise.

    Raises:
        PydanticUndefinedAnnotation: If `raise_error` is `True` and there is an undefined annotations.
    __dataclass_self__r$   argsr   kwargsr+   r>   c                 _  s"   d}| }|j jt|||d d S )NT)Zself_instance)r*   Zvalidate_pythonr   )rE   rF   rG   Z__tracebackhide__sr.   r.   r1   __init__v   s   z$complete_dataclass.<locals>.__init__z	.__init__)r:   r<   FZ__post_init_post_parse__zVSupport for `__post_init_post_parse__` has been dropped, the method will not be called)r<   r?   __signature__T)initr@   validate_by_nameextrais_dataclass`N)titleztype[PydanticDataclass]	dataclass)rE   r$   rF   r   rG   r   r+   r>   )$rI   r5   Zconfig_dictr%   rA   Zdefer_buildr   hasattrwarningswarnr   r   r   r!   r   r    r(   rL   rM   rJ   Zgenerate_schemar   namecore_configr3   Zclean_schemar   r
   r'   r   r4   Zplugin_settingsr*   r   r)   r&   )r0   r:   rB   r<   rC   Zoriginal_initrI   r?   Z
gen_schemaZschemaerV   r.   r.   r1   complete_dataclassU   sn   


	
rX   TypeIs[type[StandardDataclass]]c                C  s   d| j v o
t| d S )af  Returns `True` if the class is a stdlib dataclass and *not* a Pydantic dataclass.

    Unlike the stdlib `dataclasses.is_dataclass()` function, this does *not* include subclasses
    of a dataclass that are themselves not dataclasses.

    Args:
        cls: The class.

    Returns:
        `True` if the class is a stdlib dataclass, `False` otherwise.
    __dataclass_fields__r*   )__dict__rR   r/   r.   r.   r1   is_stdlib_dataclass   s   r\   pydantic_fieldr   dataclasses.Field[Any]c                 C  sb   d| i}t jdkr| jd ur| j|d< t jdkr| jrd|d< | jdur)| j|d< tjdi |S )	Ndefault)      docr`   
   Tkw_onlyreprr.   )sysversion_infodescriptionre   rf   dataclassesfield)r]   Z
field_argsr.   r.   r1   as_dataclass_field   s   


rl   r   DcFieldsGenerator[None]c                 c  s   g }| j dd D ]G}|jdi }dd | D }|rQ|||f | D ]'\}}tt|j}t|}t	j
dkrC|jrCd|_|jdurL|j|_|||< q)q
zdV  W |D ]\}	}
|
 D ]\}}||	|< qaqYdS |D ]\}	}
|
 D ]\}}||	|< qwqow )a  Temporarily patch the stdlib dataclasses bases of `cls` if the Pydantic `Field()` function is used.

    When creating a Pydantic dataclass, it is possible to inherit from stdlib dataclasses, where
    the Pydantic `Field()` function is used. To create this Pydantic dataclass, we first apply
    the stdlib `@dataclass` decorator on it. During the construction of the stdlib dataclass,
    the `kw_only` and `repr` field arguments need to be understood by the stdlib *during* the
    dataclass construction. To do so, we temporarily patch the fields dictionary of the affected
    bases.

    For instance, with the following example:

    ```python {test="skip" lint="skip"}
    import dataclasses as stdlib_dc

    import pydantic
    import pydantic.dataclasses as pydantic_dc

    @stdlib_dc.dataclass
    class A:
        a: int = pydantic.Field(repr=False)

    # Notice that the `repr` attribute of the dataclass field is `True`:
    A.__dataclass_fields__['a']
    #> dataclass.Field(default=FieldInfo(repr=False), repr=True, ...)

    @pydantic_dc.dataclass
    class B(A):
        b: int = pydantic.Field(repr=False)
    ```

    When passing `B` to the stdlib `@dataclass` decorator, it will look for fields in the parent classes
    and reuse them directly. When this context manager is active, `A` will be temporarily patched to be
    equivalent to:

    ```python {test="skip" lint="skip"}
    @stdlib_dc.dataclass
    class A:
        a: int = stdlib_dc.field(default=Field(repr=False), repr=False)
    ```

    !!! note
        This is only applied to the bases of `cls`, and not `cls` itself. The reason is that the Pydantic
        dataclass decorator "owns" `cls` (in the previous example, `B`). As such, we instead modify the fields
        directly (in the previous example, we simply do `setattr(B, 'b', as_dataclass_field(pydantic_field))`).

    !!! note
        This approach is far from ideal, and can probably be the source of unwanted side effects/race conditions.
        The previous implemented approach was mutating the `__annotations__` dict of `cls`, which is no longer a
        safe operation in Python 3.14+, and resulted in unexpected behavior with field ordering anyway.
    r   NrZ   c                 S  sB   i | ]\}}t |jtr|jjd us|jjs|jjdur||qS )NT)
isinstancer_   r   ri   re   rf   ).0
field_namerk   r.   r.   r1   
<dictcomp>   s    
 z%patch_base_fields.<locals>.<dictcomp>rc   T)__mro__r[   getitemsappendr
   r   r_   copyrg   rh   re   rf   )r0   Zoriginal_fields_listbaseZ	dc_fieldsZ&dc_fields_with_pydantic_field_defaultsrq   rk   r_   Znew_dc_fieldr@   Zoriginal_fieldsZoriginal_fieldr.   r.   r1   patch_base_fields   s:   7




ry   r-   )r0   r9   r:   r;   r<   r=   r+   r>   )r0   rD   r:   r;   rB   r,   r<   r=   rC   r,   r+   r,   )r0   rD   r+   rY   )r]   r   r+   r^   )r0   rD   r+   rn   )Fr6   
__future__r   Z_annotationsrw   rj   rg   rS   collections.abcr   
contextlibr   	functoolsr   typingr   r   r   r	   r
   Zpydantic_corer   r   r   r   Ztyping_extensionsr   r   errorsr   r@   r   Zplugin._schema_validatorr   r   r    r   r   _fieldsr   Z_generate_schemar   r   Z	_genericsr   Z_mock_val_serr   Z_namespace_utilsr   
_signaturer    Z_utilsr!   Z	_typeshedr"   ZStandardDataclassconfigr#   r$   rA   rX   r\   rl   dictstrFieldrm   r7   ry   r.   r.   r.   r1   <module>   sN    
l
