o
    í6WjW>  ã                   @  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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#r·e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< dgdd „ZNdhd#d$„ZOdid(d)„ZPdjd+d,„ZQdkd.d/„ZRdld2d3„ZSe&d4ƒZTdmd8d9„ZUdnd=d>„ZVe&d?ƒZWe7d@œdodFdG„ZXG dHdI„ dIe2jYƒZZe#�r)dpdMdN„Z[nG dOdN„ dNƒZ[e&dPƒZ\dqdRdS„Z]e^ƒ Z_drdWdX„Z`dsd[d\„Zaejbd]d^�G d_d`„ d`ƒƒZce&dadbdc�Zde&ddƒZeG dedf„ dfe%edeef ƒZfdS )tz�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_COLLECTIONSÚparamr   ÚreturnÚboolc                 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/   úh/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/pydantic/_internal/_utils.pyÚcan_be_positionalJ   s   r1   Úvr   c                 C  s   t | ttttttfƒS ©N)Ú
isinstanceÚlistÚtupleÚsetÚ	frozensetr   r   ©r2   r/   r/   r0   Úsequence_like]   s   r:   ÚoÚclass_or_tupleú(type[Any] | tuple[type[Any], ...] | Nonec                 C  s"   zt | |ƒW S  ty   Y dS w ©NF)r4   Ú	TypeError)r;   r<   r/   r/   r0   Úlenient_isinstancea   s
   ÿr@   Úclsc                 C  s:   zt | tƒo
t| |ƒW S  ty   t | tjƒrY dS ‚ w r>   )r4   ÚtypeÚ
issubclassr?   r!   ÚWithArgsTypes)rA   r<   r/   r/   r0   Úlenient_issubclassh   s   ýrE   úTypeGuard[type[BaseModel]]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"   rE   )rA   r&   r/   r/   r0   Úis_model_classq   s   rG   Ú
identifierÚstrc                 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)rH   r/   r/   r0   Úis_valid_identifierz   s   rM   ÚKeyTypeÚmappingúdict[KeyType, Any]Úupdating_mappingsc                 G  sd   |   ¡ }|D ])}| ¡ D ]"\}}||v r*t|| tƒr*t|tƒr*t|| |ƒ||< q|||< qq|S r3   )ÚcopyÚitemsr4   ÚdictÚdeep_update)rO   rQ   Úupdated_mappingÚupdating_mappingÚkr2   r/   r/   r0   rU   …   s    
ürU   údict[Any, Any]ÚupdateÚNonec                 K  s   |   dd„ | ¡ D ƒ¡ d S )Nc                 S  s   i | ]\}}|d ur||“qS r3   r/   )Ú.0rX   r2   r/   r/   r0   Ú
<dictcomp>‘   s    z#update_not_none.<locals>.<dictcomp>)rZ   rS   )rO   rZ   r/   r/   r0   Úupdate_not_none�   s   r^   ÚT)Úname_factoryÚ
input_listúlist[T] | tuple[T, ...]r`   úCallable[[T], str]úlist[T]c                C  sJ   g }g }| D ]}||ƒ}||vr|  |¡ |  |¡ 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)ra   r`   ÚresultÚresult_namesr2   Úv_namer/   r/   r0   Úunique_list—   s   	
rj   c                   @  sŠ   e Zd ZdZdZd-d	d
„Zd.dd„Zd.dd„Zd/dd„Zd0dd„Z	e
d1d2d d!„ƒZed3d"d#„ƒZe
d4d$d%„ƒZed5d'd(„ƒZd6d*d+„Zd,S )7Ú
ValueItemszOClass for more convenient calculation of excluded or included fields on values.)Ú_itemsÚ_typeÚvaluer   rS   ú$AbstractSetIntStr | MappingIntStrAnyr*   r[   c                 C  s2   |   |¡}t|ttfƒr|  |t|ƒ¡}|| _d S r3   )Ú_coerce_itemsr4   r5   r6   Ú_normalize_indexesÚlenrl   )Úselfrn   rS   r/   r/   r0   Ú__init__²   s   

zValueItems.__init__Úitemr+   c                 C  s   |   | j |¡¡S )zWCheck if item is fully excluded.

        :param item: key or index of a value
        )Úis_truerl   Úget©rs   ru   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
        ©rl   rx   r/   r/   r0   Úis_includedÁ   s   
zValueItems.is_includedÚeú	int | strú+AbstractSetIntStr | MappingIntStrAny | Nonec                 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)rl   rw   rv   )rs   r|   ru   r/   r/   r0   Úfor_elementÈ   s   zValueItems.for_elementr#   Úv_lengthÚintúdict[int | str, Any]c           	      C  s  i }d}|  ¡ D ]F\}}t|tƒs&t|tƒs&|  |¡s&td|› d|j› �ƒ‚|dkr0|  |¡}qt|tƒs9tdƒ‚|dk rA|| n|}|  	|| 
|¡¡||< q|sS|S |  |¡rgt|ƒD ]}| |d¡ q\|S t|ƒD ]}| |i ¡}|  |¡s€|  	||¡||< qk|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   .)rS   r4   r   ÚAbstractSetrv   r?   Ú	__class__Ú_coerce_valuer�   Úmergerw   ÚrangeÚ
setdefault)	rs   rS   r€   Únormalized_itemsÚ	all_itemsÚir2   Únormalized_iÚnormalized_itemr/   r/   r0   rq   Ï   s4   	

ÿ

€zValueItems._normalize_indexesFÚbaseÚoverrideÚ	intersectc                   sÎ   |   ˆ¡‰|   ˆ ¡‰ ˆdu rˆ S |  ˆ ¡sˆ du rˆS |  ˆ¡r&|r$ˆ S ˆS |r;‡fdd„ˆ D ƒ‡ fdd„ˆD ƒ }ntˆ ƒ‡ fdd„ˆD ƒ }i }|D ]}| jˆ  |¡ˆ |¡|d�}|durd|||< qL|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                   ó   g | ]}|ˆ v r|‘qS r/   r/   ©r\   rX   )r�   r/   r0   Ú
<listcomp>  ó    z$ValueItems.merge.<locals>.<listcomp>c                   r’   r/   r/   r“   ©r�   r/   r0   r”     r•   c                   s   g | ]}|ˆ vr|‘qS r/   r/   r“   r–   r/   r0   r”     r•   )r‘   )r†   rv   r5   r‡   rw   )rA   r�   r�   r‘   Ú
merge_keysÚmergedrX   Úmerged_itemr/   )r�   r�   r0   r‡   ô   s$   


&€zValueItems.mergec                 C  sD   t | tƒr	 | S t | tƒrt | d¡} | S t| ddƒ}td|› �ƒ‚)N.r…   z???z!Unexpected type of exclude value )r4   r   r„   rT   ÚfromkeysÚgetattrr?   )rS   Ú
class_namer/   r/   r0   rp     s   

ûþzValueItems._coerce_itemsc                 C  s    |d u s	|   |¡r|S |  |¡S r3   )rv   rp   )rA   rn   r/   r/   r0   r†   %  s   
zValueItems._coerce_valuer2   c                 C  s   | du p| du S )NT.r/   r9   r/   r/   r0   rv   +  ó   zValueItems.is_trueú_repr.ReprArgsc                 C  s   d | j fgS r3   rz   ©rs   r/   r/   r0   Ú__repr_args__/  ó   zValueItems.__repr_args__N)rn   r   rS   ro   r*   r[   )ru   r   r*   r+   )r|   r}   r*   r~   )rS   r#   r€   r�   r*   r‚   )F)r�   r   r�   r   r‘   r+   r*   r   )rS   ro   r*   r#   )rn   r   r*   r   ©r2   r   r*   r+   )r*   rž   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__rt   ry   r{   r   rq   Úclassmethodr‡   Ústaticmethodrp   r†   rv   r    r/   r/   r/   r0   rk   ­   s"    




%%
rk   ÚnameÚ	get_valueúCallable[[], T]c                 C  ó   d S r3   r/   ©rª   r«   r/   r/   r0   ÚLazyClassAttribute5  s    r¯   c                   @  s2   e Zd ZdZddd	„Zeddd„ƒZ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.
        rª   rI   r«   úCallable[[], Any]r*   r[   c                 C  s   || _ || _d S r3   r®   )rs   rª   r«   r/   r/   r0   rt   ?  s   
zLazyClassAttribute.__init__r   c                 C  s   |   ¡ S r3   )r«   rŸ   r/   r/   r0   rn   C  s   zLazyClassAttribute.valueÚinstanceÚownerú	type[Any]c                 C  s&   |d u r| j S t| j›d|j›d�ƒ‚)Nz attribute of z is class-only)rn   ÚAttributeErrorrª   r£   )rs   r±   r²   r/   r/   r0   Ú__get__G  s   zLazyClassAttribute.__get__N)rª   rI   r«   r°   r*   r[   )r*   r   )r±   r   r²   r³   r*   r[   )r£   r¤   r¥   r¦   rt   r   rn   rµ   r/   r/   r/   r0   r¯   9  s    
ÚObjÚobjc              
   C  s~   | t u s| tu r
| S | j}|tv r| S z| s&|tv r+|tu r!| W S |  ¡ W S W t
| ƒS W t
| ƒS  ttt	fy>   Y t
| ƒS w )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   r…   r'   r(   r6   rR   r?   Ú
ValueErrorÚRuntimeErrorr
   )r·   Úobj_typer/   r/   r0   Úsmart_deepcopyP  s    þùüür»   ÚleftúIterable[Any]Úrightc                 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¾   Ú	left_itemÚ
right_itemr/   r/   r0   Úall_identicalh  s
   	ÿrÃ   ÚaÚbc                 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  r�   rÆ   T)Úfrozenc                   @  s:   e Zd ZU dZdZded< dd	d
„Zerddd„ZdS 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É   ÚkeyrI   r*   r   c                C  s   | j  |t¡S r3   )rÉ   rw   rÀ   ©rs   rÊ   r/   r/   r0   Ú__getitem__‰  s   zSafeGetItemProxy.__getitem__r+   c                C  s   | j  |¡S r3   )rÉ   Ú__contains__rË   r/   r/   r0   rÍ   ’  r¡   zSafeGetItemProxy.__contains__N)rÊ   rI   r*   r   )rÊ   rI   r*   r+   )	r£   r¤   r¥   r¦   r§   Ú__annotations__rÌ   r   rÍ   r/   r/   r/   r0   rÈ   |  s   
 
þrÈ   Ú_ModelTr&   )ÚboundÚ_RTc                   @  sL   e Zd ZdZddd„Zeddd„ƒZeeddd�ddd„ƒƒZ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.
    ÚfgetúCallable[[type[_ModelT]], _RT]r*   r[   c                C  s
   || _ d S r3   )rÓ   )rs   rÓ   r/   r/   r0   rt   ¢  s   
z%deprecated_instance_property.__init__r±   Úobjtypeútype[_ModelT]rÑ   c                 C  r­   r3   r/   ©rs   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  r­   r3   r/   r×   r/   r/   r0   rµ   ¨  s   ú_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µ   )rs   r±   rÕ   Ú	attr_namer/   r/   r0   rµ   ¯  s   
ÿý
ü)rÓ   rÔ   r*   r[   )r±   r[   rÕ   rÖ   r*   rÑ   )r±   rÏ   rÕ   rÖ   r*   rÑ   )r±   rÙ   rÕ   rÖ   r*   rÑ   )r£   r¤   r¥   r¦   rt   r   rµ   r   r/   r/   r/   r0   rÒ   š  s    
ýrÒ   )r)   r   r*   r+   r¢   )r;   r   r<   r=   r*   r+   )rA   r   r<   r   r*   r+   )rA   r   r*   rF   )rH   rI   r*   r+   )rO   rP   rQ   rP   r*   rP   )rO   rY   rZ   r   r*   r[   )ra   rb   r`   rc   r*   rd   )rª   rI   r«   r¬   r*   r_   )r·   r¶   r*   r¶   )r¼   r½   r¾   r½   r*   r+   )rÄ   r   rÅ   r   r*   r   )gr¦   Ú
__future__r   Ú_annotationsÚdataclassesrK   rÝ   rà   ÚweakrefÚcollectionsr   r   r   Úcollections.abcr   r   r   r	   r„   rR   r
   Ú	functoolsr   Úinspectr   Ú	itertoolsr   Útypesr   r   r   r   r   r   Útypingr   r   r   r   r   Úpydantic_corer   r   Útyping_extensionsr   r   r   Úpydanticr   Ú r    r!   Ú_import_utilsr"   r�   rI   r#   rÎ   r$   Úmainr&   ÚfloatÚcomplexr+   ÚbytesrB   ÚNoneTypeÚrefÚNotImplementedr…   ÚEllipsisr'   r5   r7   r6   r8   rT   r(   r1   r:   r@   rE   rG   rM   rN   rU   r^   r_   rj   ÚRepresentationrk   r¯   r¶   r»   ÚobjectrÀ   rÃ   rÆ   Ú	dataclassrÈ   rÏ   rÑ   rÒ   r/   r/   r/   r0   Ú<module>   sœ      íø




	
	

ý 



