HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/pydantic/v1/__pycache__/dataclasses.cpython-310.pyc
o

|��g�F�@sTdZddlZddlZddlZddlmZddlmZzddlmZWn	e	y+Ynwddl
mZmZm
Z
mZmZmZmZmZmZmZmZddlmZddlmZdd	lmZmZmZmZdd
lm Z ddl!m"Z"ddl#m$Z$m%Z%m&Z&m'Z'dd
l(m)Z)m*Z*ddl+m,Z,er�ddl(m-Z-ddl.m/Z/m0Z0eddd�Z1eeddfZ2Gdd�d�Z3gd�Z4ed�Z5ej6dk�r;eej7e$fd�eddddddddddd�
de8de8d e8d!e8d"e8d#e8d$eeee9dfd%ee8d&ee8d'e8d(e
ee5gd)ffd*d+���Z:eej7e$fd�eddddddddddd�
d,ee5de8de8d e8d!e8d"e8d#e8d$eeee9dfd%ee8d&ee8d'e8d(d)fd-d+���Z:n}eej7e$fd�edddddddddd.�	de8de8d e8d!e8d"e8d#e8d$eeee9dfd%ee8d&ee8d(e
ee5gd)ffd/d+���Z:eej7e$fd�edddddddddd.�	d,ee5de8de8d e8d!e8d"e8d#e8d$eeee9dfd%ee8d&ee8d(d)fd0d+���Z:eej7e$fd�	dTddddddddddd�
d,eee5de8de8d e8d!e8d"e8d#e8d$eeee9dfd%ee8d&ee8d'e8d(ee
ee5gd)fd)ffd1d+��Z:ed2edd3e8d(eedddffd4d5��Z;Gd6d�d�Z<d7edd$eed%e8d8e=d(df
d9d:�Z>dUd<d=�Z?d2edd>ed(dfd?d@�Z@edfd7edd$eed8ee=d(edAfdBdC�ZAej6dDk�rqdEddFe=d(e8fdGdH�ZBndEddFe=d(e8fdIdH�ZBdVdKdL�ZCdJddMe=d3ed(dfdNdO�ZDd,eed(e8fdPdQ�ZEd7edd$eed(d;fdRdS�ZFdS)WaX
The main purpose is to enhance stdlib dataclasses by adding validation
A pydantic dataclass can be generated from scratch or from a stdlib one.

Behind the scene, a pydantic dataclass is just like a regular one on which we attach
a `BaseModel` and magic methods to trigger the validation of the data.
`__init__` and `__post_init__` are hence overridden and have extra logic to be
able to validate input data.

When a pydantic dataclass is generated from scratch, it's just a plain dataclass
with validation triggered at initialization

The tricky part if for stdlib dataclasses that are converted after into pydantic ones e.g.

```py
@dataclasses.dataclass
class M:
    x: int

ValidatedM = pydantic.dataclasses.dataclass(M)
```

We indeed still want to support equality, hashing, repr, ... as if it was the stdlib one!

```py
assert isinstance(ValidatedM(x=1), M)
assert ValidatedM(x=1) == M(x=1)
```

This means we **don't want to create a new dataclass that inherits from it**
The trick is to create a wrapper around `M` that will act as a proxy to trigger
validation without altering default `M` behaviour.
�N)�contextmanager)�wraps)�cached_property)�
TYPE_CHECKING�Any�Callable�ClassVar�Dict�	Generator�Optional�Type�TypeVar�Union�overload)�dataclass_transform)�gather_all_validators)�
BaseConfig�
ConfigDict�Extra�
get_config)�ValidationError)�DataclassTypeError)�Field�	FieldInfo�Required�	Undefined)�create_model�validate_model)�ClassAttribute)�	BaseModel)�CallableGenerator�NoArgAnyCallable�
DataclassT�	Dataclass)�bound�DataclassProxyc@s�eZdZUeeeefed<eeed<eeded<ee	ed<eeded<ee	ed<ee
eed<eedgd	fed
<ee	ed<ded
edd	fdd�Z
ede
dddfdd��Zede
ddeddfdd��Zd	S)r#�__dataclass_fields__�__dataclass_params__).N�
__post_init__�__pydantic_run_validation__�__post_init_post_parse__�__pydantic_initialised__�__pydantic_model__N�__pydantic_validate_values__�#__pydantic_has_field_info_default__�args�kwargs�returncO�dS�N���selfr/r0r4r4�B/usr/local/lib/python3.10/dist-packages/pydantic/v1/dataclasses.py�__init__P�zDataclass.__init__�clsr cCr2r3r4�r:r4r4r7�__get_validators__S�zDataclass.__get_validators__r"�vcCr2r3r4�r:r>r4r4r7�__validate__Wr=zDataclass.__validate__)�__name__�
__module__�__qualname__rr	�strr�__annotations__r�boolrr�objectr8�classmethodr<r@r4r4r4r7r#Bs
 )�	dataclass�set_validation�$create_pydantic_model_from_dataclass�is_builtin_dataclass�make_dataclass_validator�_T���
)�field_specifiersTF.�
�init�repr�eq�order�unsafe_hash�frozen�config�validate_on_init�	use_proxy�kw_onlyrTrUrVrWrXrYrZr[r\r]r1�DataclassClassOrWrapperc

Cr2r3r4rSr4r4r7rIh�rI�_clsc
Cr2r3r4)r`rTrUrVrWrXrYrZr[r\r]r4r4r7rIys�	rTrUrVrWrXrYrZr[r\c		Cr2r3r4rar4r4r7rI�sc	
Cr2r3r4)
r`rTrUrVrWrXrYrZr[r\r4r4r7rI�r_c
sHt|��dttddf����������	f
dd�}|dur |S||�S)a
    Like the python standard lib dataclasses but with type validation.
    The result is either a pydantic dataclass that will validate input data
    or a wrapper that will trigger validation around a stdlib dataclass
    to avoid modifying it directly
    r:r1r^c
s��dur�nt|�o|jdtuptt|��tt|jd��k}|r+d}t|�}d}n&|jp/d}tjdkrCt	j
|�������d�}nt	j
|������d�}d}�	durW|n�	}t|�||�|jj
di|j|i��|S)	Nr�FrO)rTrUrVrWrXrYr])rTrUrVrWrXrYTr4)rL�	__bases__rG�set�dirr%�__doc__�sys�version_info�dataclassesrI�#_add_pydantic_validation_attributesr,�__try_update_forward_refs__rA)r:�should_use_proxy�
dc_cls_doc�dc_cls�default_validate_on_init�should_validate_on_init�
rVrYrTr]rWrU�
the_configrXr\r[r4r7�wrap�s:�*�

��zdataclass.<locals>.wrapN)rrr)r`rTrUrVrWrXrYrZr[r\r]rsr4rqr7rI�s
,%r:�valueccs*�|j}z||_|VW||_dS||_wr3)r))r:rt�original_run_validationr4r4r7rJ�s�rJc@s�eZdZdZdedddfdd�Zded	edefd
d�Zdedefd
d�Z	dededdfdd�Z
dedefdd�Zddd�Z
deddfdd�ZdS)r%�
__dataclass__rnr#r1NcCst�|d|�dS)Nrv)rG�__setattr__)r6rnr4r4r7r8��zDataclassProxy.__init__r/r0cOs@t|jd��|j|i|��Wd�S1swYdS)NT)rJrvr5r4r4r7�__call__s$�zDataclassProxy.__call__�namecCst|j|�Sr3)�getattrrv)r6rzr4r4r7�__getattr__�zDataclassProxy.__getattr__�_DataclassProxy__name�_DataclassProxy__valuecCst|j||�Sr3)�setattrrv)r6r~rr4r4r7rwszDataclassProxy.__setattr__�instancecCst||j�Sr3)�
isinstancerv)r6r�r4r4r7�__instancecheck__r}z DataclassProxy.__instancecheck__cCstt�|j��Sr3)r%�copyrv�r6r4r4r7�__copy__szDataclassProxy.__copy__�memocCstt�|j|��Sr3)r%r��deepcopyrv)r6r�r4r4r7�__deepcopy__rxzDataclassProxy.__deepcopy__)r1r%)rArBrC�	__slots__rr8rryrDr|rwrFr�r�r�r4r4r4r7r%�s
rnrmc	sZ|j�t��dddtdtddf��fdd���t|d	�rPz|jj�Wnty.|j�Ynwt��dddtdtddf��fd
d��}t|d��t|d	|�nt��dddtdtddf�fd
d��}t|d|�t|dtd|��t|dd�t|dt	|�|��t|dt
�t|dtt��t|dtt
��|jjjr�|jjs�t|dt�dSdSdS)a
    We need to replace the right method. If no `__post_init__` has been set in the stdlib dataclass
    it won't even exist (code is generated on the fly by `dataclasses`)
    By default, we run validation after `__init__` or `__post_init__` if defined
    r6r#r/r0r1Ncs��jtjkr��g|�Ri�fdd�|��D���dS�jtjkrH|��D]\}}�j�||�q&��g|�Ri�fdd�|��D���dS��g|�Ri|��dS)Nc� i|]\}}|�jvr||�qSr4�r&��.0�kr>r�r4r7�
<dictcomp>%� zR_add_pydantic_validation_attributes.<locals>.handle_extra_init.<locals>.<dictcomp>cr�r4r�r�r�r4r7r�*r�)�extrar�ignore�items�allow�__dict__�
setdefault)r6r/r0r�r>)rZrTr�r7�handle_extra_init"s,,z>_add_pydantic_validation_attributes.<locals>.handle_extra_initr(csr�jdkr�|g|�Ri|��|jjr%|��t|d�r%|j|i|���jdkr7�|g|�Ri|��dSdS)N�before_validationr*�after_validation)�post_init_call�	__class__r)r-�hasattrr*r5)rZ�	post_initr4r7�
new_post_init5s


�z:_add_pydantic_validation_attributes.<locals>.new_post_initr8c	s��|g|�Ri|��|jjr|��t|d�rUi}t|jj���D](\}}|jtj	urJz	||||j
<Wq"tyI|�|j
|j
�||j
<Yq"wq"|jdi|��dSdS)Nr*r4)r�r)r-r��	enumerater&�values�_field_typeri�_FIELD_INITVARrz�
IndexError�get�defaultr*)r6r/r0�initvars_and_values�i�f)r�r4r7�new_initGs
���z5_add_pydantic_validation_attributes.<locals>.new_initr)r+Fr,r-r@r<rw)r8rrr�r(�__wrapped__�AttributeErrorr�rrK�_dataclass_validate_valuesrH�_validate_dataclass�_get_validatorsr,�
__config__�validate_assignmentr'rY�&_dataclass_validate_assignment_setattr)rnrZr[rmr�r�r4)rZr�rTr�r7rjs2"

�" �rjr ccs�|jVdSr3)r@r;r4r4r7r�ks�r�r>cCs�t|d��>t||�r|��|Wd�St|ttf�r*||�Wd�St|t�r=|di|��Wd�St|jd��1sFwYdS)NT)�
class_namer4)rJr�r-�list�tuple�dictrrAr?r4r4r7r�os
��
�	�r�rc
Cs�i}t�|�D]:}t}d}|jtjur|j}n|jtjur!|j}nt}t|t�r.|}d|_	nt
d||d�|j��}|j|f||j
<qt|�}t|jf||j|ddid�|��}	|dur`||	_|	S|jpdd|	_|	S)NT)r��default_factory�__resolve_forward_refs__F)r�rB�__validators__�__cls_kwargs__rbr4)ri�fieldsrr��MISSINGr�rr�rr.r�metadata�typerzrrrArBrf)
rnrZrm�field_definitions�fieldr�r��
field_info�
validators�modelr4r4r7rK|s:
����rK)rP��objr�cCsttt|�|d�t�Sr3)r�r{r�r�r�r�r4r4r7�_is_field_cached_property�sr�cCsdS)NFr4r�r4r4r7r��r9r6cs�t�d�rdSt�dd�r�fdd��j��D�}n�fdd��j��D�}t�j|�jd�\}}}|r6|��j�|�t��dd�dS)	Nr+r.Fcs*i|]\}}t|t�st�|�s||�qSr4)r�rr�r�r�r4r7r��s���z._dataclass_validate_values.<locals>.<dictcomp>cs i|]\}}t�|�s||�qSr4)r�r�r�r4r7r��r�r;T)	r{r�r�rr,r��updaterGrw)r6�
input_data�d�_�validation_errorr4r�r7r��s

�r�rzcCsl|jr-t|j�}|�|d�|jj�|d�}|r-|j||||jd�\}}|r-t	|g|j��t
�|||�dS)N)�locr:)r+r�r��popr,�
__fields__r��validater�rrGrw)r6rzrtr��known_field�error_r4r4r7r��s
r�cCs2t�|�ot|d�ot|j��tt|di���S)a�
    Whether a class is a stdlib dataclass
    (useful to discriminated a pydantic dataclass that is actually a wrapper around a stdlib dataclass)

    we check that
    - `_cls` is a dataclass
    - `_cls` is not a processed pydantic dataclass (with a basemodel attached)
    - `_cls` is not a pydantic dataclass inheriting directly from a stdlib dataclass
    e.g.
    ```
    @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')
    r,rE)ri�is_dataclassr�rdr&�
issupersetr{)r`r4r4r7rL�s


��rLccs�tt||dd��EdHdS)z�
    Create a pydantic.dataclass from a builtin dataclass to add type validation
    and yield the validators
    It retrieves the parameters of the dataclass and forwards them to the newly created dataclass
    T)rZr\N)r�rI)rnrZr4r4r7rM�s�rMr3)r:r^r1r )r6r#r1N)Grfr�rirg�
contextlibr�	functoolsrr�ImportError�typingrrrrr	r
rrr
rr�typing_extensionsr�pydantic.v1.class_validatorsr�pydantic.v1.configrrrr�pydantic.v1.error_wrappersr�pydantic.v1.errorsr�pydantic.v1.fieldsrrrr�pydantic.v1.mainrr�pydantic.v1.utilsrr�pydantic.v1.typingr r!r"r^r#�__all__rNrhr�rFrGrIrJr%rDrjr�r�rKr�r�r�rLrMr4r4r4r7�<module>s�!�4��������	�
�����������	�
���
���������	�
����������	�
�����
�������	�
���
�A*	����
�
V����
�'

"