o
    i,                  
   @   sV  d dl Z d dl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mZ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mZmZ dZdZdZ de!fddZ"G dd deZ#dedefddZ$dedefddZ%dededededef
ddZ&dededededef
d d!Z'ed"d#d$Z(ed%d&d$Z)G d'd( d(eZ*dS ))    N)DictList)TypeChecker)	ErrorCode)Errors)CallExpr	Decorator
ExpressionFuncDefMypyFileNameExpr)Options)CheckerPluginInterfaceFunctionContextPlugin)
is_subtype)CallableTypeInstanceTypeTypeVarTypezthinc.model.Modelzthinc.types.XY_YZ_OutTzthinc.types.XY_XY_OutTversionc                 C      t S N)ThincPlugin)r    r   >/home/ubuntu/.local/lib/python3.10/site-packages/thinc/mypy.pyplugin      r   c                       s4   e Zd Zdeddf fddZdefddZ  ZS )	r   optionsreturnNc                    s   t  | d S r   )super__init__)selfr   	__class__r   r   r!      s   zThincPlugin.__init__fullnamec                 C   r   r   )function_hook)r"   r%   r   r   r   get_function_hook   r   zThincPlugin.get_function_hook)__name__
__module____qualname__r   r!   strr'   __classcell__r   r   r#   r   r      s    r   ctxr   c                 C   s$   zt | W S  ty   | j Y S w r   )get_reducers_typeAssertionErrordefault_return_type)r-   r   r   r   r&      s
   

r&   c                 C   s  t | jtsJ t | jtsJ t | jtsJ | jj}t |ts#J |j	}t |t
tfs/J |j}t |ts9J |j}t |tsCJ |jjtksKJ |jsPJ t|jdksYJ |jd }t |tseJ |jsjJ |jtthvrt| jS ttj| j }g }tj| j D ]}t |tsJ |jjtksJ t|jdksJ || qtt|dd |dd }	tt|dd |dd }
|jtkrt|	|
D ]0\\}}\}}t |tsJ t |tsJ |jjtksJ |jjtksJ t||||| jd qt| jj|d jd |d jd gS |jtkret|	|
D ]5\\}}\}}t |ts,J t |ts4J |jjtks=J |jjtksFJ t||||| jd qt| jj|d jd |d jd gS J d)	a  
    Determine a more specific model type for functions that combine models.

    This function operates on function *calls*. It analyzes each function call
    by looking at the function definition and the arguments passed as part of
    the function call, then determines a more specific return type for the
    function call.

    This method accepts a `FunctionContext` as part of the Mypy plugin
    interface. This function context provides easy access to:
    * `args`: List of "actual arguments" filling each "formal argument" of the
      called function. "Actual arguments" are those passed to the function
      as part of the function call. "Formal arguments" are the parameters
      defined by the function definition. The same actual argument may serve
      to fill multiple formal arguments. In some cases the relationship may
      even be ambiguous. For example, calling `range(*args)`, the actual
      argument `*args` may fill the `start`, `stop` or `step` formal
      arguments, depending on the length of the list.

      The `args` list is of length `num_formals`, with each element
      corresponding to a formal argument. Each value in the `args` list is a
      list of actual arguments which may fill the formal argument. For
      example, in the function call `range(*args, num)`, `num` may fill the
      `start`, `end` or `step` formal arguments depending on the length of
      `args`, so type-checking needs to consider all of these possibilities.
    * `arg_types`: Type annotation (or inferred type) of each argument. Like
      `args`, this value is a list of lists with an outer list entry for each
      formal argument and an inner list entry for each possible actual
      argument for the formal argument.
    * `arg_kinds`: "Kind" of argument passed to the function call. Argument
      kinds include positional, star (`*args`), named (`x=y`) and star2
      (`**kwargs`) arguments (among others). Like `args`, this value is a list
      of lists.
    * `context`: AST node representing the function call with all available
      type information. Notable attributes include:
      * `args` and `arg_kinds`: Simple list of actual arguments, not mapped to
        formal arguments.
      * `callee`: AST node representing the function being called. Typically
        this is a `NameExpr`. To resolve this node to the function definition
        it references, accessing `callee.node` will usually return either a
        `FuncDef` or `Decorator` node.
    * etc.

    This function infers a more specific type for model-combining functions by
    making certain assumptions about how the function operates based on the
    order of its formal arguments and its return type.

    If the return type is `Model[InT, XY_YZ_OutT]`, the output of each
    argument is expected to be used as the input to the next argument. It's
    therefore necessary to check that the output type of each model is
    compatible with the input type of the following model. The combined model
    has the type `Model[InT, OutT]`, where `InT` is the input type of the
    first model and `OutT` is the output type of the last model.

    If the return type is `Model[InT, XY_XY_OutT]`, all model arguments
    receive input of the same type and are expected to produce output of the
    same type. It's therefore necessary to check that all models have the same
    input types and the same output types. The combined model has the type
    `Model[InT, OutT]`, where `InT` is the input type of all model arguments
    and `OutT` is the output type of all model arguments.

    Raises:
        AssertionError: Raised if a more specific model type couldn't be
            determined, indicating that the default general return type should
            be used.
          Nl1_argl1_typel2_argl2_typeapir   Fz;Thinc mypy plugin error: it should return before this point)
isinstancer9   r   r0   r   contextr   calleer   noder
   r   typer   ret_typer%   thinc_model_fullnameargslenr   intoin_outtoout_out_fullnamechained_out_fullnamelist	itertoolschain	arg_typesappendzipcheck_chainedcheck_intoin_outtoout)r-   r<   callee_nodecallee_node_typecallee_return_typeout_typerA   rH   arg_type	arg_pairsarg_types_pairsarg1arg2type1type2r   r   r   r.   &   sj   E


 
 r.   r5   r6   r7   r8   r9   c                 C   sr   t |jd |jd s7|jd|jd  d|jd  d| td |jd|jd  d|jd  d	|td d S d S )
Nr2   r   zLayer outputs type (z) but the next layer expects (z) as an inputcodezLayer input type (z!) is not compatible with output (z) from previous layer)r   rA   failerror_layer_outputerror_layer_inputr4   r   r   r   rK      s   
rK   c                 C   s   |j d |j d kr4|jd|j d  d|j d  d| td |jd|j d  d|j d  d|td |j d |j d krj|jd|j d  d	|j d  d| td |jd|j d  d
|j d  d|td d S d S )Nr   zLayer input (z() not compatible with next layer input ()rX   z,) not compatible with previous layer input (r2   zLayer output (z)) not compatible with next layer output (z-) not compatible with previous layer output ()rA   rZ   r\   r[   r4   r   r   r   rL      s.   
rL   zlayer-mismatch-inputzInvalid layer inputThinczlayer-mismatch-outputzInvalid layer outputc                       sJ   e Zd Zdedeeef dededededee	e	f f fdd	Z
  ZS )
IntrospectCheckererrorsmodulesr   treepathr   per_line_checking_time_nsc              	      s"   g | _ t ||||||| d S r   )_error_messagesr    r!   )r"   r`   ra   r   rb   rc   r   rd   r#   r   r   r!      s   
zIntrospectChecker.__init__)r(   r)   r*   r   r   r+   r   r   r   intr!   r,   r   r   r#   r   r_      s     

r_   )+rF   typingr   r   mypy.checkerr   mypy.errorcodesr   mypy.errorsr   
mypy.nodesr   r   r	   r
   r   r   mypy.optionsr   mypy.pluginr   r   r   mypy.subtypesr   
mypy.typesr   r   r   r   r@   rD   rC   r+   r   r   r&   r.   rK   rL   r\   r[   r_   r   r   r   r   <module>   sV      

 