a
    ö£BeÖ7  ã                   @   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ƒrDeedƒZ	needƒrZeedƒZ	ne
ƒ ‚eedƒrveedƒZndZeedƒr�eedƒZndZeedƒrªe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�rPe)e*d	œd
d„Z+ne)e*d	œdd„Z+ej,e* dœdd„Z-ej,e* dœ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 Z6e'e'dœdd„Z7dd„ Z8ejdœdd„Z9d2dd„Z:G dd„ dej;ƒZ<e6d œd!d"„Z=e'e<dœd#d$„Z>e4e4e*d%œd&d'„Z?ej2ejej@d(f ejAe< e<f ZBd)d)ej,ejCeDef  ej,e* d*œd+d,„ZEe<e<ej,ejCeDef  ej,e* d*œd-d.„ZFd3e'e'ej,e ej,e* d*œd/d0„ZGg d1¢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Ú
ForwardRefZ_ForwardRefÚLiteralÚ_TypedDictMetaÚ	UnionTypeÚSTATIC_SUBTYPE_MAPPING)ÚelementÚreturnc                 C   s   | t ju p| tu S ©N)ÚtypingÚUnionr   ©r   © r   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/overrides/typing_utils.pyÚis_unionB   s    r   c                 C   s
   | t ju S r	   )r
   r   r   r   r   r   r   F   s    ©r   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>M   ó    zoptional_all.<locals>.<genexpr>F)ÚallÚunknown©Úelementsr   r   r   Úoptional_allJ   s
    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   U   r   zoptional_any.<locals>.<genexpr>F)Úanyr   r   r   r   r   Úoptional_anyR   s
    r   c                 C   s&   zt | ƒ W n ty    Y dS 0 dS )z(Determine whether `value` can be hashed.FT)ÚhashÚ	TypeError)Úvaluer   r   r   Ú	_hashableZ   s
    r    ztyping.TypeVar)Útype_r   c                 C   s4   t | tjƒr| S t| ƒs J dƒ‚| tv r0t|  S | S )Nz9_normalize_aliases should only be called on element types)Ú
isinstancer
   ÚTypeVarr    ÚBUILTINS_MAPPING)r!   r   r   r   Ú_normalize_aliasesm   s    r%   c                 C   sØ   t tdƒrttdƒ}|| ƒ}n˜t tjdƒrpt| tƒrB| jsB| j}q¶t | dƒrX| jrX| }q¶| tju rjtj}q¶d}nFt| tƒrŽ| j}|du r¶| }n(t| t	ƒr | j}n| tju r²tj}nd}|du rÐt
rÐt| t
ƒrÐt}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!   Ú_getterZorir   r   r   r&   x   s.    






r&   c                 C   sò   t tdƒrttdƒ}|| ƒ}n¬t tjdƒr|t| tƒrv| jsv| j}t| ƒt	j
ju rz|d turzt|dd… ƒ|d f}qÊd}nNt| ttfƒrÆ| j}t| ƒt	j
ju rÊ|d turÊt|dd… ƒ|d f}nd}trât| tƒrâttjfS |du rîd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   Néÿÿÿÿr   )r(   r
   r)   r*   r"   r+   r'   Ú__args__r&   ÚcollectionsÚabcÚCallableÚEllipsisÚlistr.   r   ÚstrÚAny)r!   r0   Úresr   r   r   r1   §   s(    


ÿÿ
r1   c                 C   sT   |pi }t tdƒr*ttdƒ}|| tƒ |ƒS t | dƒrJt| dƒ}|tƒ |ƒS tƒ ‚dS )z3
    eval forward_refs in all cPython versions
    Ú
_eval_typeN)r(   r
   r)   ÚglobalsÚNotImplementedError)ÚrefÚforward_refsÚlocalnsr<   r   r   r   Úeval_forward_refÏ   s    



rB   c                   @   sP   e Zd ZU dZeed< eƒ Zej	ee
f ed< dd„ Zedœ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ƒrb| j|jkrdS t | jtƒrJt |jtƒrJ| j|jkoH|j| jkS | j|jko`| j|jkS | jsr| j|kS dS )NF)r"   rC   rD   rE   Ú	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	   )rE   r   rD   ©rG   r   r   r   Ú__hash__ó   s    
zNormalizedType.__hash__c                 C   s"   | j s| j› S | j› d| j › d�S )Nú[z]))rE   rD   rJ   r   r   r   Ú__repr__ø   s    zNormalizedType.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚTypeÚ__annotations__ÚtuplerE   r
   r   rF   rI   ÚintrK   rM   r   r   r   r   rC   à   s   
rC   ©Ztpsc                 C   sV   t | tƒr| S t | tjjƒr.tdd„ | D ƒƒS t | tjjƒrNtdd„ | D ƒƒS t| ƒS )Nc                 s   s   | ]}t |ƒV  qd S r	   ©Ú_normalize_args©r   r!   r   r   r   r     r   z"_normalize_args.<locals>.<genexpr>c                 s   s   | ]}t |ƒV  qd S r	   rW   rY   r   r   r   r     r   )	r"   r9   r4   r5   ÚSequencerT   ÚSetrF   Ú	normalizerV   r   r   r   rX   þ   s    
rX   c                 C   sP   t | ƒ}t| ƒ}|s tt| ƒƒS t|ƒ}t|ƒr>tt|ƒƒ}nt|ƒ}t||ƒS )z4
    convert types to NormalizedType instances.
    )r1   r&   rC   r%   r   rX   rF   )r!   rE   rD   r   r   r   r\     s    r\   )ÚleftÚrightr   c                 C   sx   | |u rdS | d ur,| t v r,|t |  kr,dS t| dƒrR|  ¡ D ]}||kr> dS q>t| tƒrpt|tƒrpt| |ƒS | |kS )NTÚmro)r   r(   r_   r"   ÚtypeÚ
issubclass)r]   r^   Úparentr   r   r   Ú_is_origin_subtype  s     ÿþ
ý

rc   .ÚNormalizedTypeArgs)r]   r^   r@   r   c                    s
  t | tƒr@t ˆtƒsdS | ˆ }|s(dS t‡ ‡fdd„|D ƒƒS t | tjjƒrât | tƒsât ˆtjjƒrpt ˆtƒrtdS | r°| d jtur°ˆr°ˆd jtu r°t‡ ‡fdd„| D ƒƒS t	| ƒt	ˆƒkrÄdS t‡ fdd„t
 | ˆ¡D ƒƒS t | tƒsðJ ‚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   r@   r   r   r   D  r   z4_is_origin_subtype_args.<locals>.<genexpr>.<genexpr>N)r   )r   ©r@   r^   )r   r   r   C  s   z*_is_origin_subtype_args.<locals>.<genexpr>r2   c                 3   s   | ]}t |ˆd  ˆ ƒV  qdS )r   N©Ú_is_origin_subtype_args)r   Úlrh   r   r   r   V  r   c                 3   s.   | ]&\}}|d uo$|d uo$t ||ˆ ƒV  qd S r	   ri   )r   rk   rg   ©r@   r   r   r   [  s
   ýÿ)r"   rF   r   r4   r5   rZ   rC   rD   r7   ÚlenÚ	itertoolsÚzip_longestrf   )r]   r^   r@   Úexcludedr   rh   r   rj   4  sB    

ÿÿÿÿþýü
ürj   c                    s  t ˆjtƒrttˆjˆ d�ƒ‰t ˆjtƒr<ttˆjˆ d�ƒ‰ˆjtju rLdS tˆjƒrptˆjƒrptˆj	ˆj	ˆ ƒS tˆjƒr”t
‡ ‡fdd„ˆj	D ƒƒS tˆjƒr¸t‡ ‡fdd„ˆj	D ƒƒS ˆjtu ræˆjturÐdS tˆj	ƒ tˆj	ƒ¡S t ˆjtjƒ�r`t ˆjtjƒ�r`ˆjˆju �rdS tˆjdd ƒ}tˆjdd ƒ}|d u �sH|d u �rLtS tt|ƒt|ƒˆ ƒS t ˆjtjƒ�rttS t ˆjtjƒ�r°tˆjdd ƒ}|d u �r tS tt|ƒˆˆ ƒS ˆj	�sÎˆj	�sÎtˆjˆjƒS ˆj	�sätˆjˆjƒS tˆjˆjƒ�rtˆj	ˆj	ˆ ƒS dS )Nrl   Tc                 3   s   | ]}t ˆ|ˆ ƒV  qd S r	   re   ©r   Úa)r@   r]   r   r   r   {  s   z%_is_normal_subtype.<locals>.<genexpr>c                 3   s   | ]}t |ˆˆ ƒV  qd S r	   re   rq   rh   r   r   r     s   FÚ	__bound__)r"   rD   r   r\   rB   r
   r:   r   rj   rE   r   r   r   ÚsetÚissubsetr#   r)   r   rf   rc   )r]   r^   r@   Z
left_boundZright_boundr   )r@   r]   r^   r   rf   h  s\    
ÿ
ÿ

ÿÿ
rf   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
    ```
    )rf   r\   )r]   r^   r@   r   r   r   Ú	issubtype«  s    rv   )rv   r&   r1   Úget_type_hints)N)N)IrQ   Úcollections.abcr4   Úiorn   Útypesr
   r(   r)   r   r>   r   r   r   r   r*   r8   r[   rt   ÚDictr/   ÚTuplerT   Ú
ByteStringÚbytesr6   r5   rZ   r`   r$   ÚTextIOWrapperÚTextIOÚ
TextIOBaseÚStringIOÚBufferedReaderÚBinaryIOÚBufferedWriterÚBytesIOr   rR   rS   ÚobjectÚboolr   ÚOptionalr   r   r    rw   r+   r   r.   Z
OriginTypeÚAbstractSetZTypeArgsr%   r&   r1   rB   Ú
NamedTuplerC   rX   r\   rc   r:   Ú	FrozenSetrd   ÚMappingr9   rj   rf   rv   Ú__all__r   r   r   r   Ú<module>   s–   






øú		

/(

ÿü5üD ÿþ!