o
    Ggmn                     @   s  d Z ddlZddlZddlZddlZddlmZ dejdd   kr'dk r0n nddlmZ	 n+ejdd dkrQzddlmZ	 W n e
yP   ddlmZ	 Y nw ddlmZ eeZ	ejdd dkZerjddlZd	Zd	Zd	Zd	Zd
ZerddlmZmZmZmZmZmZmZmZmZ ddlmZmZ ejdd dkrddlm Z  ee ej!fZ"nefZ"nddlmZm#Z#mZmZm$Z$mZm%Z%m&Z& z
ddlm'Z'm(Z( W n e
y   eeZ'd
Zd	ZY nw zddlm)Z) W n e
y   zddlm)Z) W n e
y   d
ZY nw Y nw zddlmZ W n e
y-   zddlmZ W n e
y*   d
ZY nw Y nw zddlmZ W n e
yU   zddlmZ W n e
yR   d
ZY nw Y nw dd Z*dd Z+dd Z,dd Z-dd Z.dd Z/zej0Z1W n e2y   dZ1Y nw d d! Z3ehZ4e5ed"re46ej d#d$ Z7d%d& Z8d'd( Z9d)d* Z:d+d, Z;d-d. Z<d/d0 Z=d1d2 Z>d3d4 Z?d5d6 Z@d[d7d8ZAd9d: ZBd;d< ZCd=d> ZDd?d@ ZEdAdB ZFdCdD ZGdEdF ZHdGdH ZId\dIdJZJd\dKdLZKd\dMdNZLd\dOdPZMdQdR ZNdSdT ZOdUdV ZPdWdX ZQdYdZ ZRdS )]zDefines experimental API for runtime inspection of types defined
in the standard "typing" module.

Example usage::
    from typing_inspect import is_generic_type
    N)_TypedDictMeta)      r   r   )r   	      )	TypedDict)r      r   TF)	GenericCallableUnionTypeVarClassVarTuple_GenericAlias
ForwardRefNewType)FinalLiteral)r   r   r   )_SpecialGenericAlias)r
   CallableMetar   r   	TupleMetar   GenericMeta_ForwardRef)_Union	_ClassVar)_Final)r   )r   c                 C   s<   t | tsJ t| dr| jS | jdur| j} | jdus| S )z@This function exists for compatibility with old typing versions._gorgN)
isinstancer   hasattrr   
__origin__cls r"   Q/var/www/html/AyurvedaChatbot/venv/lib/python3.10/site-packages/typing_inspect.pyr   \   s   


r   c                 C   sR   t rt| trt| tpt| to| jttt	t
jjfvS t| to(t| ttf S )a  Test if the given type is a generic type. This includes Generic itself, but
    excludes special typing constructs such as Union, Tuple, Callable, ClassVar.
    Examples::

        is_generic_type(int) == False
        is_generic_type(Union[int, str]) == False
        is_generic_type(Union[int, T]) == False
        is_generic_type(ClassVar[List[int]]) == False
        is_generic_type(Callable[..., T]) == False

        is_generic_type(Generic) == True
        is_generic_type(Generic[T]) == True
        is_generic_type(Iterable[int]) == True
        is_generic_type(Mapping) == True
        is_generic_type(MutableMapping[T, List[int]]) == True
        is_generic_type(Sequence[Union[str, bytes]]) == True
    )
NEW_TYPINGr   type
issubclassr	   typingGenericAliasr   r   tupler   collectionsabcr
   r   r   r   tpr"   r"   r#   is_generic_typef   s   

r-   c                 C   sR   t r#| tu p"t| tr| jtjju p"t| to"t| t	o"t| tjjS t| t
u S )a  Test if the type is a generic callable type, including subclasses
    excluding non-generic types and callables.
    Examples::

        is_callable_type(int) == False
        is_callable_type(type) == False
        is_callable_type(Callable) == True
        is_callable_type(Callable[..., int]) == True
        is_callable_type(Callable[[int, int], Iterable[str]]) == True
        class MyClass(Callable[[int], int]):
            ...
        is_callable_type(MyClass) == True

    For more general tests use callable(), for more precise test
    (excluding subclasses) use::

        get_origin(tp) is collections.abc.Callable  # Callable prior to Python 3.7
    )r$   r
   r   r'   r   r)   r*   r%   r&   r	   r   r+   r"   r"   r#   is_callable_type   s   r.   c                 C   sJ   t r| tu pt| tr| jtu pt| tot| tot| tS t| t	u S )a   Test if the type is a generic tuple type, including subclasses excluding
    non-generic classes.
    Examples::

        is_tuple_type(int) == False
        is_tuple_type(tuple) == False
        is_tuple_type(Tuple) == True
        is_tuple_type(Tuple[str, int]) == True
        class MyClass(Tuple[str, int]):
            ...
        is_tuple_type(MyClass) == True

    For more general tests use issubclass(..., tuple), for more precise test
    (excluding subclasses) use::

        get_origin(tp) is tuple  # Tuple prior to Python 3.7
    )
r$   r   r   r'   r   r(   r%   r&   r	   r   r+   r"   r"   r#   is_tuple_type   s   r/   c                 C   s6   | t du rdS t| rtdd t| ddD S dS )a  Test if the type is type(None), or is a direct union with it, such as Optional[T].

    NOTE: this method inspects nested `Union` arguments but not `TypeVar` definition
    bounds and constraints. So it will return `False` if
     - `tp` is a `TypeVar` bound, or constrained to, an optional type
     - `tp` is a `Union` to a `TypeVar` bound or constrained to an optional type,
     - `tp` refers to a *nested* `Union` containing an optional type or one of the above.

    Users wishing to check for optionality in types relying on type variables might wish
    to use this method in combination with `get_constraints` and `get_bound`
    NTc                 s   s    | ]}t |V  qd S N)is_optional_type).0ttr"   r"   r#   	<genexpr>   s    z#is_optional_type.<locals>.<genexpr>)evaluateF)r%   is_union_typeanyget_argsr+   r"   r"   r#   r1      s
   r1   c                 C   s0   t r| tu pt| to| jtu S tot| tu S )zTest if the type is a final type. Examples::

        is_final_type(int) == False
        is_final_type(Final) == True
        is_final_type(Final[int]) == True
    )r$   r   r   r'   r   
WITH_FINALr%   r   r+   r"   r"   r#   is_final_type   s
   r:   c                 C   s:   t r| tu pt| to| jtu ptot| tS t| tu S )a)  Test if the type is a union type. Examples::

        is_union_type(int) == False
        is_union_type(Union) == True
        is_union_type(Union[int, int]) == False
        is_union_type(Union[T, int]) == True
        is_union_type(int | int) == False
        is_union_type(T | int) == True
    )r$   r   r   r'   r   MaybeUnionTyper%   r   r+   r"   r"   r#   r6      s   
r6   r   c                 C   s4   t r| tv pt| to| jtv S tot| ttu S r0   )r$   LITERALSr   r'   r   WITH_LITERALr%   r   r+   r"   r"   r#   is_literal_type   s
   r>   c                 C   s   t | tu S )zTest if the type represents a type variable. Examples::

        is_typevar(int) == False
        is_typevar(T) == True
        is_typevar(Union[T, int]) == False
    )r%   r   r+   r"   r"   r#   
is_typevar   s   r?   c                 C   s4   t r| tu pt| to| jtu S trt| tu S dS )zTest if the type represents a class variable. Examples::

        is_classvar(int) == False
        is_classvar(ClassVar) == True
        is_classvar(ClassVar[int]) == True
        is_classvar(ClassVar[List[T]]) == True
    F)r$   r   r   r'   r   WITH_CLASSVARr%   r   r+   r"   r"   r#   is_classvar  s   rA   c                 C   s   t sdS tjdd dkr"tjjdkr"| ttjfv p!t| ttjfS tjdd dkr]zt| tj}W n	 ty<   Y nw |rA|S | ttjfv p\t| ddduo\t| dd	d
ko\| j	dv S | tu pmt| ddduom| j	dv S )zTests if the type represents a distinct type. Examples::

        is_new_type(int) == False
        is_new_type(NewType) == True
        is_new_type(NewType('Age', int)) == True
        is_new_type(NewType('Scores', List[Dict[str, float]])) == True
    FNr   )r   
   r   beta)r   r   r   __supertype____qualname__ zNewType.<locals>.new_type)typingtyping_extensions)
WITH_NEWTYPEsysversion_inforeleaselevelr   rH   r   	TypeErrorgetattr
__module__)r,   resr"   r"   r#   is_new_type  s0   rQ   c                 C   s   t st| tS t| tS )zTests if the type is a :class:`typing.ForwardRef`. Examples::

        u = Union["Milk", Way]
        args = get_args(u)
        is_forward_ref(args[0]) == True
        is_forward_ref(args[1]) == False
    )r$   r   r   r   r+   r"   r"   r#   is_forward_ref6  s   

rR   c                 C   s:   t rtdt }t| d|}||u rdS |du r| S |S )a  Get the last base of (multiply) subscripted type. Supports generic types,
    Union, Callable, and Tuple. Returns None for unsupported types.
    Examples::

        get_last_origin(int) == None
        get_last_origin(ClassVar[int]) == None
        get_last_origin(Generic[T]) == Generic
        get_last_origin(Union[T, int][str]) == Union[T, int]
        get_last_origin(List[Tuple[T, T]][int]) == List[Tuple[T, T]]
        get_last_origin(List) == List
    zEThis function is only supported in Python 3.6, use get_origin insteadr   N)r$   
ValueErrorobjectrN   )r,   sentineloriginr"   r"   r#   get_last_originC  s   rW   c                 C   sz   t rt| tr| jtur| jS dS | tu rtS dS t| tr"t| S t| r(t	S t
| r.tS t| r;t r9| jp8| S tS dS )a  Get the unsubscripted version of a type. Supports generic types, Union,
    Callable, and Tuple. Returns None for unsupported types. Examples::

        get_origin(int) == None
        get_origin(ClassVar[int]) == None
        get_origin(Generic) == Generic
        get_origin(Generic[T]) == Generic
        get_origin(Union[T, int]) == Union
        get_origin(List[Tuple[T, T]][int]) == list  # List prior to Python 3.7
    N)r$   r   r'   r   r   r	   r   r   r6   r   r/   r   r>   r   r+   r"   r"   r#   
get_origin[  s"   


rX   c                 C   s  t rt| rg }| jdur| jndD ]}|t|7 }qt|S t| r<g }| jdur-| jndD ]}|t|7 }q/t|S t| rg }| j}|du rKdS |D ].}t|rWt	|n|fD ] }t
|rkt|tsktd| |du rqg }||vrz|| qZqM|durt|S dS dS trt| trt| dst| trt| tr| tur| jS dS t| st| st| st| r| jdur| jS dS dS )a>  Return type parameters of a parameterizable type as a tuple
    in lexicographic order. Parameterizable types are generic types,
    unions, tuple types and callable types. Examples::

        get_parameters(int) == ()
        get_parameters(Generic) == ()
        get_parameters(Union) == ()
        get_parameters(List[int]) == ()

        get_parameters(Generic[T]) == (T,)
        get_parameters(Tuple[List[T], List[S_co]]) == (T, S_co)
        get_parameters(Union[S_co, Tuple[T, T]][int, U]) == (U,)
        get_parameters(Mapping[T, Tuple[S_co, T]]) == (T, S_co)
    Nr"   zKCannot inherit from a generic class parameterized with non-type-variable %s__parameters__)LEGACY_TYPINGr6   __union_params__get_parametersr(   r/   __tuple_params__r-   rY   r8   _has_type_varr   r   rM   appendr$   r'   r   r%   r&   r	   r.   )r,   paramsargbase_paramsbp_bpr"   r"   r#   r\   z  sp   

r\   c                 C   s6  t rtdt| r| jdur| jfS dS t| r?z| jdur*t| jdkr*| jW S W n	 ty4   Y nw | jdur=| jS dS t	| rez| jdurM| jW S dW S  tyd   | j
dur`| j
 Y S d Y S w t| rs| jdurq| jS dS t| rz| jdur| jW S dW S  ty   | jdur| j Y S d Y S w dS )a  Get last arguments of (multiply) subscripted type.
       Parameters for Callable are flattened. Examples::

        get_last_args(int) == ()
        get_last_args(Union) == ()
        get_last_args(ClassVar[int]) == (int,)
        get_last_args(Union[T, int]) == (T, int)
        get_last_args(Iterable[Tuple[T, S]][int, T]) == (int, T)
        get_last_args(Callable[[T], int]) == (T, int)
        get_last_args(Callable[[], int]) == (int,)
    zCThis function is only supported in Python 3.6, use get_args insteadNr"   r   )r$   rS   rA   __type__r-   __args__lenAttributeErrorrY   r6   r[   r.   r/   r]   r+   r"   r"   r#   get_last_args  s8   ri   c              
   C   s   g }| D ]f}t |ts|| qt|d rUt|dd }t|dkr1|tg |d f  q|d tu rC|td|d f  q|tt|dd |d f  q|t	|d 
|d t|dd  qt|S )zInternal helper for get_args.r      Nr   .)r   r(   r_   r.   
_eval_argsrg   r
   Ellipsislistr%   __getitem__)argsrP   ra   callable_argsr"   r"   r#   rl     s   
$,rl   c                 C   s  t rA|dur|stdt| tr5t| dr5| j}t| tjj	u r3|d t
ur3t|dd |d f}|S tr?t| tr?| jS dS t| sIt| rT| jdurR| jfS dS t| r]| jp\dS t| smt| smt| smt| rz|  }W n' ty   t| rt| }nt| rt| }nt| rt| }nY dS Y nw t|trt|dkr|s|dd S t|dd }t| t	u r|d t
urt|dd |d f}|S dS )a  Get type arguments with all substitutions performed. For unions,
    basic simplifications used by Union constructor are performed.
    On versions prior to 3.7 if `evaluate` is False (default),
    report result as nested tuple, this matches
    the internal representation of types. If `evaluate` is True
    (or if Python version is 3.7 or greater), then all
    type parameters are applied (this could be time and memory expensive).
    Examples::

        get_args(int) == ()
        get_args(Union[int, Union[T, int], str][int]) == (int, str)
        get_args(Union[int, Tuple[T, int]][str]) == (int, (Tuple, str, int))

        get_args(Union[int, Tuple[T, int]][str], evaluate=True) ==                  (int, Tuple[str, int])
        get_args(Dict[int, Tuple[T, T]][Optional[int]], evaluate=True) ==                  (int, Tuple[Optional[int], Optional[int]])
        get_args(Callable[[], T][int], evaluate=True) == ([], int,)
    Nz*evaluate can only be True in Python >= 3.7rf   r   rk   r"   rj   )r$   rS   r   r'   r   rf   rX   r)   r*   r
   rm   rn   r;   rA   r:   re   r>   
__values__r-   r6   r.   r/   
_subs_treerh   _union_subs_tree_generic_subs_tree_tuple_subs_treer(   rg   rl   )r,   r5   rP   treer"   r"   r#   r8     sV   



r8   c                 C   $   t | r
t| ddS tdt|  )zReturn the type bound to a `TypeVar` if any.

    It the type is not a `TypeVar`, a `TypeError` is raised.
    Examples::

        get_bound(TypeVar('T')) == None
        get_bound(TypeVar('T', bound=int)) == int
    	__bound__Ntype is not a `TypeVar`: r?   rN   rM   strr+   r"   r"   r#   	get_boundA     
r}   c                 C   rx   )zReturns the constraints of a `TypeVar` if any.

    It the type is not a `TypeVar`, a `TypeError` is raised
    Examples::

        get_constraints(TypeVar('T')) == ()
        get_constraints(TypeVar('T', int, str)) == (int, str)
    __constraints__r"   rz   r{   r+   r"   r"   r#   get_constraintsQ  r~   r   c                 C   s    t | dd}|dur|S t| S )a6  Get the generic type of an object if possible, or runtime class otherwise.
    Examples::

        class Node(Generic[T]):
            ...
        type(Node[int]()) == Node
        get_generic_type(Node[int]()) == Node[int]
        get_generic_type(Node[T]()) == Node[T]
        get_generic_type(1) == int
    __orig_class__N)rN   r%   )objgen_typer"   r"   r#   get_generic_typea  s   r   c                 C   s$   t rtdd | jD S t| ddS )a  Get generic base types of a type or empty tuple if not possible.
    Example::

        class MyClass(List[int], Mapping[str, List[int]]):
            ...
        MyClass.__bases__ == (List, Mapping)
        get_generic_bases(MyClass) == (List[int], Mapping[str, List[int]])
    c                 s   s    | ]
}t |tr|V  qd S r0   )r   r   r2   tr"   r"   r#   r4   {  s    z$get_generic_bases.<locals>.<genexpr>__orig_bases__r"   )rZ   r(   	__bases__rN   r+   r"   r"   r#   get_generic_basesq  s   	r   c                 C   s   t | ttfr| j S dS )a  If td is a TypedDict class, return a dictionary mapping the typed keys to types.
    Otherwise, return None. Examples::

        class TD(TypedDict):
            x: int
            y: int
        class Other(dict):
            x: int
            y: int

        typed_dict_keys(TD) == {'x': int, 'y': int}
        typed_dict_keys(dict) == None
        typed_dict_keys(Other) == None
    N)r   _TypedDictMeta_Mypy_TypedDictMeta_TE__annotations__copy)tdr"   r"   r#   typed_dict_keys  s   
r   c                 C   s   t | r| jS dS )a  
    If fr is a ForwardRef, return the string representation of the forward reference.
    Otherwise return None. Examples::

        tp = List["FRef"]
        fr = get_args(tp)[0]
        get_forward_arg(fr) == "FRef"
        get_forward_arg(tp) == None
    N)rR   __forward_arg__)frr"   r"   r#   get_forward_arg  s   
r   c                 C   s|   |du rg }t | rt| ||S t| rt| ||S t| r$t| ||S t| tr<t|D ]\}}| |kr;||   S q-| S )zbackport of _replace_argN)	r6   rt   r/   rv   r-   ru   r   r   	enumerate)ra   tvarsrp   itvarr"   r"   r#   _replace_arg  s   
r   c                    s  g }| D ].}t |tr||j qt |tr-t|dkr-|d tu r-||dd  q|| qt| t t|k r\g }|D ]}| v rS||  	| qC|} r\J  t| |D ]t t
sjqbtfdd h D r} 	 qbt fdd|D S )z backport of _remove_dups_flattenr   rj   Nc                 3   sD    | ]}t |trt|d ust |tst |tot |V  qd S r0   )r   r   rX   r   r%   r&   )r2   t2)t1r"   r#   r4     s    z'_remove_dups_flatten.<locals>.<genexpr>c                 3   s    | ]	}| v r|V  qd S r0   r"   r   )
all_paramsr"   r#   r4     s    )r   r   extendr[   r(   rg   r   r_   setremover%   r7   )
parametersr`   p
new_paramsr   r"   )r   r   r#   _remove_dups_flatten  s6   
"



r   c                 C   s   dd }|| }|du rt | st| s| S g }||dur-|| ||}||dusg }dd }|| D ]}|t||| q7|D ]}	g }
||	D ]}|
t|t|	| qM|
}qE|S )z;backport of typing._subs_tree, adapted for legacy versions c                 S   s   z| j W S  ty   Y d S w r0   )r   rh   r    r"   r"   r#   _get_origin  s
   z_subs_tree.<locals>._get_originNc                 S   sR   t | r| j}nt| r| j}nz| j}W n ty    d}Y nw |d ur'|S dS )Nr"   )r6   r[   r/   r]   rf   rh   )r!   cls_argsr"   r"   r#   	_get_args  s   
z_subs_tree.<locals>._get_args)r6   r/   r_   r   r\   )r!   r   rp   r   current
orig_chain	tree_argsr   ra   oclsnew_tree_argsr"   r"   r#   rs     s(   
rs   c                 C   s>   | t u rt S t| ||}t|}t|dkr|d S t f| S )z backport of Union._subs_tree rj   r   )r   rs   r   rg   r,   r   rp   r   r"   r"   r#   rt     s   
rt   c                 C   s,   | j du r| S t| ||}t| ft| S )z$ backport of GenericMeta._subs_tree N)r   rs   r   r(   r   r"   r"   r#   ru     s   
ru   c                 C   s&   | t u rt S t| ||}t ft| S )z7 ad-hoc function (inspired by union) for legacy typing )r   rs   r(   r   r"   r"   r#   rv     s   rv   c                 C   sP   | d u rdS t | rt| S t| rt| S t| rt| S t| r&t| S dS )NF)r6   _union_has_type_varr/   _tuple_has_type_varr-   _generic_has_type_varr.   _callable_has_type_var)r   r"   r"   r#   r^   '  s   r^   c                 C   $   | j r| j D ]	}t|r dS qdS NTF)r[   r^   r,   r   r"   r"   r#   r   6     
r   c                 C   r   r   )r]   r^   r   r"   r"   r#   r   >  r   r   c                 C   s*   | j r| j D ]	}t|r dS qt| jS )NT)rf   r^   
__result__r   r"   r"   r#   r   F  s   

r   c                 C   r   r   )rY   r^   r   r"   r"   r#   r   N  r   r   r0   )NN)S__doc__rJ   typesrG   rH   mypy_extensionsr   r   rK   r   ImportErrorr   r%   r$   collections.abcr)   r9   r=   r@   rI   rZ   r	   r
   r   r   r   r   r   r   r   r   r   r   GenericAliasr'   r   r   r   r   r   r   r   r   r-   r.   r/   r1   r:   	UnionTyper;   rh   r6   r<   r   addr>   r?   rA   rQ   rR   rW   rX   r\   ri   rl   r8   r}   r   r   r   r   r   r   r   rs   rt   ru   rv   r^   r   r   r   r   r"   r"   r"   r#   <module>   s    	",(

 E+
D
(
-

