a
    “˜ëjW>  ã                   @  s6  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Zddl	m
Z
mZmZ ddlmZ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mZmZmZm Z m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 e#�rpee6e$f ee7e$f B Z8de9d< ee6 ee7 B Z:de9d< ddl;m<Z< e6e=e>e7e?e@eAe3jBeee ejCee!eDjEeFjEhZGde9d< eHeIeJeKeLe
eehZMde9d< dddœdd„ZNd dd!œd"d#„ZOd d$dd%œd&d'„ZPd d dd(œd)d*„ZQd d+d,œd-d.„ZRd/dd0œd1d2„ZSe&d3ƒZTd4d4d4d5œd6d7„ZUd8d d9d:œd;d<„ZVe&d=ƒZWe7d>œd?d@dAdBœdCdD„ZXG dEdF„ dFe2jYƒZZe#�rœd/dGd=dHœdIdJ„Z[nG dKdJ„ dJƒZ[e&dLƒZ\dLdLdMœdNdO„Z]e^ƒ Z_dPdPddQœdRdS„Z`d d d dTœdUdV„ZaejbdWdX�G dYdZ„ dZƒƒZce&d[d\d]�Zde&d^ƒZeG d_d`„ d`e%edeef ƒZfdS )az�Bucket of reusable internal utilities.

This should be reduced as much as possible with functions only used in one place, moved to that place.
é    )ÚannotationsN)ÚOrderedDictÚdefaultdictÚdeque)ÚCallableÚIterableÚMapping)ÚSet)Údeepcopy)Úcached_property)Ú	Parameter)Úzip_longest)ÚBuiltinFunctionTypeÚCodeTypeÚFunctionTypeÚGeneratorTypeÚ
LambdaTypeÚ
ModuleType)ÚTYPE_CHECKINGÚAnyÚGenericÚTypeVarÚoverload)ÚMISSINGÚPydanticUndefined)Ú	TypeAliasÚ	TypeGuardÚ
deprecated)ÚPydanticDeprecatedSince211é   )Ú_reprÚ_typing_extra)Úimport_cached_base_modelr   ÚMappingIntStrAnyÚAbstractSetIntStré   )Ú	BaseModelzset[type[Any]]ÚIMMUTABLE_NON_COLLECTIONS_TYPESÚBUILTIN_COLLECTIONSr   Úbool)ÚparamÚreturnc                 C  s   | j tjtjfv S )aZ  Return whether the parameter accepts a positional argument.

    ```python {test="skip" lint="skip"}
    def func(a, /, b, *, c):
        pass

    params = inspect.signature(func).parameters
    can_be_positional(params['a'])
    #> True
    can_be_positional(params['b'])
    #> True
    can_be_positional(params['c'])
    #> False
    ```
    )Úkindr   ÚPOSITIONAL_ONLYÚPOSITIONAL_OR_KEYWORD)r*   © r/   úX/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pydantic/_internal/_utils.pyÚcan_be_positionalJ   s    r1   r   ©Úvr+   c                 C  s   t | ttttttfƒS ©N)Ú
isinstanceÚlistÚtupleÚsetÚ	frozensetr   r   ©r3   r/   r/   r0   Úsequence_like]   s    r;   z(type[Any] | tuple[type[Any], ...] | None)ÚoÚclass_or_tupler+   c                 C  s&   zt | |ƒW S  ty    Y dS 0 d S ©NF)r5   Ú	TypeError)r<   r=   r/   r/   r0   Úlenient_isinstancea   s    r@   )Úclsr=   r+   c                 C  sB   zt | tƒot| |ƒW S  ty<   t | tjƒr6Y dS ‚ Y n0 d S r>   )r5   ÚtypeÚ
issubclassr?   r!   ZWithArgsTypes)rA   r=   r/   r/   r0   Úlenient_issubclassh   s    rD   zTypeGuard[type[BaseModel]])rA   r+   c                 C  s   t ƒ }t| |ƒo| |uS )zŒReturns true if cls is a _proper_ subclass of BaseModel, and provides proper type-checking,
    unlike raw calls to lenient_issubclass.
    )r"   rD   )rA   r&   r/   r/   r0   Úis_model_classq   s    rE   Ústr)Ú
identifierr+   c                 C  s   |   ¡ ot | ¡ S )z¦Checks that a string is a valid identifier and not a Python keyword.
    :param identifier: The identifier to test.
    :return: True if the identifier is valid.
    )ÚisidentifierÚkeywordÚ	iskeyword)rG   r/   r/   r0   Úis_valid_identifierz   s    rK   ÚKeyTypezdict[KeyType, Any])ÚmappingÚupdating_mappingsr+   c                 G  sd   |   ¡ }|D ]R}| ¡ D ]D\}}||v rTt|| tƒrTt|tƒrTt|| |ƒ||< q|||< qq|S r4   )ÚcopyÚitemsr5   ÚdictÚdeep_update)rM   rN   Zupdated_mappingZupdating_mappingÚkr3   r/   r/   r0   rR   …   s     rR   zdict[Any, Any]ÚNone)rM   Úupdater+   c                 K  s   |   dd„ | ¡ D ƒ¡ d S )Nc                 S  s   i | ]\}}|d ur||“qS r4   r/   )Ú.0rS   r3   r/   r/   r0   Ú
<dictcomp>‘   ó    z#update_not_none.<locals>.<dictcomp>)rU   rP   )rM   rU   r/   r/   r0   Úupdate_not_none�   s    rY   ÚT)Úname_factoryzlist[T] | tuple[T, ...]zCallable[[T], str]zlist[T])Ú
input_listr[   r+   c                C  sJ   g }g }| D ]8}||ƒ}||vr6|  |¡ |  |¡ q||| |¡< q|S )z£Make a list unique while maintaining order.
    We update the list if another one with the same name is set
    (e.g. model validator overridden in subclass).
    )ÚappendÚindex)r\   r[   ÚresultZresult_namesr3   Zv_namer/   r/   r0   Úunique_list—   s    	
r`   c                   @  sÌ   e Zd ZdZdZddddœdd„Zdd	d
œdd„Zdd	d
œdd„Zdddœdd„Zddddœdd„Z	e
d,ddd	ddœdd„ƒZedddœdd „ƒZe
ddd!œd"d#„ƒZedd	d$œd%d&„ƒZd'd(œd)d*„Zd+S )-Ú
ValueItemszOClass for more convenient calculation of excluded or included fields on values.)Ú_itemsÚ_typer   z$AbstractSetIntStr | MappingIntStrAnyrT   )ÚvaluerP   r+   c                 C  s2   |   |¡}t|ttfƒr(|  |t|ƒ¡}|| _d S r4   )Ú_coerce_itemsr5   r6   r7   Ú_normalize_indexesÚlenrb   )Úselfrd   rP   r/   r/   r0   Ú__init__²   s    
zValueItems.__init__r)   )Úitemr+   c                 C  s   |   | j |¡¡S )zWCheck if item is fully excluded.

        :param item: key or index of a value
        )Úis_truerb   Úget©rh   rj   r/   r/   r0   Úis_excludedº   s    zValueItems.is_excludedc                 C  s
   || j v S )z`Check if value is contained in self._items.

        :param item: key or index of value
        ©rb   rm   r/   r/   r0   Úis_includedÁ   s    zValueItems.is_includedz	int | strz+AbstractSetIntStr | MappingIntStrAny | None)Úer+   c                 C  s   | j  |¡}|  |¡s|S dS )z�:param e: key or index of element on value
        :return: raw values for element if self._items is dict and contain needed element
        N)rb   rl   rk   )rh   rq   rj   r/   r/   r0   Úfor_elementÈ   s    zValueItems.for_elementr#   Úintzdict[int | str, Any])rP   Úv_lengthr+   c           	      C  s  i }d}|  ¡ D ]Œ\}}t|tƒsLt|tƒsL|  |¡sLtd|› d|j› �ƒ‚|dkr`|  |¡}qt|tƒsrtdƒ‚|dk r‚|| n|}|  	|| 
|¡¡||< q|s¦|S |  |¡rÎt|ƒD ]}| |d¡ q¸|S t|ƒD ]*}| |i ¡}|  |¡sÖ|  	||¡||< qÖ|S )a]  :param items: dict or set of indexes which will be normalized
        :param v_length: length of sequence indexes of which will be

        >>> self._normalize_indexes({0: True, -2: True, -1: True}, 4)
        {0: True, 2: True, 3: True}
        >>> self._normalize_indexes({'__all__': True}, 4)
        {0: True, 1: True, 2: True, 3: True}
        Nz,Unexpected type of exclude value for index "z" Ú__all__z€Excluding fields from a sequence of sub-models or dicts must be performed index-wise: expected integer keys or keyword "__all__"r   .)rP   r5   r   ÚAbstractSetrk   r?   Ú	__class__Ú_coerce_valuers   Úmergerl   ÚrangeÚ
setdefault)	rh   rP   rt   Znormalized_itemsZ	all_itemsÚir3   Znormalized_iÚnormalized_itemr/   r/   r0   rf   Ï   s2    	

ÿ

zValueItems._normalize_indexesF)ÚbaseÚoverrideÚ	intersectr+   c                   sÎ   |   ˆ¡‰|   ˆ ¡‰ ˆdu r ˆ S |  ˆ ¡s2ˆ du r6ˆS |  ˆ¡rL|rHˆ S ˆS |rv‡fdd„ˆ D ƒ‡ fdd„ˆD ƒ }ntˆ ƒ‡ fdd„ˆD ƒ }i }|D ]0}| jˆ  |¡ˆ |¡|d�}|dur˜|||< q˜|S )aH  Merge a `base` item with an `override` item.

        Both `base` and `override` are converted to dictionaries if possible.
        Sets are converted to dictionaries with the sets entries as keys and
        Ellipsis as values.

        Each key-value pair existing in `base` is merged with `override`,
        while the rest of the key-value pairs are updated recursively with this function.

        Merging takes place based on the "union" of keys if `intersect` is
        set to `False` (default) and on the intersection of keys if
        `intersect` is set to `True`.
        Nc                   s   g | ]}|ˆ v r|‘qS r/   r/   ©rV   rS   )r   r/   r0   Ú
<listcomp>  rX   z$ValueItems.merge.<locals>.<listcomp>c                   s   g | ]}|ˆ v r|‘qS r/   r/   r�   ©r~   r/   r0   r‚     rX   c                   s   g | ]}|ˆ vr|‘qS r/   r/   r�   rƒ   r/   r0   r‚     rX   )r€   )rx   rk   r6   ry   rl   )rA   r~   r   r€   Z
merge_keysÚmergedrS   Zmerged_itemr/   )r~   r   r0   ry   ô   s"    


&
zValueItems.merge)rP   r+   c                 C  sB   t | tƒrn2t | tƒr$t | d¡} nt| ddƒ}td|› �ƒ‚| S )N.rw   z???z!Unexpected type of exclude value )r5   r   rv   rQ   ÚfromkeysÚgetattrr?   )rP   Ú
class_namer/   r/   r0   re     s    

zValueItems._coerce_items)rd   r+   c                 C  s    |d u s|   |¡r|S |  |¡S r4   )rk   re   )rA   rd   r/   r/   r0   rx   %  s    zValueItems._coerce_valuer2   c                 C  s   | du p| du S )NT.r/   r:   r/   r/   r0   rk   +  s    zValueItems.is_truez_repr.ReprArgs©r+   c                 C  s   d | j fgS r4   ro   ©rh   r/   r/   r0   Ú__repr_args__/  s    zValueItems.__repr_args__N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__ri   rn   rp   rr   rf   Úclassmethodry   Ústaticmethodre   rx   rk   rŠ   r/   r/   r/   r0   ra   ­   s    %%
ra   zCallable[[], T]©ÚnameÚ	get_valuer+   c                 C  s   d S r4   r/   ©r“   r”   r/   r/   r0   ÚLazyClassAttribute5  rX   r–   c                   @  sF   e Zd ZdZddddœdd„Zedd	œd
d„ƒZddddœdd„ZdS )r–   z­A descriptor exposing an attribute only accessible on a class (hidden from instances).

        The attribute is lazily computed and cached during the first access.
        rF   zCallable[[], Any]rT   r’   c                 C  s   || _ || _d S r4   r•   )rh   r“   r”   r/   r/   r0   ri   ?  s    zLazyClassAttribute.__init__r   rˆ   c                 C  s   |   ¡ S r4   )r”   r‰   r/   r/   r0   rd   C  s    zLazyClassAttribute.valuez	type[Any])ÚinstanceÚownerr+   c                 C  s*   |d u r| j S t| j›d|j›d�ƒ‚d S )Nz attribute of z is class-only)rd   ÚAttributeErrorr“   r‹   )rh   r—   r˜   r/   r/   r0   Ú__get__G  s    zLazyClassAttribute.__get__N)r‹   rŒ   r�   rŽ   ri   r   rd   rš   r/   r/   r/   r0   r–   9  s
   ÚObj)Úobjr+   c              
   C  sn   | t u s| tu r| S | j}|tv r&| S z&| sJ|tv rJ|tu r@| n|  ¡ W S W n ttt	fyd   Y n0 t
| ƒS )z¬Return type as is for immutable built-in types
    Use obj.copy() for built-in empty collections
    Use copy.deepcopy() for non-empty collections and unknown objects.
    )r   r   rw   r'   r(   r7   rO   r?   Ú
ValueErrorÚRuntimeErrorr
   )rœ   Zobj_typer/   r/   r0   Úsmart_deepcopyP  s    rŸ   zIterable[Any])ÚleftÚrightr+   c                 C  s*   t | |td�D ]\}}||ur dS qdS )a  Check that the items of `left` are the same objects as those in `right`.

    >>> a, b = object(), object()
    >>> all_identical([a, b, a], [a, b, a])
    True
    >>> all_identical([a, b, [a]], [a, b, [a]])  # new list object, while "equal" is not "identical"
    False
    )Ú	fillvalueFT)r   Ú	_SENTINEL)r    r¡   Z	left_itemZ
right_itemr/   r/   r0   Úall_identicalh  s    	r¤   )ÚaÚbr+   c                 C  s   | dur| S |S )zTReturn the first argument if it is not `None`, otherwise return the second argument.Nr/   )r¥   r¦   r/   r/   r0   Úget_first_not_nonew  s    r§   T)Úfrozenc                   @  sB   e Zd ZU dZdZded< dddœdd	„Zer>dd
dœdd„ZdS )ÚSafeGetItemProxyz¦Wrapper redirecting `__getitem__` to `get` with a sentinel value as default

    This makes is safe to use in `operator.itemgetter` when some keys may be missing
    )ÚwrappedzMapping[str, Any]rª   rF   r   )Úkeyr+   c                C  s   | j  |t¡S r4   )rª   rl   r£   ©rh   r«   r/   r/   r0   Ú__getitem__‰  s    zSafeGetItemProxy.__getitem__r)   c                C  s   | j  |¡S r4   )rª   Ú__contains__r¬   r/   r/   r0   r®   ’  s    zSafeGetItemProxy.__contains__N)	r‹   rŒ   r�   rŽ   r�   Ú__annotations__r­   r   r®   r/   r/   r/   r0   r©   |  s   
r©   Ú_ModelTr&   )ÚboundÚ_RTc                   @  sj   e Zd ZdZdddœdd„Zedddd	œd
d„ƒZeeddd�dddd	œdd„ƒƒZdddd	œdd„ZdS )Údeprecated_instance_propertya›  A decorator exposing the decorated class method as a property, with a warning on instance access.

    This decorator takes a class method defined on the `BaseModel` class and transforms it into
    an attribute. The attribute can be accessed on both the class and instances of the class. If accessed
    via an instance, a deprecation warning is emitted stating that instance access will be removed in V3.
    zCallable[[type[_ModelT]], _RT]rT   )Úfgetr+   c                C  s
   || _ d S r4   )r´   )rh   r´   r/   r/   r0   ri   ¢  s    z%deprecated_instance_property.__init__ztype[_ModelT]r²   )r—   Úobjtyper+   c                 C  s   d S r4   r/   ©rh   r—   rµ   r/   r/   r0   rš   ¦  s    z$deprecated_instance_property.__get__z›Accessing this attribute on the instance is deprecated, and will be removed in Pydantic V3. Instead, you should access this attribute from the model class.N)Úcategoryr°   c                 C  s   d S r4   r/   r¶   r/   r/   r0   rš   ¨  s    z_ModelT | Nonec                 C  sL   |d ur<t jdkr| jjn| jjj}tjd|›d�tdd� | j ||¡ƒ S )N)é   é
   zAccessing the zi attribute on the instance is deprecated. Instead, you should access this attribute from the model class.r%   )r·   Ú
stacklevel)	ÚsysÚversion_infor´   r‹   Ú__func__ÚwarningsÚwarnr   rš   )rh   r—   rµ   Ú	attr_namer/   r/   r0   rš   ¯  s    ÿ
ý
ü)r‹   rŒ   r�   rŽ   ri   r   rš   r   r/   r/   r/   r0   r³   š  s   ýr³   )grŽ   Ú
__future__r   Z_annotationsZdataclassesrI   r»   r¾   ÚweakrefÚcollectionsr   r   r   Úcollections.abcr   r   r   r	   rv   rO   r
   Ú	functoolsr   Úinspectr   Ú	itertoolsr   Útypesr   r   r   r   r   r   Útypingr   r   r   r   r   Zpydantic_corer   r   Ztyping_extensionsr   r   r   Zpydanticr   Ú r    r!   Z_import_utilsr"   rs   rF   r#   r¯   r$   Úmainr&   ÚfloatÚcomplexr)   ÚbytesrB   ZNoneTypeÚrefÚNotImplementedrw   ÚEllipsisr'   r6   r8   r7   r9   rQ   r(   r1   r;   r@   rD   rE   rK   rL   rR   rY   rZ   r`   ZRepresentationra   r–   r›   rŸ   Úobjectr£   r¤   r§   Z	dataclassr©   r°   r²   r³   r/   r/   r/   r0   Ú<module>   sš     íø		ý 
