
    EEipJ              	          d dl Z d dlmZ d dlmZmZmZmZmZm	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mZ 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m Z 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, d dl-m.Z. d dl/m0Z1  G d de      Z2 G d de2      Z3dededeeeef   fdZ4dedefdZ5dedeeef   fdZ6dedefdZ7dedefdZ8	 ddede9defdZ:de;dee   fdZ<y)    N)partial)CallableListOptionalSequenceTuplecast)Literal)Type)message_registry)PluginFunctionContextMethodContextCheckerPluginInterface)ARG_POSARG_STARTypeInfoContextFuncDefStrExprIntExpr
Expression)r   CallableTypeNoneTyp
OverloadedTypeVarLikeTypeTypeVarTypeInstance	UnionTypeUninhabitedTypeAnyType	TypeOfAnyget_proper_typeget_proper_types)make_simplified_union)TypeChecker)parsec                   .    e Zd Zdedeeegef      fdZy)
TrioPluginfullnamereturnc                 \    |dk(  rt         S |dk(  rt        S |dk(  rt        S |dk(  rt        S y )N#trio_typing.takes_callable_and_argszasync_generator.async_generatorzasync_generator.yield_zasync_generator.yield_from_) takes_callable_and_args_callbackasync_generator_callbackyield_callbackyield_from_callback)selfr*   s     ^/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/trio_typing/plugin.pyget_function_hookzTrioPlugin.get_function_hook'   sC     <<3388++//!!44&&    N)	__name__
__module____qualname__strr   r   r   r   r4    r5   r3   r)   r)   &   s)    	(O,d23	4r5   r)   c                   :     e Zd Zdedeeegef      f fdZ xZ	S )TrioPlugin13r*   r+   c                 N    |dk(  rt        t        d      S t        |   |      S )Nr-   F)has_type_var_default)r   r.   superr4   )r2   r*   	__class__s     r3   r4   zTrioPlugin13.get_function_hook6   s-     <<;RWXXw(22r5   )
r6   r7   r8   r9   r   r   r   r   r4   __classcell__)r@   s   @r3   r<   r<   5   s/    33	(O,d23	43 3r5   r<   ctxoriginal_async_return_typer+   c                    g }t        |      }t        |t              r|j                  }n|g}d}d}g }	 |D ]  }t        |      }t        |t              r|j
                  j                  dk(  r@t        |j                        dk7  rt        d      |t        d      |j                  d   }w|j
                  j                  dk(  r@t        |j                        dk7  rt        d      |t        d	      |j                  d   }|j                  |       |j                  |        	 |5|3t        t         j$                        t        t         j$                        |fS |4t'        | j                  j(                  | j                  j*                        }|4t'        | j                  j(                  | j                  j*                        }|s9||t-        d| j                  j(                  | j                  j*                        fS ||t/        || j                  j(                  | j                  j*                        fS # t        $ rr}	| j                  j                  d
j                  |	      | j                         t        t         j"                        t        t         j"                        |fcY d}	~	S d}	~	ww xY w)a  Return the yield type, send type, and return type of
    an @async_generator decorated function that was
    originally declared to return ``original_async_return_type``.

    This tries to interpret ``original_async_return_type`` as a union
    between the async generator return type (i.e., the thing actually
    returned by the decorated function, which becomes the value
    associated with a ``StopAsyncIteration`` exception), an optional
    ``trio_typing.YieldType[X]`` where ``X`` is the type of values
    that the async generator yields, and an optional
    ``trio_typing.SendType[Y]`` where ``Y`` is the type of values that
    the async generator expects to be sent. If one of ``YieldType``
    and ``SendType`` is specified, the other is assumed to be None;
    if neither is specified, both are assumed to be Any.
    If ``original_async_return_type`` includes a ``YieldType``
    and/or a ``SendType`` but no actual return type, the return
    is inferred as ``NoReturn``.
    Nztrio_typing.YieldType   z YieldType must take one argumentz"YieldType specified multiple timesr   ztrio_typing.SendTypezSendType must take one argumentz!SendType specified multiple timesz(invalid @async_generator return type: {}T)is_noreturnlinecolumn)r#   
isinstancer   itemsr   typer*   lenargs
ValueErrorappendapifailformatcontextr!   r"   
from_errorunannotatedr   rG   rH   r    r%   )
rB   rC   armsresolved_async_return_type
yield_type	send_type
other_armsorig_armarmexs
             r3   "decode_agen_types_from_return_typer^   ?   sk   , D!01K!L,i8)//*+JIJ
 	,H!(+C#x(88$$(??388})()KLL!-()MNN!$!JXX&&*@@388})()JKK ,()LMM #I%%h/!!(+%	,6 i/I))*I))*&
 	
 S[[--s{{/A/AB
CKK,,ckk.@.@A	 s{{'7'7@R@R
 	
 !*ckk.>.>@R@RS
 	
9  
?FFrJCKKXI(()I(()&
 	

s   C7I 	J>A'J93J>9J>c                 r   | j                   }t        |t              s|S t        |j                        }t        |t
              rs|j                  j                  dk(  rZt        |j                        dk(  rB|j                  |j                  t        t        | |j                  d                           S |S )aP  Handle @async_generator.

    This moves the yield type and send type declarations from
    the return type of the decorated function to the appropriate
    type parameters of ``trio_typing.CompatAsyncGenerator``.
    That is, if you say::

        @async_generator
        async def example() -> Union[str, YieldType[bool], SendType[int]]:
            ...

    then the decorated ``example()`` will return values of type
    ``CompatAsyncGenerator[bool, int, str]``, as opposed to
    ``CompatAsyncGenerator[Any, Any, Union[str,
    YieldType[bool], SendType[int]]`` without the plugin.
     trio_typing.CompatAsyncGenerator      )rM   )ret_type)default_return_typerI   r   r#   rc   r   rK   r*   rL   rM   copy_modifiedlistr^   )rB   decorator_return_typeagen_return_types      r3   r/   r/      s    $  33+\:$$&'<'E'EF#X.!!**.PP %%&!+$22%336s<L<Q<QRS<TU 4  3 
 	
 ! r5   c                 j   t        t        | j                        }|j                  j	                         }|(t        |t              r|j                  r|j                  sX| j                  j                  d| j                         t        t        j                        t        t        j                        fS t        |j                  t              rt        |j                  j                   t"              r|j                  j                   j                  j$                  dk(  rat'        |j                  j                   j(                        dk(  r5t+        | |j                  j                   j(                  d         \  }}}||fS t        t        j,                        t        t        j,                        fS )zReturn the yield and send types that would be returned by
    decode_agen_types_from_return_type() for the function that's
    currently being typechecked, i.e., the function that contains the
    call described in ``ctx``.
    z9async_generator.yield_() outside an @async_generator funcztyping.Coroutinera   rb   )r	   r&   rP   scopetop_functionrI   r   is_coroutineis_decoratedrQ   rS   r!   r"   rT   rK   r   rc   r   r*   rL   rM   r^   implementation_artifact)rB   private_apienclosing_funcrX   rY   _s         r3   decode_enclosing_agen_typesrr      sR    {CGG,K &&335N.'2**** 	G	
 y++,gi6J6J.KKK 	>&&5~**33X>((--66:LL##,,112a7#E$$--2215$
 
Iq 9$$ 		112	112 r5   c                 0   t        | j                        dk(  r5t        | j                  j                  | j                  j
                        }nF| j                  r.t        | j                  d         dk(  r| j                  d   d   }n| j                  S t        t        | j                        }t        |       \  }}|N|L|j                  ||| j                  t        j                  dd       | j                  j                  d|g      S | j                  S )zbProvide a more specific argument and return type for yield_()
    inside an @async_generator.
    r   rE   zyield_ argumentzdeclared YieldTypesubtype	supertyperS   msgsubtype_labelsupertype_labelztyping.Awaitable)rL   	arg_typesr   rS   rG   rH   rd   r	   r&   rP   rr   check_subtyper   INCOMPATIBLE_TYPESnamed_generic_type)rB   arg_typero   rX   rY   s        r3   r0   r0      s     3==Q3;;++S[[-?-?@	3s}}Q/0A5==#A&&&&{CGG,K7<J	)"7!! KK 33+0 	" 	
 ww))*<ykJJ"""r5   c                 T   | j                   r7t        | j                   d         dk(  rt        | j                   d   d         }n| j                  S t	        t
        | j                        }t        |       \  }}||| j                  S t        |t              r|j                  j                  dv rt        |j                        dk\  r||j                  dd \  }}|j                  ||| j                  t        j                   dd       |j                  ||| j                  t        j                   d	d
       | j                  S t        |t              rJ|j                  || j                  j#                  d|g      | j                  t        j                   dd       | j                  S )z-Provide a better typecheck for yield_from_().r   rE   N)r`   ztrio_typing.AsyncGeneratorztyping.AsyncGeneratorrb   zyield_from_ argument YieldTypezlocal declared YieldTypert   zlocal declared SendTypezyield_from_ argument SendTypeztyping.AsyncIterablezyield_from_ argument typezexpected iterable type)rz   rL   r#   rd   r	   r&   rP   rr   rI   r   rK   r*   rM   r{   rS   r   r|   r}   )rB   r~   ro   our_yield_typeour_send_typetheir_yield_typetheir_send_types          r3   r1   r1      s   
}}S]]1-.!3"3==#3A#67&&&{CGG,K$?$D!NM!6&&& 	8X&MM""

 !#,4MM"1,=)/!!$$KK 33:6 	" 	
 	!!!%KK 333; 	" 	
( """ 
Hh	'!!gg00&(8 KK 3354 	" 		
 """r5   r>   c                 
   	 | j                   rt        | j                   d         dk7  rt        d      t        | j                   d   d         }t	        |t
              r#t	        t        | j                        t
              st        d      d}d}d}d}t        t        |j                  |j                               D ]1  \  }\  }}	t        |	      }	t	        |	t              r|t        k(  r
|dk(  sJ |}8t	        |	t        t
        f      sO|t        k(  sYt	        |	t              rYt        |	j                        D ]@  }
t	        |
t
              s|
j                   r!t#        d |
j                  D              s>|
}	 n t        t        |	j                  |	j                               D ]D  \  }\  }}t        |      }t	        |t              s%|t        k(  s/|dk7  rt        d      |}|}|	}F 4 |dk(  rt        d      |dk(  s|t        d	      g }g }t%        dd
      D ]  }t'        |j                         }|j)                  |j                   d| t+        t,        t.           |      z   |j                   |dz   d z   |j                  d| t        gt        |      z  z   |j                  |dz   d z   |j0                  d| dgt        |      z  z   |j0                  |dz   d z         ||<   |j3                  |j)                  |d| t+        t,        t.           |      z   ||dz   d z   |j                  d| t        gt        |      z  z   |j                  |dz   d z   |j0                  d| dgt        |      z  z   |j0                  |dz   d z   t'        |j4                        t+        t,        t6           |      z                |r|j3                  t9        dj;                  |      dj;                  |      t        |j4                         |z
  dz
  g | j<                  j?                  dg       | j@                  jB                  | j@                  jD                  t        tF        jH                                     Z|j3                  t9        dj;                  |      dj;                  |      t        |j4                         |z
  dz
  g | j<                  j?                  dg       | j@                  jB                  | j@                  jD                                tK        |      S # t        $ rK}| j<                  jM                  dj;                  |      | j@                         | j                  cY d}~S d}~ww xY w)a  Automate the boilerplate for writing functions that accept
    arbitrary positional arguments of the same type accepted by
    a callable.

    For example, this supports writing::

        @trio_typing.takes_callable_and_args
        def start_soon(
            self,
            async_fn: Callable[[VarArg()], Any],
            *args: Any,
        ) -> None: ...

    instead of::

        T1 = TypeVar("T1")
        T2 = TypeVar("T2")
        T3 = TypeVar("T3")
        T4 = TypeVar("T4")

        @overload
        def start_soon(
            self,
            async_fn: Callable[[], Any],
        ) -> None: ...

        @overload
        def start_soon(
            self,
            async_fn: Callable[[T1], Any],
            __arg1: T1,
        ) -> None: ...

        @overload
        def start_soon(
            self,
            async_fn: Callable[[T1, T2], Any],
            __arg1: T1,
            __arg2: T2
        ) -> None: ...

        # etc

    r   rE   zmust be used as a decoratorNc              3   .   K   | ]  }|t         k(    y w)N)r   ).0kind_s     r3   	<genexpr>z3takes_callable_and_args_callback.<locals>.<genexpr>  s     #Q%EX$5#Qs   zSdecorated function may only take one callable that has an argument of type VarArg()z'decorated function must take *args: Anyz\decorated function must take a callable that has a argument of type mypy_extensions.VarArg()   )rz   	arg_kinds	arg_names)rz   r   r   	variablesz__T{}zbuiltins.object)rG   rH   default)rG   rH   z+invalid use of @takes_callable_and_args: {})'rz   rL   rN   r#   rI   r   rd   	enumeratezipr   r!   r   r   r   r$   rJ   is_ellipsis_argsanyrangerf   re   r	   r   r   r   rO   r   r   r   rR   rP   r}   rS   rG   rH   r"   from_omitted_genericsr   rQ   )rB   r>   fn_typecallable_idxcallable_args_idxcallable_tyargs_idxidxkindtyr\   idx_r   ty_expanded_fnstype_var_typesarg_idxrz   r]   s                      r3   r.   r.   5  sh   ^J'}}CMM!$4 5 ::;;!#--"21"56'<0
C334l9
 :;;(W->->@Q@Q)RS 	)OC$ $B"g&48+;2~%~BL 9:tw b),/9 	!&sL9$'$8$8 ##Q3==#Q Q!$B!	! !*3Cbll4S*T 
)&D,5#)#.C!#w/EX4E'2-",!H#  (+,0)&(
)+	)B r>FGG2!4< 
 Q{ H	GW../I&1&?&?))*<+<=4:~67!++,=,A,CDE
  ))*<+<=y3~#668!++,=,A,CDE
  ))*<+<=vN 335!++,=,A,CDE '@ 'Il#, %%!)8,tDz>:;#HqLN34
  )))84#9s>'::<!++HqLN;<
  )))84 6C$779!++HqLN;<
 W../tO4nEF# & 0 $%%w/w/W..//'9A=223DbI [[--"{{11 '	(G(G H	 %%w/w/W..//'9A=223DbI [[--"{{11
}H	T ,'' '9@@Dckk	
 &&&	'sK   D
T( 	T( 8T( T( T( :AT( 	T( MT( (	U<1A U71U<7U<versionc                 ~    t        |       }|t        d      k  rt        d      |t        d      k  rt        S t        S )Nz1.0z7This version of trio-typing requires at least mypy 1.0.z1.4)parse_versionRuntimeErrorr<   r)   )r   mypy_versions     r3   pluginr     s>     )LmE**TUU	e,	,r5   )T)=sys	functoolsr   typingr   r   r   r   r   r	   typing_extensionsr
   r   typing_Typemypyr   mypy.pluginr   r   r   r   
mypy.nodesr   r   r   r   r   r   r   r   
mypy.typesr   r   r   r   r   r   r   r    r!   r"   r#   r$   mypy.typeopsr%   mypy.checkerr&   packaging.versionr'   r   r)   r<   r^   r/   rr   r0   r1   boolr.   r9   r   r:   r5   r3   <module>r      s+   
  B B % & ! V V	 	 	    / $ 4 3: 3S
	S
6:S

4tS
l"!/ "!d "!J'_ 'tTz9J 'T# #D #63#_ 3# 3#n 8<y'	y'04y'	y'xC K/ r5   