B
    xbe                 @   sZ  d Z ddlZddlZddlmZ dejdd   kr>dk rPn nddlmZ n4ejdd dkrpddl	mZ nddl	m
Z
 ee
Zejdd dkZerddlZd	Zd	Zd	Zd
Zer(ddl	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rddl	mZ eeejfZnefZnddl	mZm Z mZmZm!Z!mZm"Z"m#Z# yddl	m$Z$m%Z% W n& e&k
r   eeZ$d
Zd	ZY nX yddlm'Z' W nB e&k
r   yddl	m'Z' W n e&k
r   d
ZY nX Y nX yddlm(Z( W nB e&k
r2   yddl	m(Z( W n e&k
r,   d
ZY nX Y nX dd Z)dd Z*dd Z+dd Z,dd Z-dd Z.dd  Z/d!d" Z0d#d$ Z1d%d& Z2d'd( Z3d)d* Z4d+d, Z5d-d. Z6d/d0 Z7d1d2 Z8d3d4 Z9dYd5d6Z:d7d8 Z;d9d: Z<d;d< Z=d=d> Z>d?d@ Z?dAdB Z@dCdD ZAdEdF ZBdZdGdHZCd[dIdJZDd\dKdLZEd]dMdNZFdOdP ZGdQdR ZHdSdT ZIdUdV ZJdWdX ZKd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)FinalLiteral)r   r   r   )_SpecialGenericAlias)r
   CallableMetar   r   	TupleMetar   GenericMeta_ForwardRef)_Union	_ClassVar)_Final)_Literalc             C   s8   t | tstt| dr| jS x| jdk	r2| j} q W | S )z@This function exists for compatibility with old typing versions._gorgN)
isinstancer   AssertionErrorhasattrr   
__origin__)cls r"   Q/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/typing_inspect.pyr   J   s    

r   c             C   sR   t r8t| trt| tp6t| to6| jttt	t
jjfkS t| toPt| 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_typeT   s    

r,   c             C   sR   t rF| tkpDt| tr$| jtjjkpDt| toDt| t	oDt| tjjS t| t
k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_typen   s    r-   c             C   sJ   t r>| tkp<t| tr | jtkp<t| to<t| to<t| tS t| t	k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   s:   | t dkrdS t| r2tdd t| ddD S 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_args)r+   r"   r"   r#   r/      s
    r/   c             C   s0   t r | tkpt| to| jtkS to.t| tk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    r8   c             C   s,   t r | tkpt| to| jtkS t| tkS )zTest 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
    )r$   r   r   r'   r    r%   r   )r+   r"   r"   r#   r4      s    r4   c             C   s0   t r | tkpt| to| jtkS to.t| tkS )N)r$   r   r   r'   r    WITH_LITERALr%   r   )r+   r"   r"   r#   is_literal_type   s    r:   c             C   s   t | tk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   s8   t r | tkpt| to| jtkS tr0t| tkS d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
    FN)r$   r   r   r'   r    WITH_CLASSVARr%   r   )r+   r"   r"   r#   is_classvar   s    r=   c             C   s   t | dddk	S )zTests if the type represents a distinct type. Examples::

        is_new_type(int) == False
        is_new_type(NewType('Age', int)) == True
        is_new_type(NewType('Scores', List[Dict[str, float]])) == True
    __supertype__N)getattr)r+   r"   r"   r#   is_new_type   s    r@   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_ref   s    
rA   c             C   s:   t rtdt }t| d|}||kr*dS |dkr6| 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objectr?   )r+   sentineloriginr"   r"   r#   get_last_origin  s    rF   c             C   s`   t r2t| tr"| jtk	r| jS dS | tkr.tS dS t| trDt| S t| rPt	S t
| r\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   r4   r   r.   r   )r+   r"   r"   r#   
get_origin%  s    

rG   c             C   s  t r*t| rDg }x(| jdk	r$| jndD ]}|t|7 }q(W t|S t| rg }x(| jdk	rb| jndD ]}|t|7 }qfW t|S t| r$g }| j}|dkrdS xh|D ]`}xZt|rt	|n|fD ]@}t
|rt|tstd| |dkrg }||kr|| qW qW |dk	rt|S dS ndS ntrjt| ts^t| trdt| trd| tk	rd| jS dS nBt| st| st| st| r| jdk	r| jS d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)LEGACY_TYPINGr4   __union_params__get_parametersr(   r.   __tuple_params__r,   __parameters__r6   _has_type_varr   r   	TypeErrorappendr$   r'   r%   r&   r	   r-   )r+   paramsargZbase_paramsZbp_bpr"   r"   r#   rJ   @  sP    



rJ   c             C   s6  t rtdn"t| r.| jdk	r*| jfS dS t| ry"| jdk	rVt| jdkrV| jS W n tk
rl   Y nX | jdk	r~| jS dS t	| ry| jdk	r| jS dS  tk
r   | j
dk	r| j
S dS X nlt| r| jdk	r| jS dS t| r.y| jdk	r | jS dS  tk
r*   | jdk	r&| jS dS X ndS 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$   rB   r=   __type__r,   __args__lenAttributeErrorrL   r4   rI   r-   r.   rK   )r+   r"   r"   r#   get_last_args  s0    

rW   c          
   C   s   g }x| D ]}t |ts$|| q
t|d rt|dd }t|dkrd|tg |d f  q|d tkr|td|d f  q|tt|dd |d f  q
|t	|d 
|d t|dd  q
W t|S )zInternal helper for get_args.r      Nr   .)r   r(   rO   r-   
_eval_argsrU   r
   Ellipsislistr%   __getitem__)argsresrQ   Zcallable_argsr"   r"   r#   rZ     s    

$.rZ   c             C   s  t rn|dk	r|stdt| trjt| drj| j}t| tjj	krf|d t
k	rft|dd |d f}|S dS t| s~t| r| jdk	r| jfS dS t| r| jpdS t| st| st| st| ry|  }W nT tk
r(   t| rt| }n,t| rt| }nt| r t| }ndS Y nX t|trt|dkr|sV|dd S t|dd }t| t	kr|d t
k	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.7rT   r   rY   r"   rX   )r$   rB   r   r'   r   rT   rG   r)   r*   r
   r[   r\   r=   r8   rS   r:   Z
__values__r,   r4   r-   r.   
_subs_treerV   _union_subs_tree_generic_subs_tree_tuple_subs_treer(   rU   rZ   )r+   r3   r_   treer"   r"   r#   r6     sB    






r6   c             C   s(   t | rt| ddS tdt|  dS )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__Nztype is not a `TypeVar`: )r;   r?   rN   str)r+   r"   r"   r#   	get_bound  s    
rg   c             C   s(   t | rt| ddS tdt|  dS )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"   ztype is not a `TypeVar`: N)r;   r?   rN   rf   )r+   r"   r"   r#   get_constraints  s    
ri   c             C   s    t | dd}|dk	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)r?   r%   )objZgen_typer"   r"   r#   get_generic_type!  s    rl   c             C   s(   t rtdd | jD S t| ddS 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 )N)r   r   )r0   tr"   r"   r#   r2   ;  s    z$get_generic_bases.<locals>.<genexpr>__orig_bases__r"   N)rH   r(   	__bases__r?   )r+   r"   r"   r#   get_generic_bases1  s    	rp   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    
rv   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)rA   __forward_arg__)frr"   r"   r#   get_forward_argT  s    
ry   c             C   s|   |dkrg }t | r t| ||S t| r4t| ||S t| rHt| ||S t| trxx$t|D ]\}}| |kr\|| S q\W | S )zbackport of _replace_argN)	r4   ra   r.   rc   r,   rb   r   r   	enumerate)rQ   tvarsr^   itvarr"   r"   r#   _replace_argc  s    
r~   c                s  g }xd| D ]\}t |tr&||j q
t |tr\t|dkr\|d tkr\||dd  q
|| q
W t| t t|k rg }x(|D ] }| kr||  	| qW |} rt
 t| x>|D ]6t tsqtfdd h D rΈ 	 qW t fdd|D S )z backport of _remove_dups_flattenr   rX   Nc             3   sB   | ]:}t |trt|d k	st |tst |to8t |V  qd S )N)r   r   rG   r   r%   r&   )r0   t2)t1r"   r#   r2     s   
z'_remove_dups_flatten.<locals>.<genexpr>c             3   s   | ]}| kr|V  qd S )Nr"   )r0   rm   )
all_paramsr"   r#   r2     s    )r   r   extendrI   r(   rU   r   rO   setremover   r%   r5   )
parametersrP   p
new_paramsrm   r"   )r   r   r#   _remove_dups_flattenv  s0    

"



r   c             C   s   dd }|| }|dkr,t | s,t| s,| S g }x"||dk	rR|| ||}q2W g }dd }x"|| D ]}|t||| qjW x<|D ]4}	g }
x&||	D ]}|
t|t|	| qW |
}qW |S )z;backport of typing._subs_tree, adapted for legacy versions c             S   s    y| j S  tk
r   d S X d S )N)r    rV   )r!   r"   r"   r#   _get_origin  s    z_subs_tree.<locals>._get_originNc             S   sT   t | r| j}n4t| r | j}n$y
| j}W n tk
rB   d}Y nX |d k	rP|S dS )Nr"   )r4   rI   r.   rK   rT   rV   )r!   Zcls_argsr"   r"   r#   	_get_args  s    

z_subs_tree.<locals>._get_args)r4   r.   rO   r~   rJ   )r!   r{   r^   r   currentZ
orig_chain	tree_argsr   rQ   ZoclsZnew_tree_argsr"   r"   r#   r`     s&    

r`   c             C   s>   | t krt S t| ||}t|}t|dkr4|d S t f| S )z backport of Union._subs_tree rX   r   )r   r`   r   rU   )r+   r{   r^   r   r"   r"   r#   ra     s    ra   c             C   s,   | j dkr| S t| ||}t| ft| S )z$ backport of GenericMeta._subs_tree N)r    r`   r   r(   )r+   r{   r^   r   r"   r"   r#   rb     s    
rb   c             C   s&   | t krt S t| ||}t ft| S )z7 ad-hoc function (inspired by union) for legacy typing )r   r`   r(   )r+   r{   r^   r   r"   r"   r#   rc     s    rc   c             C   sT   | d krdS t | rt| S t| r,t| S t| r<t| S t| rLt| S dS d S )NF)r4   _union_has_type_varr.   _tuple_has_type_varr,   _generic_has_type_varr-   _callable_has_type_var)rm   r"   r"   r#   rM     s    rM   c             C   s&   | j r"x| j D ]}t|rdS qW dS )NTF)rI   rM   )r+   rm   r"   r"   r#   r     s
    r   c             C   s&   | j r"x| j D ]}t|rdS qW dS )NTF)rK   rM   )r+   rm   r"   r"   r#   r     s
    r   c             C   s,   | j r"x| j D ]}t|rdS qW t| jS )NT)rT   rM   Z
__result__)r+   rm   r"   r"   r#   r     s
    r   c             C   s&   | j r"x| j D ]}t|rdS qW dS )NTF)rL   rM   )r+   rm   r"   r"   r#   r     s
    r   )N)NN)NN)NN)NN)L__doc__systypesZmypy_extensionsr   rq   version_infotyping_extensionsrr   typingr   r%   r$   collections.abcr)   r7   r9   r<   rH   r	   r
   r   r   r   r   r   r   r   r   r   GenericAliasr'   r   r   r   r   r   r   ImportErrorr   r   r   r,   r-   r.   r/   r8   r4   r:   r;   r=   r@   rA   rF   rG   rJ   rW   rZ   r6   rg   ri   rl   rp   rv   ry   r~   r   r`   ra   rb   rc   rM   r   r   r   r   r"   r"   r"   r#   <module>   s   "(
(


A+
B(
-


