
    X-Ph                    X   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZmZmZ ddlmZ ddlmZmZ dd	lm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&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z> ddl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZP ddlQmRZRmSZS ddlTmUZU  G d de          ZV G d de          ZW G d de5          ZX G d de;          ZYdXd&ZZ G d' d(e;          Z[dYd*Z\ G d+ d,e]          Z^dZd0Z_d[d2Z` G d3 d4          Zad\d8Zbd]d=Zcd^d>Zd G d? d@eK          Ze edAeGB          Zfd_dCZg G dD dEeL          Zhd`dGZidadKZjdbdNZkdcdRZldddSZm edT          ZndedWZodS )fa  Mechanisms for inferring function types based on callsites.

Currently works by collecting all argument types at callsites,
synthesizing a list of possible function types from that, trying them
all, and picking the one with the fewest errors that we think is the
"best".

Can return JSON that pyannotate can use to apply the annotations to code.

There are a bunch of TODOs here:
 * Maybe want a way to surface the choices not selected??
 * We can generate an exponential number of type suggestions, and probably want
   a way to not always need to check them all.
 * Our heuristics for what types to try are primitive and not yet
   supported by real practice.
 * More!

Other things:
 * This is super brute force. Could we integrate with the typechecker
   more to understand more about what is going on?
 * Like something with tracking constraints/unification variables?
 * No understanding of type variables at *all*
    )annotationsN)Iterator)contextmanager)Callable
NamedTuple	TypedDictTypeVarcast)map_actuals_to_formals)GraphStatehas_any_type)InvalidSourceListSourceFinder)join_type_list)meet_type_list)PYTHON_EXTENSIONS)ARG_STAR	ARG_STAR2ArgKindCallExpr	Decorator
ExpressionFuncDefMypyFileRefExpr
ReturnStmt
SymbolNodeSymbolTableTypeInfoVarreverse_builtin_aliases)Options)FunctionContextMethodContextPlugin)FineGrainedBuildManager)state)TraverserVisitor)	bind_selfmake_simplified_union)AnyTypeCallableTypeFunctionLikeInstanceNoneType
ProperType	TupleTypeTypeTypeAliasTypeTypedDictType	TypeOfAnyTypeStrVisitorTypeTranslatorTypeVarTypeUninhabitedType	UnionTypeget_proper_type)is_overlapping_noneremove_optional)split_targetc                  $    e Zd ZU ded<   ded<   dS )PyAnnotateSignaturestrreturn_type	list[str]	arg_typesN__name__
__module____qualname____annotations__     P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/suggestions.pyrB   rB   W   s*         rM   rB   c                  L    e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   ded<   dS )CallsiterC   pathintlinelist[list[ArgKind]]	arg_kindszlist[str | None]callee_arg_nameslist[list[str | None]]	arg_nameslist[list[Type]]rF   NrG   rL   rM   rN   rP   rP   \   sT         IIIIII""""&&&&%%%%rM   rP   c                  2    e Zd ZdZddZdd	ZddZddZdS )SuggestionPluginz0Plugin that records all calls to a given target.targetrC   returnNonec                    |                     d          r|                    dd          d         }|| _        g | _        d S )N)z.__new__z	.__init__.   r   )endswithrsplitr\   mystery_hits)selfr\   s     rN   __init__zSuggestionPlugin.__init__h   sF    ??455 	.]]3**1-F -/rM   fullname(Callable[[FunctionContext], Type] | Nonec                *    || j         k    r| j        S d S Nr\   logre   rg   s     rN   get_function_hookz"SuggestionPlugin.get_function_hookq       t{""8O4rM   &Callable[[MethodContext], Type] | Nonec                *    || j         k    r| j        S d S rj   rk   rm   s     rN   get_method_hookz SuggestionPlugin.get_method_hookw   ro   rM   ctxFunctionContext | MethodContextr4   c           
         | j                             t          |j        j        |j        j        |j        |j        |j	        |j
                             |j        S rj   )rd   appendrP   apirQ   contextrS   rU   rV   rX   rF   default_return_type)re   rs   s     rN   rl   zSuggestionPlugin.log}   sY       $ 		
 		
 		
 &&rM   N)r\   rC   r]   r^   )rg   rC   r]   rh   )rg   rC   r]   rp   )rs   rt   r]   r4   )rH   rI   rJ   __doc__rf   rn   rr   rl   rL   rM   rN   r[   r[   e   sj        ::/ / / /      ' ' ' ' ' 'rM   r[   c                  *    e Zd ZdZddZdd	ZddZdS )ReturnFinderz7Visitor for finding all types returned from a function.typemapdict[Expression, Type]r]   r^   c                "    || _         g | _        d S rj   )r}   return_types)re   r}   s     rN   rf   zReturnFinder.__init__   s    (*rM   or   c                    |j         :|j         | j        v r.| j                            | j        |j                             d S d S d S rj   )exprr}   r   rv   re   r   s     rN   visit_return_stmtzReturnFinder.visit_return_stmt   sL    6!&DL"8"8$$T\!&%9::::: "8"8rM   r   c                    d S rj   rL   r   s     rN   visit_func_defzReturnFinder.visit_func_def   s    rM   N)r}   r~   r]   r^   )r   r   r]   r^   )r   r   r]   r^   )rH   rI   rJ   rz   rf   r   r   rL   rM   rN   r|   r|      sV        AA+ + + +; ; ; ;     rM   r|   r}   r~   funcr   r]   
list[Type]c                b    t          |           }|j                            |           |j        S )z9Find all the types returned by return statements in func.)r|   bodyacceptr   r}   r   finders      rN   get_return_typesr      s.    '""FIVrM   c                  "    e Zd ZdZddZddZdS )ArgUseFinderzVisitor for finding all the types of arguments that each arg is passed to.

    This is extremely simple minded but might be effective anyways.
    r   r   r}   r~   r]   r^   c                @    || _         d |j        D             | _        d S )Nc                    i | ]
}|j         g S rL   )variable).0args     rN   
<dictcomp>z)ArgUseFinder.__init__.<locals>.<dictcomp>   s    7c7c7cSb7c7c7crM   )r}   	argumentsrF   )re   r   r}   s      rN   rf   zArgUseFinder.__init__   s&    7c7cTXTb7c7c7crM   r   r   c                    t           fd|j        D                       sd S t           j                            |j                            }t          |t                    sd S t          |j	        |j
        |j	        |j
        d           }t          |          D ]j\  }}|D ]b}|j        |         }t          |t                    r>|j         j        v r0 j        |j                                     |j        |                    ckd S )Nc              3  ^   K   | ]'}t          |t                    o|j        j        v V  (d S rj   )
isinstancer   noderF   )r   ere   s     rN   	<genexpr>z/ArgUseFinder.visit_call_expr.<locals>.<genexpr>   s;      WW1:a))Faf.FWWWWWWrM   c                4    t          t          j                  S rj   )r-   r7   special_form)ns    rN   <lambda>z.ArgUseFinder.visit_call_expr.<locals>.<lambda>   s    gi455 rM   )anyargsr=   r}   getcalleer   r.   r   rU   rX   	enumerater   r   rF   rv   )re   r   typformal_to_actualir   arg_idxr   s   `       rN   visit_call_exprzArgUseFinder.visit_call_expr   s%   WWWWPQPVWWWWW 	Fdl..qx8899#|,, 	F1KKMM55
 
 !!122 	F 	FGAt F FfWoc7++ FDN0J0JN38,33CM!4DEEEF	F 	FrM   N)r   r   r}   r~   r]   r^   )r   r   r]   r^   )rH   rI   rJ   rz   rf   r   rL   rM   rN   r   r      sP         
d d d dF F F F F FrM   r   rY   c                ~    t          ||           |j                                       fd|j        D             S )zFind all the types of arguments that each arg is passed to.

    For example, given
      def foo(x: int) -> None: ...
      def bar(x: str) -> None: ...
      def test(x, y):
          foo(x)
          bar(y)

    this will return [[int], [str]].
    c                4    g | ]}j         |j                 S rL   )rF   r   )r   r   r   s     rN   
<listcomp>z get_arg_uses.<locals>.<listcomp>   s#    EEEsFS\*EEErM   )r   r   r   r   r   s     @rN   get_arg_usesr      sF     $((FIVEEEEdnEEEErM   c                      e Zd ZdS )SuggestionFailureN)rH   rI   rJ   rL   rM   rN   r   r      s        DrM   r   r   r-   boolc                ,    | j         t          j        k    S rj   )type_of_anyr7   explicitr   s    rN   is_explicit_anyr      s     ?i000rM   r4   c                j    t          |           } t          | t                    ot          |            S rj   )r=   r   r-   r   r   s    rN   is_implicit_anyr      s/    
#

Cc7##@OC,@,@(@@rM   c                  2   e Zd ZdZdddddddidZdjdZdjdZedkd            Zedld            Z	dmdZ
dmd Zdnd)Zdod*Zdpd,Zdqd/Zdrd2Zdsd4Zdtd6Zdud9Zdvd?ZdwdBZdxdFZdydKZdzdNZd{dRZd|dUZd}d~dXZdd[Zdd^Zdd`ZddbZdddZ ddgZ!ddhZ"dS )SuggestionEnginez8Engine for finding call sites and suggesting signatures.FN)	no_errorsno_anyflex_any	use_fixmemax_guesses	fgmanagerr(   jsonr   r   r   r   float | Noner   
str | Noner   
int | Noner]   r^   c                  || _         |j        | _        | j        j        | _        |j        | _        t	          | j        j        | j        j                  | _        || _        || _	        || _
        |rd| _
        |pd| _        || _        d S )N      ?@   )r   managerplugingraphr   fscacheoptionsr   	give_jsonr   r   r   r   )re   r   r   r   r   r   r   r   s           rN   rf   zSuggestionEngine.__init__   s     # (l)_
"4<#79MNN"  	 DM&,""rM   functionrC   c                z   |                      |          \  }}}|                     |          5  |                                 5  |                     ||          }ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   | j        r|                     ||||          S |                     |          S )z&Suggest an inferred type for function.N)	find_noderestore_afterwith_export_typesget_suggestionr   json_suggestionformat_signature)re   r   mod	func_namer   
suggestions         rN   suggestzSuggestionEngine.suggest  s9   #~~h77Y$$ 	< 	<'')) < <!00d;;
< < < < < < < < < < < < < < <	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< > 	5''YjIII((444s5   A>A'A>'A+	+A>.A+	/A>>BBc                                          |          \  }}}                     |          5                       |          \  }}ddd           n# 1 swxY w Y   d                    t	           fd|D                                 S )z&Find a list of call sites of function.N
c                \    g | ](\  }}}}}}| d | d                     |||           )S ):z: )format_args)r   rQ   rS   rU   _rX   rF   re   s          rN   r   z6SuggestionEngine.suggest_callsites.<locals>.<listcomp>  s]       FdIq)Y YYdYYd&6&6y)Y&W&WYY  rM   )r   r   get_callsitesjoindedup)re   r   r   r   r   	callsitess   `     rN   suggest_callsitesz"SuggestionEngine.suggest_callsites  s    ~~h//Q$$ 	4 	4--d33LIq	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 yy   JS   
 
 	
s   AAAmoduleIterator[None]c              #     K   	 dV  |                      | j        |                    dS # |                      | j        |                    w xY w)zContext manager that reloads a module after executing the body.

        This should undo any damage done to the module state while mucking around.
        N)reloadr   )re   r   s     rN   r   zSuggestionEngine.restore_after"  sR      	,EEEKK
6*+++++DKK
6*++++s	   * "Ac              #     K   | j         j        j        }d| j         j        _        	 dV  || j         j        _        dS # || j         j        _        w xY w)zContext manager that enables the export_types flag in the body.

        This causes type information to be exported into the manager's all_types variable.
        TN)r   r   export_types)re   olds     rN   r   z"SuggestionEngine.with_export_types-  s\       l"/,0)	4EEE03DL ---DL -3333s	   = Afdefr   r.   c           	         t          d |j        D             |j        |j        t          t          j                  |                     d                    S )z?Generate a trivial callable type from a func def, with all Anysc                @    g | ]}t          t          j                  S rL   )r-   r7   suggestion_engine)r   r   s     rN   r   z5SuggestionEngine.get_trivial_type.<locals>.<listcomp>@  s#    JJJaWY011JJJrM   zbuiltins.function)r.   rU   rX   r-   r7   r   
named_typere   r   s     rN   get_trivial_typez!SuggestionEngine.get_trivial_type:  sQ    
 JJ4>JJJNNI/00OO/00
 
 	
rM   c                    t          |j        t                    rt          |j                  S |                     |          S rj   )r   typer.   make_suggestion_anysr   r   s     rN   get_starting_typez"SuggestionEngine.get_starting_typeG  s:    di.. 	/'	222((...rM   	is_methodbasedefaultslist[Type | None]r   list[Callsite]usesrY   c           	        g }t          t          |j                            D ]}|dk    r0|r.|                    t	          t
          j                  g           9g }|D ]9}	|	j        ||z
           D ]&}
t          |
          s|                    |
           ':g }||         D ]&}
t          |
          s|                    |
           '||         }|r,|                    |           |r|                    |           g }|r`t          d |D                       rG|                    t          j        |d         t	          t
          j                  g                     nQ|r#|                    t          |                     n,|                    t	          t
          j                             |r"|                    t          |                     |                    |           |S )z:Produce a list of type suggestions for each argument type.r   c              3  Z   K   | ]&}t          t          |          t                    V  'd S rj   )r   r=   r1   )r   tps     rN   r   z,SuggestionEngine.get_args.<locals>.<genexpr>q  sE       % %>@
?2..99% % % % % %rM   )rangelenrU   rv   r-   r7   r   rF   r   allr<   
make_unionr   extendgenerate_type_combinationsr   )re   r   r   r   r   r   typesr   all_arg_typescallr   all_use_typesdefaultrF   s                 rN   get_argszSuggestionEngine.get_argsM  s6    #%s4>**++ )	$ )	$AAvv)vgi&ABBCDDDM! 2 2>!i-8 2 2C*3// 2%,,S1112 MAw . .&s++ .!((---qkG 2$$W---  2!((111I 	> % %DQ% % % " " 	>   (-*:GIDV<W<W)XYY     >  !;M!J!JKKKK  );!<!<=== @  !>!>???LL####rM   c                *      fd|j         D             S )Nc                P    g | ]"}|j         rj        j        |j                  nd #S rj   )initializerr   	all_types)r   r   re   s     rN   r   z:SuggestionEngine.get_default_arg_types.<locals>.<listcomp>  sA     
 
 
 8;PDL"3?33D
 
 
rM   )r   r   s   ` rN   get_default_arg_typesz&SuggestionEngine.get_default_arg_types  s0    
 
 
 
~
 
 
 	
rM   list[CallableType]c                    |                      ||||          }t          j        t          j        | d| j                  }fd|D             S )zCompute a list of guesses for a function's type.

        This focuses just on the argument types, and doesn't change the provided return type.
        r   c           
     r    g | ]3}t                              t          |                               4S ))rF   )refine_callablecopy_modifiedlist)r   xr   s     rN   r   z0SuggestionEngine.get_guesses.<locals>.<listcomp>  s;    ^^^QRd&8&8477&8&K&KLL^^^rM   )r  	itertoolsisliceproductr   )re   r   r   r   r   r   r   r  s     `     rN   get_guesseszSuggestionEngine.get_guesses  sZ     --	49dKK "9#4g#>4CSTT^^^^V]^^^^rM   r    tuple[list[Callsite], list[str]]c                \   |                      |          }t          |j                  }| j        j                            d|           	 |                     ||          }| j        j                            d           n$# | j        j                            d           w xY w|j        |fS )z"Find all call sites of a function.r   )	r   r[   rg   r   _pluginsinserttry_typepoprd   )re   r   new_typecollector_pluginerrorss        rN   r   zSuggestionEngine.get_callsites  s    ))$//+DM::##A'7888	(]]422FK $$Q''''DK $$Q'''',f44s   B !B"guessesignore_returnc                (      fd|D             S )z~Apply any configured filters to the possible guesses.

        Currently the only option is filtering based on Any prevalance.c                V    g | ]%}j         t          |          j         k    #|&S rj   )r   any_score_callable)r   tr(  r   re   s     rN   r   z3SuggestionEngine.filter_options.<locals>.<listcomp>  sG     
 
 
}$!!Y>>$-OO  POOrM   rL   )re   r'  r   r(  s   ` ``rN   filter_optionszSuggestionEngine.filter_options  s:    
 
 
 
 
 

 
 
 	
rM   tuple[CallableType, int]c                     |st          d           fd|D             t          | fd          }|t          |                   fS )zFrom a list of possible function types, find the best one.

        For best, we want the fewest errors, then the best "score" from score_callable.
        zNo guesses that match criteria!c                >    i | ]}|                     |          S rL   )r"  )r   guessr   re   s     rN   r   z.SuggestionEngine.find_best.<locals>.<dictcomp>  s)    III%tU33IIIrM   c                X    t          |                                        |           fS rj   )count_errorsscore_callable)sr&  re   s    rN   r   z,SuggestionEngine.find_best.<locals>.<lambda>  s'    <q	+B+BDDWDWXYDZDZ*[ rM   )key)r   minr3  )re   r   r'  bestr&  s   ``  @rN   	find_bestzSuggestionEngine.find_best  sq    
  	G#$EFFFIIIIIIII7 [ [ [ [ [\\\\&,////rM   r   c                   |j         sg S |j         j        dd         D ]}|j                            |j                  }|r~t          |j        t          t          f          r]t          |j        j
                  }t          |t                    r/t          |j                  t          |j                  k    r|gc S g S )z8Try to get a guess of a method type from a parent class.ra   N)infomronamesr   namer   r   r   r   r=   r   r.   r  rF   r   )re   r   parentpnoder   s        rN   get_guesses_from_parentz(SuggestionEngine.get_guesses_from_parent  s    y 	IimABB' 	! 	!FL$$TY//E !EJ)0DEE !%ejo66c<00 !S5G5G3t~K^K^5^5^  5LLL	rM   r   rB   c                \   | j         }|                     |          \  }}t          | j        j        |          }| j        r|rt          d          t          |j                  o|j	         }t          j        ||         j        j                  5  |                     ||                     |          |                     |          ||          }ddd           n# 1 swxY w Y   ||                     |          z  }|                     ||d          }|                     ||          \  }	|                     |           t+          | j        j        |          }
t          j        ||         j        j                  5  |
rt-          |
          }nt/                      g}ddd           n# 1 swxY w Y   fd|D             }|                     ||d          }|                     ||          \  }| j        r|rt          d          |                     ||          S )zxCompute a suggestion for a function.

        Return the type and whether the first argument should be ignored.
        zFunction does not typecheck.NT)r(  c                b    g | ]+}                     t          j        |                     ,S )ret_type)r  refine_typerE  )r   r,  r8  s     rN   r   z3SuggestionEngine.get_suggestion.<locals>.<listcomp>  s7    aaaRS4%%{4=!/L/L%MMaaarM   FzNo annotation without errors)r   r   r   r   r  r   r   r   r;  	is_staticr)   strict_optional_setr   strict_optionalr  r   r  rA  r-  r9  r"  r   r  r1   pyannotate_signature)re   r   r   r   r   orig_errorsr   r   r'  r   returns	ret_typesr&  r8  s                @rN   r   zSuggestionEngine.get_suggestion  s   
 
!%!3!3D!9!9	;DL2D99> 	Dk 	D#$BCCCOO:DN(:	&uSz'9'IJJ 	 	&&&&t,,**400 G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	4//555%%gy%MM..w//a 	dD!!!"4<#94@@&uSz'9'IJJ 	) 	) )6w??		%ZZL			) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) baaaW`aaa%%gy%NN~~dG44f> 	Df 	D#$BCCC((i>>>s%   A CC#&C#"F55F9<F9rU   rT   rX   rW   rF   c                   g }t          t          |                    D ]}t          ||         ||         ||                   D ][\  }}}|                     d |          }	|t          k    rd|	z   }	-|t
          k    rd|	z   }	>|                                r	|r| d|	 }	\|                    |	           dd                    |           dS )N*z**=(, ))	r  r  zipformat_typer   r   is_namedrv   r   )
re   rU   rX   rF   r   r   kindr>  r   r   s
             rN   r   zSuggestionEngine.format_args  s     s9~~&& 
	 
	A#&y|Yq\9Q<#P#P . .dC&&tS118##)CCY&&*CC]]__ . .!%ooooKK%499T??%%%%rM   r6  tuple[str, str, FuncDef]c                   d}d|v r|                     d          dk    r"t          d                    |                    |                    d          \  }}|                                st          d|           t          |          }|                     ||          \  }}|j        t          |          dz   d         }nIt          | j
        j        |          }|st          d|           |\  }}|                     ||          }t          |t                    r*|                     |          }|st          d| d          t          |t                     st          d| d	          |||fS )
a4  From a target name, return module/target names and the func def.

        The 'key' argument can be in one of two formats:
        * As the function full name, e.g., package.module.Cls.method
        * As the function location as file and line separated by column,
          e.g., path/to/file.py:42
        Nr   ra   zgMalformed location for function: {}. Must be either package.module.Class.method or path/to/file.py:linez"Line number must be a number. Got zCannot find module for zObject z is a decorator we can't handlez is not a function)countr   formatsplitisdigitrR   find_node_by_file_and_linerg   r  r@   r   r   find_node_by_module_and_namer   r   extract_from_decoratorr   )	re   r6  r   filerS   line_numbermodnametailr\   s	            rN   r   zSuggestionEngine.find_node  s    #'#::yy~~!!'KKQ6RU;;   3JD$<<>> U'(ST(S(STTTd))K ;;D+NNMGT=W!1!3!34DD!$."6<<F I'(G#(G(GHHH"MGT44WdCCDdI&& 	X..t44D X'(V#(V(V(VWWW$(( 	G#$Ec$E$E$EFFFd""rM   rc  rd  SymbolNode | Nonec                   |                      | j        j        |                   }|j        }|                    d          }t          |dd                   D ]\  }}||vrAt          d                    |d                    |d|dz                                          ||         j	        }t          |t                    sAt          d                    |d                    |d|dz                                          |j        }|d         }	|	|vr@|dz   |z   }
t          d                    t          |          dk    rdnd	|
                    ||	         j	        S )
znFind symbol node by module id and qualified name.

        Raise SuggestionFailure if can't find one.
        r`   NzUnknown class {}.{}ra   zObject {}.{} is not a classzUnknown {} {}methodr   )ensure_loadedr   r   r=  r\  r   r   r[  r   r   r   r!   r  )re   rc  rd  treer=  
componentsr   	componentr   funcnamer6  s              rN   r_  z-SuggestionEngine.find_node_by_module_and_name9  s   
 !!$."6w"?@@
 "Z ZZ__
%j"o66 
	 
	LAy%%')00#((:gPQTUPUgCV:W:WXX   ',I&6&;DdH-- '188#((:V]XY\]X]V]K^B_B_``   JEE b>5  C-$&C#&&3z??Q3F3FxxJX[\\   X##rM   ra  rS   rR   tuple[str, SymbolNode]c                   t          fdt          D                       st          d          	 | j                            t
          j                                                \  }}n%# t          $ r}t          dz             |d}~ww xY w|| j	        vrt          d|z             | 
                    | j        j	        |         d          }d}d}|                                D ]L\  }}	}t          |	j        t          t           f          r|	j        j        }
n5|
|k    r||
|k    r	|
}|	j        }M|st          d|           ||fS )	zFind symbol node by path to file and line number.

        Find the first function declared *before or on* the line number.

        Return module id and the node found. Raise SuggestionFailure if can't find one.
        c              3  B   K   | ]}                     |          V  d S rj   )rb   )r   extra  s     rN   r   z>SuggestionEngine.find_node_by_file_and_line.<locals>.<genexpr>c  s/      CC#4==%%CCCCCCrM   z Source file is not a Python filezInvalid source file name: NzUnknown module: T)forcezCannot find a function at line )r   r   r   r   crawl_uposrQ   normpathr   r   ri  r   local_definitionsr   r   r   r   rS   )re   ra  rS   rc  r   r   rj  r   closest_linesymsym_lines    `         rN   r^  z+SuggestionEngine.find_node_by_file_and_line\  s    CCCC1BCCCCC 	H#$FGGG	P--bg.>.>t.D.DEEJGQQ  	P 	P 	P#$@4$GHHaO	P$*$$#$6$@AAA!!$."6w"?t!LL"&#'//11 
	  
	 IAsA#(Wi$899 8=  4\%9X=T=T'x 	N#$Ld$L$LMMM}s   :A- -
B7B

Br   FuncDef | Nonec                   |j         D ]}d }t          |t                    r;t          |j        t          t
          f          rt          |j        j                  }nt          |t                    rt          |j	        t                    rst          |j	        j        t          t
          t          f          rGt          t          |j	        j        j                  x}t                    rt          |j                  }t          |t                    r^|j                            d          }t          |t
                    r/t          |j        t                    rt!          |j        d           }t          |t                    s d S |j        D ]T}t%          |j                  dk    r6t          |j        d         t(                    r|j        d         |j        k    s  d S U|j        S )N__call__ra   r   )
decoratorsr   r   r   r"   r   r=   r   r   r   r   r.   rE  r0   
get_methodr/   r+   itemsr  rF   r:   r   )re   r   decr   call_tpcall_methodcts          rN   r`  z'SuggestionEngine.extract_from_decorator  s   ? 	  	 CC#w'' 8Jsx#w,P,P 8%chm443))8sz7338 szGS0IJJ8 ?3:?;O+P+P PS_``	8 &g&677#x(( <!h11*==k733 <
;CSUa8b8b <#K$4d;;Cc<00 tti    %%**"2<?K@@ +Q2;66444 7	  yrM   r   r2   rE   c                    |j         }d|_        ||_         	 | j                            |j                  }|||_         S # ||_         w xY w)z_Recheck a function while assuming it has type typ.

        Return all error messages.
        N)unanalyzed_typer   r   triggerrg   )re   r   r   r   ress        rN   r"  zSuggestionEngine.try_type  s]    
 " 	"	'.((77C #&D  3D &&&&s	    ? 	Ar)   r   c                    |j         J | j                                         | j                            |j        |j         fgg           S )z"Recheck the module given by state.)rQ   r   flush_cacheupdateid)re   r)   s     rN   r   zSuggestionEngine.reload  sH    z%%%""$$$~$$ux&<%=rBBBrM   rr  r   c                v    |j         r|j         j        s|r|                     |           |j         J |j         S )z?Make sure that the module represented by state is fully loaded.)rj  is_cache_skeletonr   )re   r)   rr  s      rN   ri  zSuggestionEngine.ensure_loaded  sG    z 	UZ9 	U 	KKz%%%zrM   r5  r0   c                @    | j         j                            |          S rj   )r   semantic_analyzerr   )re   r5  s     rN   r   zSuggestionEngine.named_type  s    |-88;;;rM   r   r   c                    |j         s|j        r|                    dd          d         }t          j                            | j        |         j                  }||j        ||dd}t          j
        |gd          S )zLProduce a json blob for a suggestion suitable for application by pyannotate.r`   ra   rg  r   )	signaturerS   rQ   r   samplesT)	sort_keys)is_classrG  r\  rt  rQ   abspathr   xpathrS   r   dumps)re   r   r   r   r   rQ   objs          rN   r   z SuggestionEngine.json_suggestion  s    
 = 	4DN 	4!Q//3I
 wtz#455 $I"
 
 z3%40000rM   
cur_modulec                     t          |          } fd|j        |d         D                                  |j                  dS )z+Format a callable type as a pyannotate dictc                <    g | ]}                     |          S rL   )rU  )r   r,  r  re   s     rN   r   z9SuggestionEngine.pyannotate_signature.<locals>.<listcomp>  s)    YYYa$**:q99YYYrM   N)rF   rD   )rR   rF   rU  rE  )re   r  r   r   starts   ``   rN   rJ  z%SuggestionEngine.pyannotate_signature  s]     IYYYYY3=QVQWQWCXYYY++JEE
 
 	
rM   sigc                P    dd                     |d                    d|d          S )zDFormat a callable type in a way suitable as an annotation... kind ofrQ  rR  rF   z) -> rD   )r   )re   r  s     rN   r   z!SuggestionEngine.format_signature  s.    I499S-..IIS5GIIIrM   r4   c                    | j         r)t          t          |          t                    r| j         S |                    t          || j        | j        j                            S rj   )	r   r   r=   r-   r   TypeFormatterr   r   r   )re   r  r   s      rN   rU  zSuggestionEngine.format_type  sR    > 	"j)=)=wGG 	">!zz-
DJ@TUUVVVrM   r,  arg_posc                   t          |          }t          |t                    rdS |rt          |t                    rdS t          |t                    rQt          d |j        D                       rdS t          d |j        D                       rdS t          |          sdS t          |t                    r t          |          st          |          rdS dS )zGenerate a score for a type that we use to pick which type to use.

        Lower is better, prefer non-union/non-any types. Don't penalize optionals.
           c              3  Z   K   | ]&}t          t          |          t                    V  'd S rj   r   r=   r-   r   r  s     rN   r   z.SuggestionEngine.score_type.<locals>.<genexpr>  s4      LLq:oa00'::LLLLLLrM   c              3  4   K   | ]}t          |          V  d S rj   r   r  s     rN   r   z.SuggestionEngine.score_type.<locals>.<genexpr>  s(      44q<??444444rM      
   r   )r=   r   r-   r1   r<   r   r  r>   r.   r   is_tricky_callable)re   r,  r  s      rN   
score_typezSuggestionEngine.score_type  s    
 Aa!! 	2 	z!X.. 	2a## 	LLAGLLLLL r44AG44444 r&q)) ra&& 	LOO 	?QRS?T?T 	2qrM   c                ~     t           fd|j        D                                            |j        d          z   S )Nc              3  F   K   | ]}                     |d           V  dS )Tr  N)r  )r   r  re   s     rN   r   z2SuggestionEngine.score_callable.<locals>.<genexpr>   s3      II4??1d?33IIIIIIrM   Fr  )sumrF   r  rE  re   r,  s   ` rN   r4  zSuggestionEngine.score_callable  sO    IIIIQ[IIIIIDOOJ M\ M
 M
 
 	
rM   )r   r(   r   r   r   r   r   r   r   r   r   r   r   r   r]   r^   )r   rC   r]   rC   )r   rC   r]   r   )r]   r   )r   r   r]   r.   )r   r   r   r.   r   r   r   r   r   rY   r]   rY   )r   r   r]   r   )r   r   r   r.   r   r   r   r   r   rY   r]   r  )r   r   r]   r  )r'  r  r   r   r(  r   r]   r  )r   r   r'  r  r]   r.  )r   r   r]   r  )r   rC   r   r   r]   rB   )rU   rT   rX   rW   rF   rY   r]   rC   )r6  rC   r]   rX  )rc  rC   rd  rC   r]   re  )ra  rC   rS   rR   r]   rn  )r   r   r]   rz  )r   r   r   r2   r]   rE   )r)   r   r]   rE   )F)r)   r   rr  r   r]   r   )r5  rC   r]   r0   )
r   rC   r   rC   r   r   r   rB   r]   rC   )r  r   r   r   r   r.   r]   rB   )r  rB   r]   rC   )r  r   r   r4   r]   rC   )r,  r4   r  r   r]   rR   )r,  r.   r]   rR   )#rH   rI   rJ   rz   rf   r   r   r   r   r   r   r   r  r  r  r   r-  r9  rA  r   r   r   r_  r^  r`  r"  r   ri  r   r   rJ  r   rU  r  r4  rL   rM   rN   r   r      s       BB  !% $"&# # # # # #45 5 5 5
 
 
 
 , , , ^, 
4 
4 
4 ^
4
 
 
 
/ / / /4 4 4 4l
 
 
 
_ _ _ _$5 5 5 5
 
 
 
	0 	0 	0 	0   "*? *? *? *?X& & & &(%# %# %# %#N!$ !$ !$ !$F! ! ! !F   <' ' ' '(C C C C    < < < <1 1 1 1,
 
 
 
J J J JW W W W
   *
 
 
 
 
 
rM   r   utr  floatc                   t          |           }t          |t                    r|j        t          j        k    rdS t          |t                    r|rdS t          |t                    r@t          d |j	        D                       rdS t          d |j	        D                       rdS t          |t                    rt          |          rdS t          |          rdS dS )ziGenerate a very made up number representing the Anyness of a type.

    Higher is better, 1.0 is max
    r   g      ?c              3  Z   K   | ]&}t          t          |          t                    V  'd S rj   r  r  s     rN   r   z!any_score_type.<locals>.<genexpr>  s4      HH1z/!,,g66HHHHHHrM   c              3  4   K   | ]}t          |          V  d S rj   r   r  s     rN   r   z!any_score_type.<locals>.<genexpr>  s(      001|A000000rM   g      ?r   )r=   r   r-   r   r7   r   r1   r<   r   r  r.   r  r   )r  r  r,  s      rN   any_score_typer    s    
 	A!W !-93N"N"Nq!X 7 s!Y HHHHHHH 	30000000 	4!\"" '9!'<'< sA s3rM   r,  r.   r   r(  c                    d | j         t          |          d          D             }t          t          | j                  t
                    r|s!|rdnt          | j        d          }|||gz  }t          |          t          |          z  S )Nc                0    g | ]}t          |d           S )Tr  )r  r  s     rN   r   z&any_score_callable.<locals>.<listcomp>  s%    UUU!nQ---UUUrM   r   Fr  )	rF   rR   r   r=   rE  r1   r  r  r  )r,  r   r(  scoresrets        rN   r+  r+    s    UUq{3y>>CSCS7TUUUF oaj118<< F "Qccqz5(Q(Q(Q3*v;;V$$rM   c                L    | j         pt          d | j        D                       S )z@Is t a callable that we need to put a ... in for syntax reasons?c              3  f   K   | ],}|                                 p|                                V  -d S rj   )is_starrV  )r   ks     rN   r   z%is_tricky_callable.<locals>.<genexpr>+  s7      $V$VQQYY[[%@AJJLL$V$V$V$V$V$VrM   )is_ellipsis_argsr   rU   r,  s    rN   r  r  )  s)    V$V$V!+$V$V$V!V!VVrM   c                  `     e Zd ZdZd fd
ZddZddZddZddZd dZ	d! fdZ
d"dZ xZS )#r  zVisitor used to format typesr   r   r   r   r   r$   r]   r^   c                h    t                                          |           || _        || _        d S )N)r   )superrf   r   r   )re   r   r   r   	__class__s       rN   rf   zTypeFormatter.__init__2  s0    )))


rM   r,  r-   rC   c                "    |j         r|j         S dS NAny)missing_import_namer  s     rN   	visit_anyzTypeFormatter.visit_any7  s      	((5rM   r0   c                .   |j         j        p|j         j        pd }|dS |t          v rt          |         }t	          | j        |          }|sJ |\  }}| j        rD|                    d          }| j        | j                 j        }|r|d         |j	        v r| j        }||fdk    r)dd|j
        d                             |           z   dz   }}n(|j
        r!|d|                     |j
                   d	z  }|d
k    rdS |dk    r|S d|vrdnd}||z   |z   S )Nz<???>r`   r   )builtinstupletypingTuple[z, ...][])r  unicodeTextr  r   )r   rg   r>  r#   r@   r   r   r\  rj  r=  r   r   list_str)	re   r,  r5  mod_objr   r  partsrj  delims	            rN   visit_instancezTypeFormatter.visit_instance=  sO   FO2qv{2d97''''*Atz1--S
 ; 	"IIcNNE:dk*/D "aDJ..k:...AF1I,<,<T,B,B!BX!MCCV 	0/t}}QV,,////C---6JJc>>CCsE;$$rM   r3   c                    |j         r=|j         j        r1|j         j        j        }|dk    r|j                             |           S |                     |j                  }d| dS )Nzbuiltins.tupler  r  )partial_fallbackr   rg   r   r  r  )re   r,  fallback_namer5  s       rN   visit_tuple_typezTypeFormatter.visit_tuple_type^  sl     	7!"4"9 	7.3<M 000)00666MM!'""}}}rM   r;   c                    dS r  rL   r  s     rN   visit_uninhabited_typez$TypeFormatter.visit_uninhabited_typef  s    urM   r6   c                6    |j                             |           S rj   )fallbackr   r  s     rN   visit_typeddict_typez"TypeFormatter.visit_typeddict_typei  s    z  &&&rM   r<   c                    t          |j                  dk    r5t          |          r&dt          |                              |            dS t                                          |          S )N   z	Optional[r  )r  r  r>   r?   r   r  visit_union_type)re   r,  r  s     rN   r  zTypeFormatter.visit_union_typel  sc    qw<<1!4Q!7!7Aq1188>>AAAA77++A...rM   r.   c                     t          |          rd}n, fd|j        D             }dd                    |           d}d| d|j                                        dS )Nz...c                :    g | ]}|                               S rL   r   )r   r   re   s     rN   r   z5TypeFormatter.visit_callable_type.<locals>.<listcomp>{  s%    <<<CJJt$$<<<rM   r  rR  r  z	Callable[)r  rF   r   rE  r   )re   r,  arg_strr   s   `   rN   visit_callable_typez!TypeFormatter.visit_callable_typer  sz    a   	-GG =<<<<<<D,$))D//,,,G@7@@aj&7&7&=&=@@@@rM   )r   r   r   r   r   r$   r]   r^   )r,  r-   r]   rC   )r,  r0   r]   rC   )r,  r3   r]   rC   )r,  r;   r]   rC   )r,  r6   r]   rC   )r,  r<   r]   rC   )r,  r.   r]   rC   )rH   rI   rJ   rz   rf   r  r  r  r  r  r  r  __classcell__)r  s   @rN   r  r  .  s        &&     
   % % % %B      ' ' ' '/ / / / / /A A A A A A A ArM   r  TType)boundc                j    t          t          |                     t                                          S )zMake all anys in the type as coming from the suggestion engine.

    This keeps those Anys from influencing constraint generation,
    which allows us to do better when refining types.
    )r
   r  r   MakeSuggestionAnyr  s    rN   r   r     s'     qxx 1 3 344555rM   c                      e Zd Zd	dZd
dZdS )r  r,  r-   r]   r4   c                T    |j         s |                    t          j                  S |S )N)r   )r  r  r7   r   r  s     rN   r  zMakeSuggestionAny.visit_any  s)    $ 	??y/J?KKKHrM   r5   c                R     |                      fd|j        D                       S )Nc                :    g | ]}|                               S rL   r  )r   are   s     rN   r   z;MakeSuggestionAny.visit_type_alias_type.<locals>.<listcomp>  s#    $D$D$DQXXd^^$D$D$DrM   r   )r  r   r  s   ` rN   visit_type_alias_typez'MakeSuggestionAny.visit_type_alias_type  s.    $D$D$D$DQV$D$D$DEEErM   N)r,  r-   r]   r4   )r,  r5   r]   r4   )rH   rI   rJ   r  r  rL   rM   rN   r  r    sB           F F F F F FrM   r  r  c                X    t          |           }t          |           }||k    r|gS ||gS )zGenerate possible combinations of a list of types.

    mypy essentially supports two different ways to do this: joining the types
    and unioning the types. We try both.
    )r   r,   )r  joined_type
union_types      rN   r  r    s<     !''K&u--Jj  }Z((rM   msgsrE   rR   c                4    t          d | D                       S )Nc                    g | ]}d |v |	S )z error: rL   r  s     rN   r   z count_errors.<locals>.<listcomp>  s    333a:?????rM   )r  )r  s    rN   r3  r3    s    334333444rM   tisic                   t          |           }t          |          }t          |t                    r t          |t                    r	|j        r|n|S t          |t                    r]t          |t                    rH|j        |j        k    r8|                    d t          |j        |j                  D                       S t          |t                    rt          |t                    rr|j
        |j
        k    rbt          |j                  t          |j                  k    r8|                    d t          |j        |j                  D                       S t          |t                    r%t          |t                    rt          ||          S t          |t                    rt!          ||          S |S )a  Refine `ti` by replacing Anys in it with information taken from `si`

    This basically works by, when the types have the same structure,
    traversing both of them in parallel and replacing Any on the left
    with whatever the type on the right is. If the types don't have the
    same structure (or aren't supported), the left type is chosen.

    For example:
      refine(Any, T) = T,  for all T
      refine(float, int) = float
      refine(List[Any], List[int]) = List[int]
      refine(Dict[int, Any], Dict[Any, int]) = Dict[int, int]
      refine(Tuple[int, Any], Tuple[Any, int]) = Tuple[int, int]

      refine(Callable[[Any], Any], Callable[[int], int]) = Callable[[int], int]
      refine(Callable[..., int], Callable[[int, float], Any]) = Callable[[int, float], int]

      refine(Optional[Any], int) = Optional[int]
      refine(Optional[Any], Optional[int]) = Optional[int]
      refine(Optional[Any], Union[int, str]) = Optional[Union[int, str]]
      refine(Optional[List[Any]], List[int]) = List[int]

    c                4    g | ]\  }}t          ||          S rL   rF  r   tasas      rN   r   zrefine_type.<locals>.<listcomp>  s&    $[$[$[VR[R%8%8$[$[$[rM   r  c                4    g | ]\  }}t          ||          S rL   r  r   s      rN   r   zrefine_type.<locals>.<listcomp>  s&    %^%^%^fb"k"b&9&9%^%^%^rM   )r  )r=   r   r-   r  r0   r   r  rT  r   r3   r  r  r  r.   r  r<   refine_union)r  r  r,  r5  s       rN   rF  rF    s   0 	AA!W Lq'**Kq/DKqq!K!X ]:a#:#: ]qv?O?O$[$[s16STSYGZGZ$[$[$[\\\ 	1i  `q)$$` !"444LLCLL((%^%^AGUVU\H]H]%^%^%^___!\"" %z!\'B'B %q!$$$!Y "Aq!!! HrM   r<   r5  r2   c                x   | |k    r| S t          |t                    r|j        n|g}g }| j        D ]M}d}|D ]/}t          ||          }||k    r|                    |           d}0|s|                    |           Nt          j        d          5  t          |          cddd           S # 1 swxY w Y   dS )a7  Refine a union type based on another type.

    This is done by refining every component of the union against the
    right hand side type (or every component of its union if it is
    one). If an element of the union is successfully refined, we drop it
    from the union in favor of the refined versions.
    FTN)r   r<   r  rF  rv   r)   rH  r,   )r,  r5  	rhs_items	new_itemslhsrefinedrhsnews           rN   r  r    s)    	Avv%a33<!IIw " " 	 	Cc3''Cczz  %%% 	"S!!! 
	"4	(	( 0 0$Y//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0s   B//B36B3c                   | j         |j         k    r| S | j        r=t          |          s.|                    t	          | j        |j                            S t          |           s| j        |j        k    r| S |                     d t          | j        |j                  D             t	          | j        |j                            S )zKRefine a callable based on another.

    See comments for refine_type.
    rD  c                4    g | ]\  }}t          ||          S rL   r  r   s      rN   r   z#refine_callable.<locals>.<listcomp>  s&    SSS62r;r2&&SSSrM   )rF   rE  )	r  r  r  r  rF  rE  rU   rT  rF   )r,  r5  s     rN   r  r     s    
 	zQZ M"4Q"7"7 MAJ
(K(KLLL! q{ : :??SSSak5R5RSSSQZ44    rM   Tr   list[T]c                F    g }| D ]}||vr|                     |           |S rj   )rv   )r   r  r  s      rN   r   r     s4    C  C<<JJqMMMJrM   )r}   r~   r   r   r]   r   )r}   r~   r   r   r]   rY   )r   r-   r]   r   )r   r4   r]   r   )r  r4   r  r   r]   r  )r,  r.   r   r   r(  r   r]   r  )r,  r.   r]   r   )r,  r  r]   r  )r  r   r]   r   )r  rE   r]   rR   )r  r4   r  r4   r]   r4   )r,  r<   r5  r2   r]   r4   )r,  r.   r5  r.   r]   r.   )r   r  r]   r  )prz   
__future__r   r  r   rt  collections.abcr   
contextlibr   r  r   r   r   r	   r
   mypy.argmapr   
mypy.buildr   r   mypy.checkexprr   mypy.find_sourcesr   r   	mypy.joinr   	mypy.meetr   mypy.modulefinderr   
mypy.nodesr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   mypy.optionsr$   mypy.pluginr%   r&   r'   mypy.server.updater(   
mypy.stater)   mypy.traverserr*   mypy.typeopsr+   r,   
mypy.typesr-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   mypy.types_utilsr>   r?   	mypy.utilr@   rB   rP   r[   r|   r   r   r   	Exceptionr   r   r   r   r  r+  r  r  r  r   r  r  r3  rF  r  r  r  r   rL   rM   rN   <module>r&     s)   0 # " " " " "      				 $ $ $ $ $ $ % % % % % % A A A A A A A A A A A A A A . . . . . . # # # # # # # # ' ' ' ' ' ' = = = = = = = = $ $ $ $ $ $ $ $ $ $ $ $ / / / / / /                                 " !           > > > > > > > > > > 6 6 6 6 6 6       + + + + + + 9 9 9 9 9 9 9 9                                     & B A A A A A A A " " " " " "    )   
         z      #' #' #' #' #'v #' #' #'P    #       F F F F F# F F FBF F F F"	 	 	 	 		 	 	 	1 1 1 1A A A A
Y
 Y
 Y
 Y
 Y
 Y
 Y
 Y
x   .
% 
% 
% 
%W W W W
PA PA PA PA PAN PA PA PAf 	t$$$6 6 6 6F F F F F F F F) ) ) )5 5 5 52 2 2 2j0 0 0 0B   ( GCLL     rM   