o
    3Ih7                     @   s  U d Z ddlZddlZddlZddlZddlZeedr"eedZ	needr-eedZ	ne
 eedr;eedZndZeedrHeedZndZeedrUeedZndZdZejeejeejeejeejeejejjejejjeddiZejejej ejej!ejej"ej#ej$ej#ej%ej#iZ&ejeej'f e(d< erd	e)d
e*fddZ+n	d	e)d
e*fddZ+d
ej,e* fddZ-d
ej,e* fddZ.dd Z/ej0Z0eejZ1eej2Z3ej2dedf Z'ej2def Z4ej2eej5e eje f Z6de'd
e'fddZ7dd Z8d
ejfddZ9d3ddZ:G dd dej;Z<d e6fd!d"Z=de'd
e<fd#d$Z>d%e4d&e4d
e*fd'd(Z?ej2ejej@d)f ejAe< e<f ZBd%d*d&d*d+ej,ejCeDef  d
ej,e* fd,d-ZEd%e<d&e<d+ej,ejCeDef  d
ej,e* fd.d/ZF	d3d%e'd&e'd+ej,e d
ej,e* fd0d1ZGg d2ZHdS )4a}  
Backport Python3.8+ typing utils &amp; issubtype &amp; more

![Python 3.6](https://github.com/bojiang/typing_utils/workflows/Python%203.6/badge.svg)
![Python 3.7](https://github.com/bojiang/typing_utils/workflows/Python%203.7/badge.svg)
![Python 3.8](https://github.com/bojiang/typing_utils/workflows/Python%203.8/badge.svg)

## Install

``` bash
    pip install typing_utils
```
    N
ForwardRef_ForwardRefLiteral_TypedDictMeta	UnionTypeSTATIC_SUBTYPE_MAPPINGelementreturnc                 C   s   | t ju p| tu S N)typingUnionr   r    r   V/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/overrides/typing_utils.pyis_unionC   s   r   c                 C   s
   | t ju S r
   )r   r   r   r   r   r   r   H   s   
c                 C   s&   t | rdS t dd | D rdS tS )NTc                 s   s    | ]}|d u V  qdS )FNr   .0er   r   r   	<genexpr>O       zoptional_all.<locals>.<genexpr>F)allunknownelementsr   r   r   optional_allL   
   r   c                 C   s&   t | rdS t dd | D rtS dS )NTc                 s   s    | ]}|d u V  qd S r
   r   r   r   r   r   r   W   r   zoptional_any.<locals>.<genexpr>F)anyr   r   r   r   r   optional_anyT   r   r   c                 C   s$   zt |  W dS  ty   Y dS w )z(Determine whether `value` can be hashed.FT)hash	TypeError)valuer   r   r   	_hashable\   s   
r!   ztyping.TypeVartype_c                 C   s4   t | tjr| S t| sJ d| tv rt|  S | S )Nz9_normalize_aliases should only be called on element types)
isinstancer   TypeVarr!   BUILTINS_MAPPING)r"   r   r   r   _normalize_aliaseso   s   r&   c                 C   s   t tdrttd}|| }nLt tjdr8t| tr!| js!| j}n:t | dr,| jr,| }n/| tju r5tj}n&d}n#t| trG| j}|du rF| }nt| t	rP| j}n| tju rYtj}nd}|du rht
rht| t
rht}t|S )a%  Get the unsubscripted version of a type.
    This supports generic types, Callable, Tuple, Union, Literal, Final and ClassVar.
    Return None for unsupported types.

    Examples:

    ```python
        from typing_utils import get_origin

        get_origin(Literal[42]) is Literal
        get_origin(int) is None
        get_origin(ClassVar[int]) is ClassVar
        get_origin(Generic) is Generic
        get_origin(Generic[T]) is Generic
        get_origin(Union[T, int]) is Union
        get_origin(List[Tuple[T, T]][int]) == list
    ```
    
get_origin_specialN)hasattrr   getattrListr#   GenericClassr(   
__origin__Generic
UnionClassr   dictr&   )r"   _getterorir   r   r   r'   z   s0   






r'   c                 C   s   t tdrttd}|| }nVt tjdr>t| tr;| js;| j}t| t	j
ju r:|d tur:t|dd |d f}n*d}n't| ttfrc| j}t| t	j
ju rb|d turbt|dd |d f}nd}trqt| trqttjfS |du rwdS |S )a  Get type arguments with all substitutions performed.
    For unions, basic simplifications used by Union constructor are performed.

    Examples:

    ```python
        from typing_utils import get_args

        get_args(Dict[str, int]) == (str, int)
        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(Callable[[], T][int]) == ([], int)
    ```
    get_argsr(   r   Nr   )r)   r   r*   r+   r#   r,   r(   __args__r'   collectionsabcCallableEllipsislistr/   r   strAny)r"   r1   resr   r   r   r3      s,   



r3   c                 C   sP   |pi }t tdrttd}|| t |S t | dr%t| d}|t |S t )z3
    eval forward_refs in all cPython versions
    
_eval_type)r)   r   r*   globalsNotImplementedError)refforward_refslocalnsr>   r   r   r   eval_forward_ref   s   



rD   c                   @   sP   e Zd ZU dZeed< e Zej	ee
f ed< dd ZdefddZd	d
 ZdS )NormalizedTypezK
    Normalized type, made it possible to compare, hash between types.
    originargsc                 C   sv   t |tr1| j|jkrdS t | jtr%t |jtr%| j|jko$|j| jkS | j|jko0| j|jkS | js9| j|kS dS )NF)r#   rE   rF   rG   	frozenset)selfotherr   r   r   __eq__   s   

zNormalizedType.__eq__r	   c                 C   s    | j st| jS t| j| j fS r
   )rG   r   rF   rI   r   r   r   __hash__   s   
zNormalizedType.__hash__c                 C   s"   | j s| j S | j d| j  dS )N[z]))rG   rF   rL   r   r   r   __repr__   s   zNormalizedType.__repr__N)__name__
__module____qualname____doc__Type__annotations__tuplerG   r   r   rH   rK   intrM   rO   r   r   r   r   rE      s   
 rE   tpsc                 C   sV   t | tr| S t | tjjrtdd | D S t | tjjr'tdd | D S t| S )Nc                 s       | ]}t |V  qd S r
   _normalize_argsr   r"   r   r   r   r     r   z"_normalize_args.<locals>.<genexpr>c                 s   rY   r
   rZ   r\   r   r   r   r     r   )	r#   r;   r6   r7   SequencerV   SetrH   	normalize)rX   r   r   r   r[      s   
r[   c                 C   sP   t | }t| }|stt| S t|}t|rtt|}nt|}t||S )z4
    convert types to NormalizedType instances.
    )r3   r'   rE   r&   r   r[   rH   )r"   rG   rF   r   r   r   r_   
  s   
r_   leftrightc                 C   sx   | |u rdS | d ur| t v r|t |  krdS t| dr)|  D ]	}||kr( dS qt| tr8t|tr8t| |S | |kS )NTmro)r   r)   rb   r#   type
issubclass)r`   ra   parentr   r   r   _is_origin_subtype  s   

rf   .NormalizedTypeArgsrB   c                    s
  t | tr t tsdS |  }|sdS t fdd|D S t | tjjrqt | tsqt tjjr8t tr:dS | rX| d jturXrXd jtu rXt fdd| D S t	| t	krbdS t fddt
| D S t | tsxJ t tsJ t|  S )NFTc                 3   s(    | ] t  fd dD V  qdS )c                 3   s    | ]	}t  |V  qd S r
   _is_normal_subtype)r   r)r   rB   r   r   r   H  s    z4_is_origin_subtype_args.<locals>.<genexpr>.<genexpr>N)r   )r   rB   ra   )r   r   r   G  s    
z*_is_origin_subtype_args.<locals>.<genexpr>r4   c                 3   s     | ]}t |d   V  qdS )r   N_is_origin_subtype_args)r   lrk   r   r   r   Z  s    c                 3   s0    | ]\}}|d uo|d uot || V  qd S r
   rl   )r   rn   rj   rB   r   r   r   _  s    

)r#   rH   r   r6   r7   r]   rE   rF   r9   len	itertoolszip_longestri   )r`   ra   rB   excludedr   rk   r   rm   8  s>   


rm   c                    s  t jtrttj dt jtrttj djtju r&dS tjr8tjr8tj	j	 S tjrJt
 fddj	D S tjr\t fddj	D S jtu rsjturhdS tj	tj	S t jtjrt jtjrjju rdS tjdd }tjdd }|d u s|d u rtS tt|t| S t jtjrtS t jtjrtjdd }|d u rtS tt| S j	s݈j	stjjS j	stjjS tjjrtj	j	 S dS )Nro   Tc                 3   s    | ]	}t | V  qd S r
   rh   r   a)rB   r`   r   r   r         
z%_is_normal_subtype.<locals>.<genexpr>c                 3   s    | ]	}t | V  qd S r
   rh   rt   rk   r   r   r     rv   F	__bound__)r#   rF   r   r_   rD   r   r<   r   rm   rG   r   r   r   setissubsetr$   r*   r   ri   rf   )r`   ra   rB   
left_boundright_boundr   )rB   r`   ra   r   ri   l  s\   



ri   c                 C   s   t t| t||S )a  Check that the left argument is a subtype of the right.
    For unions, check if the type arguments of the left is a subset of the right.
    Also works for nested types including ForwardRefs.

    Examples:

    ```python
        from typing_utils import issubtype

        issubtype(typing.List, typing.Any) == True
        issubtype(list, list) == True
        issubtype(list, typing.List) == True
        issubtype(list, typing.Sequence) == True
        issubtype(typing.List[int], list) == True
        issubtype(typing.List[typing.List], list) == True
        issubtype(list, typing.List[int]) == False
        issubtype(list, typing.Union[typing.Tuple, typing.Set]) == False
        issubtype(typing.List[typing.List], typing.List[typing.Sequence]) == True
        JSON = typing.Union[
            int, float, bool, str, None, typing.Sequence["JSON"],
            typing.Mapping[str, "JSON"]
        ]
        issubtype(str, JSON, forward_refs={'JSON': JSON}) == True
        issubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON}) == True
        issubtype(typing.Dict[str, bytes], JSON, forward_refs={'JSON': JSON}) == False
    ```
    )ri   r_   )r`   ra   rB   r   r   r   	issubtype  s    r|   )r|   r'   r3   get_type_hintsr
   )IrS   collections.abcr6   iorq   typesr   r)   r*   r   r@   r   r   r   r   r+   r:   r^   rx   Dictr0   TuplerV   
ByteStringbytesr8   r7   r]   rc   r%   TextIOWrapperTextIO
TextIOBaseStringIOBufferedReaderBinaryIOBufferedWriterBytesIOr   rT   rU   objectboolr   Optionalr   r   r!   r}   r,   r   r/   
OriginTypeAbstractSetTypeArgsr&   r'   r3   rD   
NamedTuplerE   r[   r_   rf   r<   	FrozenSetrg   Mappingr;   rm   ri   r|   __all__r   r   r   r   <module>   s    






		

/
(

4
F
#