o
    í6Wj„3  ã                   @  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r²d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]Ú__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/   ún/home/esfera/Documents/content_generation/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__Úclassmethodr3   r/   r/   r/   r2   r$   (   s   
 r$   r1   útype[StandardDataclass]Úconfig_wrapperú_config.ConfigWrapperÚns_resolverúNsResolver | Noner,   ÚNonec                 C  s"   t | ƒ}t| |||d�}|| _dS )zÚCollect 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)   )r1   r;   r=   r@   Úfieldsr/   r/   r2   Úset_dataclass_fieldsA   s
   ÿ
rB   TF)Úraise_errorsr=   Ú_force_buildú	type[Any]rC   r-   rD   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)Úself_instance)r+   Úvalidate_pythonr   )rF   rG   rH   Ú__tracebackhide__Úsr/   r/   r2   Ú__init__v   s   z$complete_dataclass.<locals>.__init__z	.__init__)r;   r=   FÚ__post_init_post_parse__zVSupport for `__post_init_post_parse__` has been dropped, the method will not be called)r=   r@   Ú__signature__T)ÚinitrA   Úvalidate_by_nameÚextraÚis_dataclassú`N)Útitleztype[PydanticDataclass]Ú	dataclass)rF   r$   rG   r   rH   r   r,   r?   )$rM   r6   Úconfig_dictr%   rB   Údefer_buildr   ÚhasattrÚwarningsÚwarnr   r   r   r!   r   r    r)   rQ   rR   rO   Úgenerate_schemar   ÚnameÚcore_configr4   Úclean_schemar   r
   r'   r   r5   Úplugin_settingsr+   r   r*   r&   )r1   r;   rC   r=   rD   Úoriginal_initrM   r@   Ú
gen_schemaÚschemaÚer^   r/   r/   r2   Úcomplete_dataclassU   sn   


þý	øþ€üþ
ÿre   ú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__rY   r0   r/   r/   r2   Úis_stdlib_dataclassÁ   s   ri   Úpydantic_fieldr   údataclasses.Field[Any]c                 C  sh   d| i}t jdkr| jd ur| j|d< t jdkr"| jd ur"| j|d< | jdur,| j|d< tjdi |¤ŽS )	NÚdefault)é   é   Údoc©rm   é
   Úkw_onlyTÚreprr/   )ÚsysÚversion_infoÚdescriptionrr   rs   ÚdataclassesÚfield)rj   Ú
field_argsr/   r/   r2   Úas_dataclass_fieldÐ   s   



rz   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   Nrg   c                 S  sB   i | ]\}}t |jtƒr|jjd us|jjs|jjdur||“qS )NT)Ú
isinstancerl   r   rv   rr   rs   )Ú.0Ú
field_namerx   r/   r/   r2   Ú
<dictcomp>   s    
ý üz%patch_base_fields.<locals>.<dictcomp>rp   T)Ú__mro__rh   ÚgetÚitemsÚappendr
   r   rl   Úcopyrt   ru   rr   rs   )r1   Úoriginal_fields_listÚbaseÚ	dc_fieldsÚ&dc_fields_with_pydantic_field_defaultsr   rx   rl   Únew_dc_fieldrA   Úoriginal_fieldsÚoriginal_fieldr/   r/   r2   Úpatch_base_fieldså   s:   €7þ


€
ÿÿ
ÿÿr�   r.   )r1   r:   r;   r<   r=   r>   r,   r?   )r1   rE   r;   r<   rC   r-   r=   r>   rD   r-   r,   r-   )r1   rE   r,   rf   )rj   r   r,   rk   )r1   rE   r,   r|   )Fr7   Ú
__future__r   Ú_annotationsr…   rw   rt   rZ   Úcollections.abcr   Ú
contextlibr   Ú	functoolsr   Útypingr   r   r   r	   r
   Úpydantic_corer   r   r   r   Útyping_extensionsr   r   Úerrorsr   rA   r   Úplugin._schema_validatorr   r   r   Ú r   r   Ú_fieldsr   Ú_generate_schemar   r   Ú	_genericsr   Ú_mock_val_serr   Ú_namespace_utilsr   Ú
_signaturer    Ú_utilsr!   Ú	_typeshedr"   ÚStandardDataclassÚconfigr#   r$   rB   re   ri   rz   ÚdictÚstrÚFieldr{   r8   r�   r/   r/   r/   r2   Ú<module>   sN    ýú
l
