a
    _g4                     @  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	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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#m$Z$ ddl%m&Z& ej'rdZ(de)d< dZ*de)d< ddl+m,Z, e-e.e/e0e1e2e3e$j4eeeej5eee6j7e8j7hZ9de)d< e:e;e<e=e>e	e
ehZ?de)d< dddddZ@dddd d!ZAdd"dd#d$d%ZBdddd&d'd(ZCdd)d*d+d,ZDd-dd.d/d0ZEed1ZFd2d2d2d3d4d5ZGd6dd7d8d9d:ZHed;ZIe0d<d=d>d?d@dAdBZJG dCdD dDe#jKZLej'r4d-dEd;dFdGdHZMnG dIdH dHZMedJZNdJdJdKdLdMZOeP ZQdNdNddOdPdQZRejSdRdSG dTdU dUZTdS )VzBucket 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)deepcopy)cached_property)	Parameter)zip_longest)BuiltinFunctionTypeCodeTypeFunctionTypeGeneratorType
LambdaType
ModuleType)AnyCallableMappingTypeVar)	TypeAlias	TypeGuard   )_repr_typing_extra)import_cached_base_modelz3typing.Mapping[int, Any] | typing.Mapping[str, Any]r   MappingIntStrAnyz1typing.AbstractSet[int] | typing.AbstractSet[str]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&   g/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/pydantic/_internal/_utils.pycan_be_positionalC   s    r(   r   vr"   c                 C  s   t | ttttttfS N)
isinstancelisttupleset	frozensetr   r   r*   r&   r&   r'   sequence_likeV   s    r2   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)r,   	TypeError)r3   r4   r&   r&   r'   lenient_isinstanceZ   s    r7   )clsr4   r"   c                 C  sB   zt | tot| |W S  ty<   t | tjr6Y dS  Y n0 d S r5   )r,   type
issubclassr6   r   ZWithArgsTypes)r8   r4   r&   r&   r'   lenient_issubclassa   s    r;   zTypeGuard[type[BaseModel]])r8   r"   c                 C  s   t  }t| |o| |uS )zReturns true if cls is a _proper_ subclass of BaseModel, and provides proper type-checking,
    unlike raw calls to lenient_issubclass.
    )r   r;   )r8   r   r&   r&   r'   is_model_classj   s    r<   str)
identifierr"   c                 C  s   |   ot|  S )zChecks 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)r>   r&   r&   r'   is_valid_identifiers   s    rB   KeyTypezdict[KeyType, Any])mappingupdating_mappingsr"   c                 G  sd   |   }|D ]R}| D ]D\}}||v rTt|| trTt|trTt|| |||< q|||< qq|S r+   )copyitemsr,   dictdeep_update)rD   rE   Zupdated_mappingZupdating_mappingkr*   r&   r&   r'   rI   ~   s     rI   zdict[Any, Any]None)rD   updater"   c                 K  s   |  dd | D  d S )Nc                 S  s   i | ]\}}|d ur||qS r+   r&   ).0rJ   r*   r&   r&   r'   
<dictcomp>       z#update_not_none.<locals>.<dictcomp>)rL   rG   )rD   rL   r&   r&   r'   update_not_none   s    rP   T)name_factoryzlist[T] | tuple[T, ...]ztyping.Callable[[T], str]zlist[T])
input_listrR   r"   c                C  sJ   g }g }| D ]8}||}||vr6| | | | q||||< q|S )zMake 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)rS   rR   resultZresult_namesr*   Zv_namer&   r&   r'   unique_list   s    	
rW   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 | MappingIntStrAnyrK   )valuerG   r"   c                 C  s2   |  |}t|ttfr(| |t|}|| _d S r+   )_coerce_itemsr,   r-   r.   _normalize_indexeslenrY   )selfr[   rG   r&   r&   r'   __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_truerY   getr_   ra   r&   r&   r'   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
        rY   rd   r&   r&   r'   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)rY   rc   rb   )r_   rh   ra   r&   r&   r'   for_element   s    zValueItems.for_elementr   intzdict[int | str, Any])rG   v_lengthr"   c           	      C  s
  i }d}|  D ]\}}t|tjsPt|tjsP| |sPtd| d|j |dkrd| |}qt|t	svtd|dk r|| n|}| 
|||||< q|s|S | |rt|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__zExcluding fields from a sequence of sub-models or dicts must be performed index-wise: expected integer keys or keyword "__all__"r   .)rG   r,   typingr   AbstractSetrb   r6   	__class___coerce_valuerj   mergerc   range
setdefault)	r_   rG   rk   Znormalized_itemsZ	all_itemsir*   Znormalized_inormalized_itemr&   r&   r'   r]      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&   rM   rJ   )rw   r&   r'   
<listcomp>  rO   z$ValueItems.merge.<locals>.<listcomp>c                   s   g | ]}| v r|qS r&   r&   ry   rv   r&   r'   rz     rO   c                   s   g | ]}| vr|qS r&   r&   ry   r{   r&   r'   rz   	  rO   )rx   )rp   rb   r-   rq   rc   )r8   rv   rw   rx   Z
merge_keysmergedrJ   Zmerged_itemr&   )rv   rw   r'   rq      s"    


&
zValueItems.merge)rG   r"   c                 C  sF   t | tjrn4t | tjr(t| d} nt| dd}td| | S )N.ro   z???z!Unexpected type of exclude value )r,   rm   r   rn   rH   fromkeysgetattrr6   )rG   
class_namer&   r&   r'   r\     s    zValueItems._coerce_items)r[   r"   c                 C  s    |d u s|  |r|S | |S r+   )rb   r\   )r8   r[   r&   r&   r'   rp     s    zValueItems._coerce_valuer)   c                 C  s   | du p| du S )NT.r&   r1   r&   r&   r'   rb   $  s    zValueItems.is_truez_repr.ReprArgsr"   c                 C  s   d | j fgS r+   rf   r_   r&   r&   r'   __repr_args__(  s    zValueItems.__repr_args__N)F)__name__
__module____qualname____doc__	__slots__r`   re   rg   ri   r]   classmethodrq   staticmethodr\   rp   rb   r   r&   r&   r&   r'   rX      s    %%
rX   zCallable[[], T]name	get_valuer"   c                 C  s   d S r+   r&   r   r   r&   r&   r'   LazyClassAttribute.  rO   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   zA descriptor exposing an attribute only accessible on a class (hidden from instances).

        The attribute is lazily computed and cached during the first access.
        r=   zCallable[[], Any]rK   r   c                 C  s   || _ || _d S r+   r   )r_   r   r   r&   r&   r'   r`   8  s    zLazyClassAttribute.__init__r   r   c                 C  s   |   S r+   )r   r   r&   r&   r'   r[   <  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)r[   AttributeErrorr   r   )r_   r   r   r&   r&   r'   __get__@  s    zLazyClassAttribute.__get__N)r   r   r   r   r`   r   r[   r   r&   r&   r&   r'   r   2  s
   Obj)objr"   c              
   C  sZ   | j }|tv r| S z&| s6|tv r6|tu r,| n|  W S W n tttfyP   Y n0 t| S )zReturn 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.
    )	ro   r   r   r.   rF   r6   
ValueErrorRuntimeErrorr   )r   obj_typer&   r&   r'   smart_deepcopyI  s    r   ztyping.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&   r'   all_identical_  s    	r   T)frozenc                   @  sD   e Zd ZU dZdZded< ddddd	Zejr@dd
dddZ	dS )SafeGetItemProxyzWrapper 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   r=   r   )keyr"   c                C  s   | j |tS r+   )r   rc   r   r_   r   r&   r&   r'   __getitem__{  s    zSafeGetItemProxy.__getitem__r    c                C  s   | j |S r+   )r   __contains__r   r&   r&   r'   r     s    zSafeGetItemProxy.__contains__N)
r   r   r   r   r   __annotations__r   rm   TYPE_CHECKINGr   r&   r&   r&   r'   r   n  s   
r   )Ur   
__future__r   Z_annotationsdataclassesr@   rm   weakrefcollectionsr   r   r   rF   r   	functoolsr   inspectr   	itertoolsr	   typesr
   r   r   r   r   r   r   r   r   r   Ztyping_extensionsr   r    r   r   Z_import_utilsr   r   r   r   r   mainr   rj   floatcomplexr=   r    bytesr9   ZNoneTyperefNotImplementedro   Ellipsisr   r-   r/   r.   r0   rH   r   r(   r2   r7   r;   r<   rB   rC   rI   rP   rQ   rW   ZRepresentationrX   r   r   r   objectr   r   	dataclassr   r&   r&   r&   r'   <module>   s    		 
