U
    nËj%  ã                   @  s–  d Z ddlmZ ddlZddlZddlZddlmZm	Z	 ddl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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! 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+ ej,�rFddl-m.Z/ ddl0m1Z1 ddl2m3Z3 G dd„ de/ej4ƒZ5neZ6d/dddddœd d!„Z7d"dd#d$œd%d&d'dd'd'd(œd)d*„Z8d%d+d,œd-d.„Z9dS )0z0Private logic for creating pydantic dataclasses.é    )ÚannotationsN)ÚpartialÚwraps)ÚAnyÚClassVar)Ú
ArgsKwargsÚSchemaSerializerÚSchemaValidatorÚcore_schema)Ú	TypeGuardé   )ÚPydanticUndefinedAnnotation)ÚPluggableSchemaValidatorÚcreate_schema_validator)ÚPydanticDeprecatedSince20é   )Ú_configÚ_decorators)Úcollect_dataclass_fields)ÚGenerateSchema)Úget_standard_typevars_map)Úset_dataclass_mocks)Ú
NsResolver)ÚCallbackGetCoreSchemaHandler)Úgenerate_pydantic_signature)ÚLazyClassAttribute)ÚDataclassInstance)Ú
ConfigDict)Ú	FieldInfoc                   @  sJ   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< 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__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© r+   r+   úC/tmp/pip-unpacked-wheel-zvl9l234/pydantic/_internal/_dataclasses.pyr   &   s   
r   ztype[StandardDataclass]zNsResolver | Nonez_config.ConfigWrapper | NoneÚNone)ÚclsÚns_resolverÚconfig_wrapperÚreturnc                 C  s"   t | ƒ}t| |||d�}|| _dS )zîCollect and set `cls.__pydantic_fields__`.

    Args:
        cls: The class.
        ns_resolver: Namespace resolver to use when getting dataclass annotations.
        config_wrapper: The config wrapper instance, defaults to `None`.
    )r/   Útypevars_mapr0   N)r   r   r#   )r.   r/   r0   r2   Úfieldsr+   r+   r,   Úset_dataclass_fieldsA   s       ÿr4   TF)Úraise_errorsr/   Ú_force_buildz	type[Any]z_config.ConfigWrapperÚbool)r.   r0   r5   r/   r6   r1   c             
     s   | j }dddddœdd„}| j› d�|_|| _ |j| _t| ||d� |s^|jr^t| | jƒ d	S t| d
ƒrtt	 
dt¡ t| ƒ}t|||d�}tdtt|| j|j|jdd�ƒ| _t| ddƒ}	z6|	rÞ|	| tt|jd	d�|dd�ƒ}
n|j| d	d�}
W nJ tk
�r8 } z*|�r
‚ t| | jd|j› d�ƒ W Y ¢d	S d}~X Y nX |j| jd�}z| |
¡}
W n( |jk
�r~   t| | jdƒ Y d	S X t d| ¡} |
| _t |
| | j!| jd||j"ƒ | _#‰ t$|
|ƒ| _%|j&�röt'| j(ƒdddddœ‡ fdd„ƒ}| )d| ¡| _(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.
    r   r   r-   )Ú__dataclass_self__ÚargsÚkwargsr1   c                 _  s"   d}| }|j jt||ƒ|d� d S )NT)Zself_instance)r%   Zvalidate_pythonr   )r8   r9   r:   Z__tracebackhide__Úsr+   r+   r,   Ú__init__v   s    z$complete_dataclass.<locals>.__init__z	.__init__)r0   FZ__post_init_post_parse__zVSupport for `__post_init_post_parse__` has been dropped, the method will not be called)r/   r2   Ú__signature__T)Úinitr3   Úpopulate_by_nameÚextraÚis_dataclassZ__get_pydantic_core_schema__N)Zfrom_dunder_get_core_schemaÚunpack)Zref_modeú`)Útitlezall referenced typesztype[PydanticDataclass]Z	dataclassÚstr)ÚinstanceÚfieldÚvaluer1   c                  s   ˆ   | ||¡ d S )N)Úvalidate_assignment)rF   rG   rH   ©Z	validatorr+   r,   Úvalidated_setattrÊ   s    z-complete_dataclass.<locals>.validated_setattr)+r<   r(   Zconfig_dictr    r4   Zdefer_buildr   r&   ÚhasattrÚwarningsÚwarnÚDeprecationWarningr   r   r   r   r   r#   r?   r@   r=   Úgetattrr   Zgenerate_schemar   ÚnameÚcore_configZclean_schemaZCollectedInvalidÚtypingÚcastr"   r   r'   Zplugin_settingsr%   r   r$   rI   r   Ú__setattr__Ú__get__r!   )r.   r0   r5   r/   r6   Zoriginal_initr<   r2   Z
gen_schemaZget_core_schemaZschemaÚerR   rK   r+   rJ   r,   Úcomplete_dataclassU   s�    

 ÿý	øþýþ	      ÿ
rX   z"TypeGuard[type[StandardDataclass]])Ú_clsr1   c                 C  s2   t  | ¡o0t| dƒ o0t| jƒ tt| di ƒƒ¡S )aB  Returns True if a class is a stdlib dataclass and *not* a pydantic dataclass.

    We check that
    - `_cls` is a dataclass
    - `_cls` does not inherit from a processed pydantic dataclass (and thus have a `__pydantic_validator__`)
    - `_cls` does not have any annotations that are not dataclass fields
    e.g.
    ```python
    import dataclasses

    import pydantic.dataclasses

    @dataclasses.dataclass
    class A:
        x: int

    @pydantic.dataclasses.dataclass
    class B(A):
        y: int
    ```
    In this case, when we first check `B`, we make an extra check and look at the annotations ('y'),
    which won't be a superset of all the dataclass fields (only the stdlib fields i.e. 'x')

    Args:
        cls: The class.

    Returns:
        `True` if the class is a stdlib dataclass, `False` otherwise.
    r%   r*   )ÚdataclassesrA   rL   ÚsetZ__dataclass_fields__Ú
issupersetrP   )rY   r+   r+   r,   Úis_builtin_dataclassÔ   s
    

ÿýr]   )NN):r)   Ú
__future__r   Z_annotationsrZ   rS   rM   Ú	functoolsr   r   r   r   Zpydantic_corer   r   r	   r
   Ztyping_extensionsr   Úerrorsr   Zplugin._schema_validatorr   r   r   Ú r   r   Ú_fieldsr   Z_generate_schemar   Z	_genericsr   Z_mock_val_serr   Z_namespace_utilsr   Z_schema_generation_sharedr   Ú
_signaturer   Ú_utilsr   ÚTYPE_CHECKINGZ	_typeshedr   ZStandardDataclassÚconfigr   r3   r   ÚProtocolr   rO   r4   rX   r]   r+   r+   r+   r,   Ú<module>   sD     ýú