
    e?                    :   U d Z ddlmZ ddlZddl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 ddlmZ dd	lmZmZ dd
lmZmZ ddlmZmZmZmZ ddlmZ ddlmZ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* ddhZ+h dZ,de-d<   ddhZ.de-d<   dZ/ e0 e1edg             Z2 e3d      Z4dZ5d+dZ6	 d,	 	 	 	 	 d-dZ7d+dZ8	 d,	 	 	 	 	 d.dZ9	 	 	 	 d/dZ:	 d,	 	 	 	 	 d0d Z;	 d,	 	 	 	 	 d1d!Z<d2d"Z=d3d#Z>	 d,	 	 	 	 	 d4d$Z?d2d%Z@	 d,	 	 	 	 	 d4d&ZAd2d'ZB	 d,	 	 	 	 	 d5d(ZCd) ZDd6d*ZEy)7z$Astroid hooks for typing.py support.    )annotationsN)Iterator)partial)Final)contextextract_nodeinference_tip)register_module_extender)AstroidBuilder_extract_single_node)	PY39_PLUS
PY312_PLUS)AstroidSyntaxErrorAttributeInferenceErrorInferenceErrorUseInferenceDefault)AstroidManager)
Assign
AssignName	AttributeCallConst	JoinedStrNameNodeNG	SubscriptTuple)ClassDefFunctionDefTypeVarNewType>   typing.NewTypetyping.TypeVartyping_extensions.TypeVarr   TYPING_TYPEVARS_QUALIFIEDztyping.TypedDictztyping_extensions.TypedDictTYPING_TYPEDDICT_QUALIFIEDz
class Meta(type):
    def __getitem__(self, item):
        return self

    @property
    def __args__(self):
        return ()

class {0}(metaclass=Meta):
    pass
__all__)(ztyping.Hashableztyping.Awaitableztyping.Coroutineztyping.AsyncIterableztyping.AsyncIteratorztyping.Iterableztyping.Iteratorztyping.Reversibleztyping.Sizedztyping.Containerztyping.Collectionztyping.Callableztyping.AbstractSetztyping.MutableSetztyping.Mappingztyping.MutableMappingztyping.Sequenceztyping.MutableSequenceztyping.ByteStringztyping.Tupleztyping.Listztyping.Dequez
typing.Setztyping.FrozenSetztyping.MappingViewztyping.KeysViewztyping.ItemsViewztyping.ValuesViewztyping.ContextManagerztyping.AsyncContextManagerztyping.Dictztyping.DefaultDictztyping.OrderedDictztyping.Counterztyping.ChainMapztyping.Generatorztyping.AsyncGeneratorztyping.Typeztyping.Patternztyping.Matchz?
@classmethod
def __class_getitem__(cls, item):
    return cls
c                    | j                   }t        |t              r|j                  t        v S t        |t
              r|j                  t        v S y)NF)func
isinstancer   attrnameTYPING_TYPEVARSr   name)noder)   s     </usr/lib/python3/dist-packages/astroid/brain/brain_typing.py$looks_like_typing_typevar_or_newtyper0   q   s@    99D$	"}}//$yyO++    c                   	 t        | j                  j                  |            }|j                         t        vrt
        | j                  st
        t        | j                  d   t              rt
        | j                  d   j                         j                  d      }	 t        t        j                  |            } | j                  |      S # t        t        f$ r}t
        |d}~ww xY w# t         $ r}t        |d}~ww xY w)z8Infer a typing.TypeVar(...) or typing.NewType(...) call.r   Nr   ')nextr)   inferr   StopIterationr   qnamer%   argsr*   r   	as_stringstripr   TYPING_TYPE_TEMPLATEformatr   )r.   context_ittonr)   exctypenames        r/   infer_typing_typevar_or_newtyperA   z   s    +DIIOOMO:; zz|44!!99!!$))A,	*!!yy|%%'--c2H&077AB ::m:,,! M* +!s*+  &#%&s/   %C %C3 C0$C++C03	D<DDc                    t        | t              r| j                  t        v S t        | t              r| j
                  t        v S t        | t              rt        | j                        S y)zLTry to figure out if a Subscript node *might* be a typing-related subscript.F)	r*   r   r-   TYPING_MEMBERSr   r+   r   _looks_like_typing_subscriptvaluer.   s    r/   rD   rD      sP    $yyN**$	"}}..$	"+DJJ77r1   c                   	 t        | j                  j                               j                         j                  d      rj                         t        v rt
        t        t              rj                         dv rt        t              }|gj                  d<   t        | j                  t              r| | j                  j                  v rit!        | j                  dd      rR| j                  j"                  }|j%                  | j                  j&                        || j                  j&                  = fd| _        t+        g      S t-        t.        j1                  j                         j3                  d      d               } | j                  |	      S # t        t        f$ r}t
        |d}~ww xY w)
z Infer a typing.X[...] subscript.Nztyping.>   typing.Generictyping.Annotatedtyping_extensions.Annotated__class_getitem____cachec                    t        g      S Niter)r.   r   rE   s     r/   <lambda>z#infer_typing_attr.<locals>.<lambda>   s    ug r1   .r3   )r5   rE   r6   r   r7   r   r8   
startswithTYPING_ALIASr*   r   r   CLASS_GETITEM_TEMPLATElocalsparentbasesgetattrrL   getslots_explicit_inferencerP   r   r<   r=   split)r.   ctxr?   func_to_addcacherE   s        @r/   infer_typing_attrrb      s_   +TZZ%%'( ;;=##I.%++-<2O!!%"u{{} 9 ( ++AB-8M()t{{H-)))Y5 KK''Eyy**+7$++++,#F UG},33EKKM4G4G4LR4PQRD::c:""A M* +!s*+s   #F# #F>2F99F>c                .    | j                         t        v S )z'Check if node is TypedDict FunctionDef.)r8   r&   rF   s    r/   _looks_like_typedDictrd      s     ::<555r1   c                P    t        d      }|g| j                  d<   t        | g      S )Ndict__call__)r   rW   rP   )r.   r_   r`   s      r/   infer_old_typedDictrh      s*     'v.K*mDKK
<r1   c                   t        d| j                  | j                  | j                  | j                  | j
                        }|j                  t        d      gg d       t        d      }|g|j                  d<   t        |g      S )z,Replace TypedDict FunctionDef with ClassDef.	TypedDictr-   lineno
col_offsetrX   
end_linenoend_col_offsetrf   NrY   body
decoratorsrg   )r   rl   rm   rX   rn   ro   postinitr   r   rW   rP   )r.   r_   	class_defr`   s       r/   infer_typedDictru      s~     {{??{{??**I l623"N&v.K$/=IZ r1   c                    t        | j                  t              xr= | j                  j                  dv xr# t        | j                  d   t
        t        f      S )z
    Returns True if the node corresponds to a call to _alias function.

    For example :

    MutableSet = _alias(collections.abc.MutableSet, T)

    :param node: call node
    >   _alias_DeprecatedGenericAliasr   )r*   r)   r   r-   r9   r   rF   s    r/   _looks_like_typing_aliasry      sK     	499d# 	
IINNCC	

 tyy|i%67r1   c                    d }	 | j                  d       t        || j                         }|| _         y# t        $ r Y yw xY w)zJDisable the access to __class_getitem__ method for the node in parameters.c                :    |dk(  rt        d       | |g|i |S )z
        Raises an AttributeInferenceError in case of access to __class_getitem__ method.
        Otherwise, just call origin_func.
        rK   z'__class_getitem__ access is not allowed)r   )origin_funcattrr9   kwargss       r/   full_raiserz1_forbid_class_getitem_access.<locals>.full_raiser   s.    
 &&)*STT41$1&11r1   rK   N)rZ   r   r   )r.   r   partial_raisers      r/   _forbid_class_getitem_accessr      sD    2	()
 !dll;%" s   .4 	A A c                   t        | j                  t              rIt        | j                  j                        dk(  r't        | j                  j                  d   t
              st        	 t        | j                  d   j                  |            }| j                  j                  d   }t        |j                  |j                  |j                  | j                  |j                   |j"                        t        |t              rj%                  |gg d       | j                  d   }t&        st        |t(              r1|j*                  s%t&        r?t        |t,              r/|j.                  dkD  r t1        t2              }|gj4                  d<   nt7               fd	| _        t;        g      S # t        $ r }t        | j                  d   |      |d}~ww xY w)
a
  
    Infers the call to _alias function
    Insert ClassDef, with same name as aliased class,
    in mro to simulate _GenericAlias.

    :param node: call node
    :param context: inference context

    # TODO: evaluate if still necessary when Py3.12 is minimum
       r   r3   r.   r   Nrk   rp   rK   c                    t        g      S rN   rO   r.   r   rt   s     r/   rQ   z$infer_typing_alias.<locals>.<lambda>N      T9+5F r1   )r*   rX   r   lentargetsr   r   r5   r9   r6   r7   r   r   r-   rl   rm   rn   ro   rs   r   r   eltsr   rE   r   rV   rW   r   r]   rP   )r.   r_   reseassign_namemaybe_type_varr`   rt   s          @r/   infer_typing_aliasr     s    t{{F+4;;&&'1,$++--a0*=!!D499Q<%%c%23 ++%%a(K!!)){{))"11I #x  	#RDAYYq\NNE2>;N;N~u-  1$ ++AB1<	,-
 	%Y/  GDK  D$))A,<!CDs   ,(F6 6	G?GGc                   t        | j                  t              xr\ t         xr | j                  j                  dk(  xr| t        | j
                  d   t              xr | j
                  d   j                  dk(  xs? t        | j
                  d   t              xr  | j
                  d   j                         dk(  xs t        xr | j                  j                  dk(  xr; t        | j
                  d   t              xr | j
                  d   j                  dk(  xsZ | j                  j                  dk(  xr? t        | j
                  d   t              xr  | j
                  d   j                         dk(  S )a  Return True if call is for Tuple or Callable alias.

    In PY37 and PY38 the call is to '_VariadicGenericAlias' with 'tuple' as
    first argument. In PY39+ it is replaced by a call to '_TupleType'.

    PY37: Tuple = _VariadicGenericAlias(tuple, (), inst=False, special=True)
    PY39: Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')

    PY37: Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
    PY39: Callable = _CallableType(collections.abc.Callable, 2)
    _VariadicGenericAliasr   tuplezcollections.abc.Callable
_TupleType_CallableType)r*   r)   r   r   r-   r9   r   r:   rF   s    r/   _looks_like_special_aliasr   R  sD    dii&  	
IINN55	
 tyy|T* -		!!!W,G$))A,	2 G		!&&(,FF	
  
IINNl* -499Q<.-		!!!W,G yy~~0 G499Q<3G		!&&(,FF!r1   c                   t        | j                  t              rIt        | j                  j                        dk(  r't        | j                  j                  d   t
              st        	 t        | j                  d   j                  |            }| j                  j                  d   }t        |j                  | j                  |j                  |j                  |j                   |j"                        j%                  |gg d       t'        t(              }|gj*                  d<   fd	| _        t/        g      S # t        $ r }t        | j                  d   |      |d}~ww xY w)
zBInfer call to tuple alias as new subscriptable class typing.Tuple.r   r   r3   r   N)r-   rX   rl   rm   rn   ro   rp   rK   c                    t        g      S rN   rO   r   s     r/   rQ   z%infer_special_alias.<locals>.<lambda>  r   r1   )r*   rX   r   r   r   r   r   r5   r9   r6   r7   r   r   r-   rl   rm   rn   ro   rs   r   rV   rW   r]   rP   )r.   r_   r   r   r   r`   rt   s         @r/   infer_special_aliasr   s  s:   
 	4;;'##$)t{{**1-z:!!D499Q<%%c%23 ++%%a(K{{!!))))"11I cU=&'=>K-8MI()FD#  D$))A,<!CDs   ,(E 	E-E((E-c                    t        | t              xrl t        | j                  t              xr | j                  j                  dk(  xs5 t        | j                  t
              xr | j                  j                  dk(  S )Ncast)r*   r   r)   r   r-   r   r+   rF   s    r/   _looks_like_typing_castr     sc    dD! 499d# 	%IINNf$	)dii+ )II&(	r1   c                   t        | j                  t        t        f      st        	 t        | j                  j                  |            }t        |t              r+|j                         dk7  st        | j                        dk7  rt        | j                  d   j                  |      S # t        t        f$ r}t        |d}~ww xY w)z<Infer call to cast() returning same type as casted-from var.r3   Nztyping.cast   r   )r*   r)   r   r   r   r5   r6   r   r7   r   r8   r   r9   )r.   r_   r)   r?   s       r/   infer_typing_castr     s     dii$	!23!!+DIIOOCO01 t[)::<=(tyy>Q!!99Q<c** M* +!s*+s   %B- -C<CCc                 l    t        t                     j                  t        j                  d            S )Na  
    class Generic:
        @classmethod
        def __class_getitem__(cls, item):  return cls
    class ParamSpec: ...
    class ParamSpecArgs: ...
    class ParamSpecKwargs: ...
    class TypeAlias: ...
    class Type:
        @classmethod
        def __class_getitem__(cls, item):  return cls
    class TypeVar:
        @classmethod
        def __class_getitem__(cls, item):  return cls
    class TypeVarTuple: ...
    )r   r   string_buildtextwrapdedent r1   r/   _typing_transformr     s.    .*+88	
 r1   c                r   | j                  t        t        t              t               | j                  t
        t        t              t               | j                  t        t        t              t               t        r)| j                  t        t        t              t               n(| j                  t        t        t              t               | j                  t        t        t               t"               | j                  t        t        t$              t&               t(        rt+        | dt,               y y )Ntyping)register_transformr   r	   rA   r0   r   rb   rD   r   r   r   r   ru   rd   r   rh   r   ry   r   r   r   r
   r   )managers    r/   registerr     s    56,
 =!235Q m-.0G ""79N	
 	""m$78:O	
 m./1I m/02K  (4EF r1   )returnboolrN   )r.   r   r>   context.InferenceContext | Noner   Iterator[ClassDef])r.   r   r_   r   r   r   )r.   zFunctionDef | ClassDefr   r   )r.   r   r_   r   r   r   )r.   r   r_   r   r   r   )r.   r   r   r   )r.   r   r   None)r.   r   r_   r   r   r   )r.   r   r_   r   r   zIterator[NodeNG])r   r   r   r   )F__doc__
__future__r   r   r   collections.abcr   	functoolsr   r   astroidr   r   r	   astroid.brain.helpersr
   astroid.builderr   r   astroid.constr   r   astroid.exceptionsr   r   r   r   astroid.managerr   astroid.nodes.node_classesr   r   r   r   r   r   r   r   r   r   astroid.nodes.scoped_nodesr   r   r,   r%   __annotations__r&   r<   setrZ   rC   	frozensetrU   rV   r0   rA   rD   rb   rd   rh   ru   ry   r   r   r   r   r   r   r   r   r   r1   r/   <module>r      s  
 + "   $   8 8 : @ /  +   =i($ 5 
 &89V$W E W  WVY34)+Z  BF-
->--2 =A&#
&#9&#&#R6
 6	6 <@
8 ?C
;$*2 8<:
:4::zD 8<
4@ 8<+
+4++*.Gr1   