a
    “˜ëj„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�rfddl6m7Z8 d
dl9m:Z: G dd„ de8eƒZ;d9ddddd œd!d"„Z<d#dd$d%œd&dd'dd'd'd(œd)d*„Z=d&d+d,œd-d.„Z>d/d0d1œd2d3„Z?e@eAejBe f ZCd4eDd5< ed&d6d,œd7d8„ƒZEdS ):z0Private 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                   @  s\   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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__Úbool)Úreturnc                 C  s   d S )N© ©Úclsr-   r-   ú^/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pydantic/_internal/_dataclasses.pyÚ__pydantic_fields_complete__=   s    z.PydanticDataclass.__pydantic_fields_complete__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__Úclassmethodr1   r-   r-   r-   r0   r$   (   s   
r$   ztype[StandardDataclass]z_config.ConfigWrapperzNsResolver | NoneÚNone)r/   Úconfig_wrapperÚns_resolverr,   c                 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_mapr9   N)r   r   r(   )r/   r9   r:   r;   Úfieldsr-   r-   r0   Úset_dataclass_fieldsA   s
    ÿr=   TF)Úraise_errorsr:   Ú_force_buildz	type[Any]r+   )r/   r9   r>   r:   r?   r,   c             
   C  sx  | j }dddddœdd„}| j› d�|_|| _ |j| _t| ||d� |sZ|jrZt| ƒ d	S t| d
ƒrpt 	dt
¡ t| ƒ}t|||d�}tdtt|| j|j|jdd�ƒ| _z| | ¡}	W nB t� yø }
 z(|rÎ‚ t| d|
j› d�ƒ W Y d}
~
d	S d}
~
0 0 |j| jd�}z| |	¡}	W n t�y4   t| ƒ Y d	S 0 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.
    r$   r   r8   )Ú__dataclass_self__ÚargsÚkwargsr,   c                 _  s"   d}| }|j jt||ƒ|d� d S )NT)Zself_instance)r*   Zvalidate_pythonr   )r@   rA   rB   Z__tracebackhide__Úsr-   r-   r0   Ú__init__v   s    z$complete_dataclass.<locals>.__init__z	.__init__)r9   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ÚextraZis_dataclassú`N)Útitleztype[PydanticDataclass]Z	dataclass)$rD   r4   Zconfig_dictr%   r=   Zdefer_buildr   ÚhasattrÚwarningsÚwarnr   r   r   r!   r   r    r(   rG   rH   rE   Zgenerate_schemar   ÚnameÚcore_configr2   Zclean_schemar   r
   r'   r   r3   Zplugin_settingsr*   r   r)   r&   )r/   r9   r>   r:   r?   Zoriginal_initrD   r;   Z
gen_schemaZschemaÚerO   r-   r-   r0   Úcomplete_dataclassU   sh    

þý	øþ
ÿrQ   zTypeIs[type[StandardDataclass]])r/   r,   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__rK   r.   r-   r-   r0   Úis_stdlib_dataclassÁ   s    rT   r   zdataclasses.Field[Any])Úpydantic_fieldr,   c                 C  sh   d| i}t jdkr&| jd ur&| j|d< t jdkrD| jd urD| j|d< | jdurX| j|d< tjf i |¤ŽS )NÚdefault)é   é   Údoc©rW   é
   Úkw_onlyTÚrepr)ÚsysÚversion_infoÚdescriptionr\   r]   ÚdataclassesÚfield)rU   Z
field_argsr-   r-   r0   Úas_dataclass_fieldÐ   s    



rc   r   ÚDcFieldszGenerator[None]c                 c  s  g }| j dd… D ]Ž}|j di ¡}dd„ | ¡ D ƒ}|r| ||f¡ | ¡ D ]N\}}tt|jƒ}t |¡}t	j
dkr„|jr„d|_|jdur–|j|_|||< qPqz2dV  W |D ]"\}	}
|
 ¡ D ]\}}||	|< qÀq°n*|D ]"\}	}
|
 ¡ D ]\}}||	|< qêqÚ0 dS )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   NrR   c                 S  sB   i | ]:\}}t |jtƒr|jjd us8|jjs8|jjdur||“qS )NT)Ú
isinstancerV   r   r`   r\   r]   )Ú.0Ú
field_namerb   r-   r-   r0   Ú
<dictcomp>   s    üz%patch_base_fields.<locals>.<dictcomp>rZ   T)Ú__mro__rS   ÚgetÚitemsÚappendr
   r   rV   Úcopyr^   r_   r\   r]   )r/   Zoriginal_fields_listÚbaseZ	dc_fieldsZ&dc_fields_with_pydantic_field_defaultsrg   rb   rV   Znew_dc_fieldr<   Zoriginal_fieldsZoriginal_fieldr-   r-   r0   Úpatch_base_fieldså   s0    7þ

þro   )N)Fr5   Ú
__future__r   Z_annotationsrm   ra   r^   rL   Ú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    Ú_utilsr!   Z	_typeshedr"   ZStandardDataclassÚconfigr#   r$   r=   rQ   rT   rc   ÚdictÚstrZFieldrd   r6   ro   r-   r-   r-   r0   Ú<module>   sL    ýúl