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]__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/   [/var/www/html/ocr-read/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 )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)   )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  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rm   
   Tkw_only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
