Ë
    pPêj�˜  ã                  ó   — d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	 ddl
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 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/ 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;«      ZYd<d „ZZ G d!„ d"e;«      Z[d=d#„Z\ G d$„ d%e]«      Z^d>d&„Z_d?d'„Z`d@d(„Za G d)„ d*«      ZbdAd+„ZcdBd,„ZddCd-„Ze G d.„ d/eK«      Zf ed0eG¬1«      ZgdDd2„Zh G d3„ d4eL«      ZidEd5„ZjdFd6„ZkdGd7„ZldHd8„ZmdId9„Zn ed:«      ZodJd;„Zpy)Ka™  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)ÚCallableÚIterator)Úcontextmanager)Ú
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)Ú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<   y)ÚPyAnnotateSignatureÚstrÚreturn_typeú	list[str]Ú	arg_typesN©Ú__name__Ú
__module__Ú__qualname__Ú__annotations__© ó    úU/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/mypy/suggestions.pyrA   rA   W   s   … ØÓØÔrL   rA   c                  óJ   — e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   ded<   y)ÚCallsiterB   ÚpathÚintÚlineúlist[list[ArgKind]]Ú	arg_kindszlist[str | None]Úcallee_arg_namesúlist[list[str | None]]Ú	arg_namesúlist[list[Type]]rE   NrF   rK   rL   rM   rO   rO   \   s$   … Ø
ƒIØ
ƒIØ"Ó"Ø&Ó&Ø%Ó%ØÔrL   rO   c                  ó0   — e Zd ZdZdd„Zdd„Zd	d„Zd
d„Zy)ÚSuggestionPluginz0Plugin that records all calls to a given target.c                ól   — |j                  d«      r|j                  dd«      d   }|| _        g | _        y )N)z.__new__z	.__init__ú.é   r   )ÚendswithÚrsplitÚtargetÚmystery_hits)Úselfr`   s     rM   Ú__init__zSuggestionPlugin.__init__h   s5   € Ø�?‰?Ð4Ô5Ø—]‘] 3¨Ó*¨1Ñ-ˆFàˆŒð -/ˆÕrL   c                ó:   — || j                   k(  r| j                  S y ©N©r`   Úlog©rb   Úfullnames     rM   Úget_function_hookz"SuggestionPlugin.get_function_hookq   ó   € Ø�t—{‘{Ò"Ø—8‘8ˆOàrL   c                ó:   — || j                   k(  r| j                  S y re   rf   rh   s     rM   Úget_method_hookz SuggestionPlugin.get_method_hookw   rk   rL   c           
     ó  — | j                   j                  t        |j                  j                  |j
                  j                  |j                  |j                  |j                  |j                  «      «       |j                  S re   )ra   ÚappendrO   ÚapirP   ÚcontextrR   rT   rU   rW   rE   Údefault_return_type)rb   Úctxs     rM   rg   zSuggestionPlugin.log}   sb   € Ø×Ñ× Ñ ÜØ—‘—‘Ø—‘× Ñ Ø—‘Ø×$Ñ$Ø—‘Ø—‘óô		
ð ×&Ñ&Ð&rL   N)r`   rB   ÚreturnÚNone)ri   rB   rt   z(Callable[[FunctionContext], Type] | None)ri   rB   rt   z&Callable[[MethodContext], Type] | None)rs   zFunctionContext | MethodContextrt   r3   )rG   rH   rI   Ú__doc__rc   rj   rm   rg   rK   rL   rM   rZ   rZ   e   s   „ Ù:ó/óóô'rL   rZ   c                  ó(   — e Zd ZdZdd„Zdd„Zdd„Zy)	ÚReturnFinderz7Visitor for finding all types returned from a function.c                ó    — || _         g | _        y re   )ÚtypemapÚreturn_types)rb   rz   s     rM   rc   zReturnFinder.__init__�   s   € ØˆŒØ(*ˆÕrL   c                ó´   — |j                   �L|j                   | j                  v r3| j                  j                  | j                  |j                      «       y y y re   )Úexprrz   r{   ro   ©rb   Úos     rM   Úvisit_return_stmtzReturnFinder.visit_return_stmt”   sD   € Ø�6‰6Ð !§&¡&¨D¯L©LÑ"8Ø×Ñ×$Ñ$ T§\¡\°!·&±&Ñ%9Õ:ð #9ÐrL   c                 ó   — y re   rK   r~   s     rM   Úvisit_func_defzReturnFinder.visit_func_def˜   s   € àrL   N)rz   údict[Expression, Type]rt   ru   )r   r   rt   ru   )r   r   rt   ru   )rG   rH   rI   rv   rc   r€   r‚   rK   rL   rM   rx   rx   �   s   „ ÙAó+ó;ôrL   rx   c                óf   — t        | «      }|j                  j                  |«       |j                  S )z9Find all the types returned by return statements in func.)rx   ÚbodyÚacceptr{   )rz   ÚfuncÚfinders      rM   Úget_return_typesr‰   �   s*   € ä˜'Ó"€FØ‡I�I×Ñ�VÔØ×ÑÐrL   c                  ó    — e Zd ZdZdd„Zdd„Zy)ÚArgUseFinderz”Visitor for finding all the types of arguments that each arg is passed to.

    This is extremely simple minded but might be effective anyways.
    c                óp   — || _         |j                  D �ci c]  }|j                  g “Œ c}| _        y c c}w re   )rz   Ú	argumentsÚvariablerE   )rb   r‡   rz   Úargs       rM   rc   zArgUseFinder.__init__ª   s-   € ØˆŒØTX×TbÑTbÖ7cÈS¸¿¹ÀbÑ8HÒ7cˆ�ùÒ7cs   –3c                óB  ‡ — t        ˆ fd„|j                  D «       «      sy t        ‰ j                  j	                  |j
                  «      «      }t        |t        «      sy t        |j                  |j                  |j                  |j                  d„ «      }t        |«      D ]z  \  }}|D ]p  }|j                  |   }t        |t        «      sŒ#|j                  ‰ j                  v sŒ<‰ j                  |j                     j                  |j                  |   «       Œr Œ| y )Nc              3  ór   •K  — | ].  }t        |t        «      xr |j                  ‰j                  v –— Œ0 y ­wre   )Ú
isinstancer   ÚnoderE   )Ú.0Úerb   s     €rM   ú	<genexpr>z/ArgUseFinder.visit_call_expr.<locals>.<genexpr>¯   s-   øè ø€ ÒWÈ1”:˜a¤Ó)ÒF¨a¯f©f¸¿¹Ð.FÓFÑWùs   ƒ47c                ó4   — t        t        j                  «      S re   )r,   r6   Úspecial_form)Úns    rM   ú<lambda>z.ArgUseFinder.visit_call_expr.<locals>.<lambda>»   s   € ”gœi×4Ñ4Ó5€ rL   )ÚanyÚargsr<   rz   ÚgetÚcalleer’   r-   r   rT   rW   Ú	enumerater   r“   rE   ro   )rb   r   ÚtypÚformal_to_actualÚirœ   Úarg_idxr�   s   `       rM   Úvisit_call_exprzArgUseFinder.visit_call_expr®   sç   ø€ ÜÓWÐPQ×PVÑPVÔWÔWØä˜dŸl™l×.Ñ.¨q¯x©xÓ8Ó9ˆÜ˜#œ|Ô,Øä1Ø�K‰KØ�K‰KØ�M‰MØ�M‰MÙ5ó
Ðô !Ð!1Ó2ò 	F‰GˆAˆtØò F�Ø—f‘f˜W‘o�Ü˜c¤7Õ+°·±¸D¿N¹NÒ0JØ—N‘N 3§8¡8Ñ,×3Ñ3°C·M±MÀ!Ñ4DÕEñFñ	FrL   N)r‡   r   rz   rƒ   rt   ru   )r   r   rt   ru   )rG   rH   rI   rv   rc   r¤   rK   rL   rM   r‹   r‹   ¤   s   „ ñó
dôFrL   r‹   c                ó¼   — t        || «      }|j                  j                  |«       |j                  D �cg c]  }|j                  |j
                     ‘Œ c}S c c}w )züFind 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]].
    )r‹   r…   r†   r�   rE   rŽ   )rz   r‡   rˆ   r�   s       rM   Úget_arg_usesr¦   Å   sI   € ô ˜$ Ó(€FØ‡I�I×Ñ�VÔØ6:·n±nÖE¨sˆF×Ñ˜SŸ\™\Ó*ÒEÐEùÒEs   ¶ Ac                  ó   — e Zd Zy)ÚSuggestionFailureN)rG   rH   rI   rK   rL   rM   r¨   r¨   Ö   s   „ ØrL   r¨   c                ó<   — | j                   t        j                  k(  S re   )Útype_of_anyr6   Úexplicit©r    s    rM   Úis_explicit_anyr­   Ú   s   € ð �?‰?œi×0Ñ0Ñ0Ð0rL   c                óT   — t        | «      } t        | t        «      xr t        | «       S re   )r<   r’   r,   r­   r¬   s    rM   Úis_implicit_anyr¯   ä   s&   € Ü
˜#Ó
€CÜ�cœ7Ó#Ò@¬O¸CÓ,@Ð(@Ð@rL   c                óÀ   — t        | t        t        f«      xsG t        | t        «      xr5 | j                  j
                  xr | j                  j                  d«      d uS )NÚ__call__)r’   r9   r-   r/   ÚtypeÚis_protocolÚ
get_methodr¬   s    rM   Ú_arg_accepts_functionrµ   é   sW   € ô 	�3œ¤lÐ3Ó4ò 	8ô 	�3œÓ!ò 	8Ø�H‰H× Ñ ò	8à�H‰H×Ñ 
Ó+°4Ð7ðrL   c                  ó¶  — e Zd ZdZddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#d„Zd$d„Zd$d„Zed%d„«       Zed&d	„«       Z	d'd
„Z
d'd„Z	 	 	 	 	 	 	 	 	 	 	 	 d(d„Zd)d„Z	 	 	 	 	 	 	 	 	 	 	 	 d*d„Zd+d„Z	 	 	 	 	 	 	 	 d,d„Zd-d„Zd.d„Zd/d„Z	 	 	 	 	 	 	 	 d0d„Zd1d„Zd2d„Zd3d„Zd4d„Zd5d„Zd6d„Zd7d8d„Zd9d„Z	 	 	 	 	 	 	 	 	 	 d:d„Z	 	 	 	 	 	 	 	 d;d„Zd<d„Zd=d „Z d>d!„Z!d?d"„Z"y)@ÚSuggestionEnginez8Engine for finding call sites and suggesting signatures.FN)Ú	no_errorsÚno_anyÚflex_anyÚ	use_fixmeÚmax_guessesc               ó^  — || _         |j                  | _        | j                  j                  | _        |j                  | _        t	        | j                  j
                  | j                  j                  «      | _        || _        || _	        || _
        |rd| _
        |xs d| _        || _        y )Nç      ð?é@   )Ú	fgmanagerÚmanagerÚpluginÚgraphr   ÚfscacheÚoptionsrˆ   Ú	give_jsonr¸   rº   r¼   r»   )rb   rÀ   Újsonr¸   r¹   rº   r»   r¼   s           rM   rc   zSuggestionEngine.__init__ø   s‰   € ð #ˆŒØ ×(Ñ(ˆŒØ—l‘l×)Ñ)ˆŒØ—_‘_ˆŒ
Ü" 4§<¡<×#7Ñ#7¸¿¹×9MÑ9MÓNˆŒàˆŒØ"ˆŒØ ˆŒÙØˆDŒMà&Ò,¨"ˆÔØ"ˆ�rL   c                óH  — | j                  |«      \  }}}| j                  |«      5  | j                  «       5  | j                  ||«      }ddd«       ddd«       | j                  r| j                  |||«      S | j                  «      S # 1 sw Y   ŒBxY w# 1 sw Y   ŒFxY w)z&Suggest an inferred type for function.N)Ú	find_nodeÚrestore_afterÚwith_export_typesÚget_suggestionrÆ   Újson_suggestionÚformat_signature)rb   ÚfunctionÚmodÚ	func_namer“   Ú
suggestions         rM   ÚsuggestzSuggestionEngine.suggest  sž   € à#Ÿ~™~¨hÓ7ÑˆˆY˜à×Ñ Ó$ñ 	<Ø×'Ñ'Ó)ñ <Ø!×0Ñ0°°dÓ;�
÷<÷	<ð �>Š>Ø×'Ñ'¨¨Y¸¸jÓIÐIà×(Ñ(¨Ó4Ð4÷<ð <ú÷	<ð 	<ús"   §B¸BÁBÂB	ÂBÂB!c                óX  — | j                  |«      \  }}}| j                  |«      5  | j                  |«      \  }}ddd«       dj                  t	        D �����	�
cg c]$  \  }}}}}	}
|› d|› d| j                  ||	|
«      › �‘Œ& c}
}	}}}}«      «      S # 1 sw Y   Œ[xY wc c}
}	}}}}w )z&Find a list of call sites of function.Nú
ú:z: )rÉ   rÊ   Úget_callsitesÚjoinÚdedupÚformat_args)rb   rÏ   rÐ   Ú_r“   Ú	callsitesrP   rR   rT   rW   rE   s              rM   Úsuggest_callsitesz"SuggestionEngine.suggest_callsites  s¸   € à—~‘~ hÓ/‰ˆˆQ�Ø×Ñ Ó$ñ 	4Ø×-Ñ-¨dÓ3‰LˆI�q÷	4ð �y‰yÜð KT÷ó áF˜˜d I¨q°)¸Yð �f˜A˜d˜V 2 d×&6Ñ&6°yÀ)ÈYÓ&WÐ%XÒY÷óó
ð 	
÷	4ð 	4ü÷
s   §BÁ)B"ÂBc              #  ó˜   K  — 	 d–— | j                  | j                  |   «       y# | j                  | j                  |   «       w xY w­w)zœContext manager that reloads a module after executing the body.

        This should undo any damage done to the module state while mucking around.
        N)ÚreloadrÃ   )rb   Úmodules     rM   rÊ   zSuggestionEngine.restore_after.  s8   è ø€ ð	,Ûà�K‰K˜Ÿ
™
 6Ñ*Õ+øˆD�K‰K˜Ÿ
™
 6Ñ*Õ+üs   ‚A
„' ˆA
§ AÁA
c              #  ó  K  — | j                   j                  j                  }d| j                   j                  _        	 d–— || j                   j                  _        y# || j                   j                  _        w xY w­w)z¤Context 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)rb   Úolds     rM   rË   z"SuggestionEngine.with_export_types9  s[   è ø€ ð �l‰l×"Ñ"×/Ñ/ˆØ,0ˆ�‰×ÑÔ)ð	4Ûà03ˆD�L‰L× Ñ Õ-ø°ˆD�L‰L× Ñ Õ-üs   ‚<A?¿A ÁA?ÁA<Á<A?c           	     óü   — t        |j                  D �cg c]  }t        t        j                  «      ‘Œ c}|j                  |j
                  t        t        j                  «      | j                  d«      «      S c c}w )z?Generate a trivial callable type from a func def, with all Anyszbuiltins.function)r-   rT   r,   r6   Úsuggestion_enginerW   Ú
named_type)rb   ÚfdefrÛ   s      rM   Úget_trivial_typez!SuggestionEngine.get_trivial_typeF  sZ   € ô
 Ø;?¿>¹>ÖJ°aŒW”Y×0Ñ0Õ1ÒJØ�N‰NØ�N‰NÜ”I×/Ñ/Ó0Ø�O‰OÐ/Ó0ó
ð 	
ùÚJs   ” A9c                ó‚   — t        |j                  t        «      rt        |j                  «      S | j	                  |«      S re   )r’   r²   r-   Úmake_suggestion_anysrè   )rb   rç   s     rM   Úget_starting_typez"SuggestionEngine.get_starting_typeS  s0   € Ü�d—i‘i¤Ô.Ü'¨¯	©	Ó2Ð2à×(Ñ(¨Ó.Ð.rL   c           	     ój  — g }t        t        |j                  «      «      D �]Ž  }|dk(  r,|r*|j                  t	        t
        j                  «      g«       Œ5g }|D ]6  }	|	j                  ||z
     D ]  }
t        |
«      rŒ|j                  |
«       Œ! Œ8 g }||   D ]  }
t        |
«      rŒ|j                  |
«       Œ! ||   }|r$|j                  |«       |r|j                  |«       g }|rSt        d„ |D «       «      rA|j                  t        j                  |d   t	        t
        j                  «      g«      «       nE|r|j                  t        |«      «       n(|j                  t	        t
        j                  «      «       |r|j                  t        |«      «       |j                  |«       �Œ‘ |S )z:Produce a list of type suggestions for each argument type.r   c              3  óN   K  — | ]  }t        t        |«      t        «      –— Œ y ­wre   )r’   r<   r0   )r”   Útps     rM   r–   z,SuggestionEngine.get_args.<locals>.<genexpr>}  s!   è ø€ ò %Ø>@”
œ?¨2Ó.´×9ñ%ùó   ‚#%)ÚrangeÚlenrT   ro   r,   r6   rå   rE   r¯   Úallr;   Ú
make_unionr«   ÚextendÚgenerate_type_combinationsr   )rb   Ú	is_methodÚbaseÚdefaultsrÜ   ÚusesÚtypesr¢   Úall_arg_typesÚcallr    Úall_use_typesÚdefaultrE   s                 rM   Úget_argszSuggestionEngine.get_argsY  s›  € ð #%ˆÜ”s˜4Ÿ>™>Ó*Ó+ó )	$ˆAà�AŠv™)Ø—‘œg¤i×&AÑ&AÓBÐCÔDØàˆMØ!ò 2�ØŸ>™>¨!¨i©-Ñ8ò 2�Cä*¨3Õ/Ø%×,Ñ,¨SÕ1ñ2ð2ð
 ˆMØ˜A‘wò .�ä& sÕ+Ø!×(Ñ(¨Õ-ð.ð
 ˜q‘kˆGÙØ×$Ñ$ WÔ-Ù Ø!×(Ñ(¨Ô1àˆIá¤ñ %ØDQô%ô "ð × Ñ Ü×(Ñ(¨-¸Ñ*:¼GÄI×DVÑDVÓ<WÐ)XÓYõñ Ø× Ñ Ô!;¸MÓ!JÕKà× Ñ ¤¬×);Ñ);Ó!<Ô=áà× Ñ ¤°Ó!>Ô?à�L‰L˜Ö#ðS)	$ðT ˆrL   c                óž   — |j                   D �cg c]3  }|j                  r#| j                  j                  |j                     nd ‘Œ5 c}S c c}w re   )r�   ÚinitializerrÁ   Ú	all_types)rb   rç   r�   s      rM   Úget_default_arg_typesz&SuggestionEngine.get_default_arg_types�  sF   € ð —~‘~ö
àð 8;·²ˆD�L‰L×"Ñ" 3§?¡?Ò3ÈDÑPò
ð 	
ùò 
s   �8A
c                óþ   — | j                  |||||«      }t        j                  t        j                  |Ž d| j                  «      }|D �cg c]'  }t        ||j                  t        |«      ¬«      «      ‘Œ) c}S c c}w )z˜Compute 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   )rE   )rÿ   Ú	itertoolsÚisliceÚproductr¼   Úrefine_callableÚcopy_modifiedÚlist)	rb   rö   r÷   rø   rÜ   rù   rÅ   r  Úxs	            rM   Úget_guesseszSuggestionEngine.get_guesses•  so   € ð —-‘- 	¨4°¸9ÀdÓKˆô ×"Ñ"¤9×#4Ñ#4°gÐ#>ÀÀ4×CSÑCSÓTˆØV]Ö^ÐQR”  d×&8Ñ&8Ä4ÈÃ7Ð&8Ó&KÕLÒ^Ð^ùÒ^s   Á,A:c                óz  — | j                  |«      }t        |j                  «      }| j                  j                  j                  d|«       	 | j                  ||«      }| j                  j                  j                  d«       |j                  |fS # | j                  j                  j                  d«       w xY w)z"Find all call sites of a function.r   )	rë   rZ   ri   rÂ   Ú_pluginsÚinsertÚtry_typeÚpopra   )rb   r‡   Únew_typeÚcollector_pluginÚerrorss        rM   r×   zSuggestionEngine.get_callsites§  s–   € à×)Ñ)¨$Ó/ˆä+¨D¯M©MÓ:Ðà�‰×Ñ×#Ñ# AÐ'7Ô8ð	(Ø—]‘] 4¨Ó2ˆFà�K‰K× Ñ ×$Ñ$ QÔ'à×,Ñ,¨fÐ4Ð4øð �K‰K× Ñ ×$Ñ$ QÕ'ús   ÁB Â'B:c                óx   — |D �cg c]*  }| j                   �t        |||«      | j                   k\  r|‘Œ, c}S c c}w )z~Apply any configured filters to the possible guesses.

        Currently the only option is filtering based on Any prevalance.)rº   Úany_score_callable)rb   Úguessesrö   Úignore_returnÚts        rM   Úfilter_optionszSuggestionEngine.filter_optionsµ  sC   € ð ö
àØ�}‰}Ð$Ü! ! Y°Ó>À$Ç-Á-ÒOò ò
ð 	
ùò 
s   …/7c                ó®   ‡ ‡— |st        d«      ‚|D �ci c]  }|‰ j                  ||«      “Œ c}Št        |ˆˆ fd„¬«      }|t        ‰|   «      fS c c}w )zœFrom 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                óB   •— t        ‰|    «      ‰j                  | «      fS re   )Úcount_errorsÚscore_callable)Úsr  rb   s    €€rM   rš   z,SuggestionEngine.find_best.<locals>.<lambda>Ê  s!   ø€ ¬<¸¸q¹	Ó+BÀD×DWÑDWÐXYÓDZÐ*[€ rL   )Úkey)r¨   r  Úminr  )rb   r‡   r  ÚguessÚbestr  s   `    @rM   Ú	find_bestzSuggestionEngine.find_bestÂ  s[   ù€ ñ
 Ü#Ð$EÓFÐFØAHÖI¸�%˜Ÿ™ t¨UÓ3Ñ3ÒIˆÜ�7Ô [Ô\ˆØ”\ &¨¡,Ó/Ð/Ð/ùò Js   ”Ac                ó°  — |j                   sg S |j                   j                  dd D ]«  }|j                  j                  |j                  «      }|sŒ+t        |j                  t        t        f«      sŒLt        |j                  j                  «      }t        |t        «      sŒ|t        |j                  «      t        |j                  «      k(  sŒ¨|gc S  g S )z8Try to get a guess of a method type from a parent class.r]   N)ÚinfoÚmroÚnamesr�   Únamer’   r“   r   r   r<   r²   r-   rñ   rE   r�   )rb   r“   ÚparentÚpnoder    s        rM   Úget_guesses_from_parentz(SuggestionEngine.get_guesses_from_parentÍ  sž   € à�yŠyØˆIà—i‘i—m‘m A BÐ'ò 	!ˆFØ—L‘L×$Ñ$ T§Y¡YÓ/ˆEÚœ E§J¡J´¼)Ð0DÕEÜ% e§j¡j§o¡oÓ6�ä˜c¤<Õ0´S¸¿¹Ó5GÌ3ÈtÏ~É~ÓK^Ó5^ð  ˜5’Lð	!ð ˆ	rL   c           	     ó�  — | j                   }| j                  |«      \  }}t        | j                  j                  |«      }| j
                  r|rt        d«      ‚t        |j                  «      xr |j                  }t        j                  ||   j                  j                  «      5  | j                  || j                  |«      | j!                  |«      ||«      }ddd«       | j#                  |«      z  }| j%                  ||d¬«      }| j'                  ||«      \  }	}
| j)                  ||	«       t+        | j                  j                  |«      }t        j                  ||   j                  j                  «      5  |rt-        |«      }nt/        «       g}ddd«       D �cg c](  }|	j1                  t3        |	j4                  |«      ¬«      ‘Œ* }}| j%                  ||d¬«      }| j'                  ||«      \  }	}| j
                  r|rt        d«      ‚| j7                  |||	«      S # 1 sw Y   �ŒOxY w# 1 sw Y   ŒžxY wc c}w )zxCompute a suggestion for a function.

        Return the type and whether the first argument should be ignored.
        zFunction does not typecheck.NT)r  ©Úret_typeFzNo annotation without errors)rÃ   r×   r¦   rÁ   r  r¸   r¨   Úboolr&  Úhas_self_or_cls_argumentr(   Ústrict_optional_setrÅ   Ústrict_optionalr  rë   r  r,  r  r$  r  r‰   rõ   r0   r	  Úrefine_typer/  Úpyannotate_signature)rb   rÐ   r“   rÃ   rÜ   Úorig_errorsrù   rö   r  r#  rÛ   ÚreturnsÚ	ret_typesr  r  s                  rM   rÌ   zSuggestionEngine.get_suggestionÞ  s	  € ð
 —
‘
ˆØ!%×!3Ñ!3°DÓ!9Ñˆ	�;Ü˜DŸL™L×2Ñ2°DÓ9ˆà�>Š>™kÜ#Ð$BÓCÐCä˜Ÿ™“OÒE¨×(EÑ(Eˆ	ä×&Ñ& u¨S¡z×'9Ñ'9×'IÑ'IÓJñ 	Ø×&Ñ&ØØ×&Ñ& tÓ,Ø×*Ñ*¨4Ó0ØØóˆG÷	ð 	�4×/Ñ/°Ó5Ñ5ˆØ×%Ñ% g¨yÈÐ%ÓMˆØ—.‘.  wÓ/‰ˆˆað 	�‰�d˜DÔ!Ü" 4§<¡<×#9Ñ#9¸4Ó@ˆÜ×&Ñ& u¨S¡z×'9Ñ'9×'IÑ'IÓJñ 	)ÙÜ6°wÓ?‘	ä%›Z˜L�	÷		)ð XaÖaÐRS�4×%Ñ%¬{¸4¿=¹=È!Ó/LÐ%ÕMÐaˆÐaØ×%Ñ% g¨yÈÐ%ÓNˆØ—~‘~ d¨GÓ4‰ˆˆfà�>Š>™fÜ#Ð$BÓCÐCà×(Ñ(¨¨i¸Ó>Ð>÷9	ñ 	ú÷	)ð 	)üò bs   Â)4H*Æ H7Æ&-IÈ*H4È7I c                óX  — g }t        t        |«      «      D ]}  }t        ||   ||   ||   «      D ]Q  \  }}}| j                  d |«      }	|t        k(  rd|	z   }	Œ(|t
        k(  rd|	z   }	Œ7|j                  «       sŒH|sŒK|› d|	› �}	ŒS |j                  	«       Œ ddj                  |«      › d�S )NÚ*z**ú=ú(ú, ú))	rð   rñ   ÚzipÚformat_typer   r   Úis_namedro   rØ   )
rb   rT   rW   rE   rœ   r¢   Úkindr)  r    r�   s
             rM   rÚ   zSuggestionEngine.format_args
  sÂ   € ð ˆÜ”s˜9“~Ó&ò 
	ˆAÜ#& y°¡|°Y¸q±\À9ÈQÁ<Ó#Pò .‘��d˜CØ×&Ñ& t¨SÓ1�Øœ8Ò#Ø ™)‘CØœYÒ&Ø ™*‘CØ—]‘]•_ÚØ!%  a¨ u˜o™ð.ð �K‰K˜Õð
	ð �4—9‘9˜T“?Ð# 1Ð%Ð%rL   c                ó°  — d}d|v r´t         j                  dk(  rdnd}|j                  d«      |kD  rt        dj	                  |«      «      ‚|j                  dd«      \  }}|j                  «       st        d|› �«      ‚t        |«      }| j                  ||«      \  }}|j                  t        |«      dz   d }nGt        | j                  j                  |«      }	|	st        d|› �«      ‚|	\  }}| j                  ||«      }t        |t         «      r"| j#                  |«      }|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Ö   Úwin32é   r]   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)ÚsysÚplatformÚcountr¨   Úformatr_   ÚisdigitrQ   Úfind_node_by_file_and_lineri   rñ   r?   rÀ   rÃ   Úfind_node_by_module_and_namer’   r   Úextract_from_decoratorr   )
rb   r   r“   Úplatform_key_countÚfilerR   Úline_numberÚmodnameÚtailr`   s
             rM   rÉ   zSuggestionEngine.find_node  sc  € ð #'ˆØ�#‰:ô
 '*§l¡l°gÒ&=¡À1ÐØ�y‰y˜‹~Ð 2Ò2Ü'ðKßKQÉ6ÐRUË;óð ð Ÿ™ C¨Ó+‰JˆD�$Ø—<‘<”>Ü'Ð*LÈTÈFÐ(SÓTÐTÜ˜d›)ˆKØ ×;Ñ;¸DÀ+ÓN‰MˆG�TØ—=‘=¤ W£°Ñ!1Ð!3Ð4‰Dä! $§.¡.×"6Ñ"6¸Ó<ˆFÙÜ'Ð*AÀ#ÀÐ(GÓHÐHØ"‰MˆG�TØ×4Ñ4°W¸dÓCˆDä�dœIÔ&Ø×.Ñ.¨tÓ4ˆDÙÜ'¨'°#°Ð6UÐ(VÓWÐWä˜$¤Ô(Ü# g¨c¨UÐ2DÐ$EÓFÐFà˜˜dÐ"Ð"rL   c                ó~  — | j                  | j                  j                  |   «      }|j                  }|j	                  d«      }t        |dd «      D ]”  \  }}||vr0t        dj                  |dj                  |d|dz    «      «      «      ‚||   j                  }t        |t        «      s0t        dj                  |dj                  |d|dz    «      «      «      ‚|j                  }Œ– |d   }	|	|vr<|dz   |z   }
t        dj                  t        |«      dkD  rd|
«      «      ‚d	|
«      «      ‚||	   j                  S )
znFind symbol node by module id and qualified name.

        Raise SuggestionFailure if can't find one.
        r\   NéÿÿÿÿzUnknown class {}.{}r]   zObject {}.{} is not a classzUnknown {} {}ÚmethodrÏ   )Úensure_loadedrÀ   rÃ   r(  ÚsplitrŸ   r¨   rI  rØ   r“   r’   r!   rñ   )rb   rQ  rR  Útreer(  Ú
componentsr¢   Ú	componentr“   Úfuncnamer   s              rM   rL  z-SuggestionEngine.find_node_by_module_and_nameJ  sb  € ð
 ×!Ñ! $§.¡.×"6Ñ"6°wÑ"?Ó@ˆð
 "ŸZ™Zˆð —Z‘Z “_ˆ
Ü% j°°" oÓ6ò 
	‰LˆAˆyØ Ñ%Ü'Ø)×0Ñ0°¸#¿(¹(À:ÈgÐPQÐTUÑPUÐCVÓ:WÓXóð ð ',¨IÑ&6×&;Ñ&;ˆDÜ˜d¤HÔ-Ü'Ø1×8Ñ8¸À#Ç(Á(È:ÐV]ÐXYÐ\]ÑX]ÐK^ÓB_Ó`óð ð —J‘J‰Eð
	ð ˜b‘>ˆØ˜5Ñ Ø˜C‘- $Ñ&ˆCÜ#Ø×&Ñ&´3°z³?ÀQÒ3F xÐX[Ó\óð ØLVÐX[Ó\óð ð �X‰×#Ñ#Ð#rL   c                ó˜  ‡— t        ˆfd„t        D «       «      st        d«      ‚	 | j                  j	                  t
        j                  j                  ‰«      «      \  }}|| j                  vrt        d|z   «      ‚| j                  | j                  j                  |   d¬«      }d}d}|j                  «       D ]Z  \  }}	}t        |	j                  t        t         f«      r|	j                  j"                  }
nŒ?|
|k  sŒE|�|
|kD  sŒM|
}|	j                  }Œ\ |st        d|› �«      ‚||fS # t        $ r}t        d‰z   «      |‚d}~ww xY w)	zÝFind 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  ó@   •K  — | ]  }‰j                  |«      –— Œ y ­wre   )r^   )r”   ÚextrO  s     €rM   r–   z>SuggestionEngine.find_node_by_file_and_line.<locals>.<genexpr>t  s   øè ø€ ÒC¨#�4—=‘= ×%ÑCùs   ƒ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ÚosrP   Únormpathr   rÃ   rV  rÀ   Úlocal_definitionsr’   r“   r   r   rR   )rb   rO  rR   rQ  rÛ   r•   rX  r“   Úclosest_lineÚsymÚsym_lines    `         rM   rK  z+SuggestionEngine.find_node_by_file_and_linem  sJ  ø€ ô ÓCÔ1BÔCÔCÜ#Ð$FÓGÐGð	PØŸ™×-Ñ-¬b¯g©g×.>Ñ.>¸tÓ.DÓE‰JˆG�Qð ˜$Ÿ*™*Ñ$Ü#Ð$6¸Ñ$@ÓAÐAà×!Ñ! $§.¡.×"6Ñ"6°wÑ"?ÀtÐ!ÓLˆØ"&ˆØ#'ˆà×/Ñ/Ó1ò 
	 ‰IˆAˆs�AÜ˜#Ÿ(™(¤W¬iÐ$8Ô9ØŸ8™8Ÿ=™=‘ð ð ˜4Ó \Ð%9¸XÈÓ=TØ'�Ø—x‘x‘ð
	 ñ Ü#Ð&EÀdÀVÐ$LÓMÐMØ˜ˆ}Ðøô- !ò 	PÜ#Ð$@À4Ñ$GÓHÈaÐOûð	Pús   ¦;D, Ä,	E	Ä5EÅE	c                óþ  — |j                   D �]â  }d }t        |t        «      r@t        |j                  t        t
        f«      r t        |j                  j                  «      }n§t        |t        «      r—t        |j                  t        «      r}t        |j                  j                  t        t
        t        f«      rNt        t        |j                  j                  j                  «      x}t        «      rt        |j                  «      }t        |t        «      r[|j                  j                  d«      }t        |t
        «      r0t        |j                  t        «      rt!        |j                  d «      }t        |t        «      s y |j"                  D ]Z  }t%        |j&                  «      dk(  r>t)        t        |j&                  d   «      «      r|j&                  d   |j                  k(  rŒY  y  �Œå |j*                  S )Nr±   r]   r   )Ú
decoratorsr’   r   r“   r"   r   r<   r²   r   rž   r   r-   r/  r/   r´   r.   r*   Úitemsrñ   rE   rµ   r‡   )rb   r“   Údecr    Úcall_tpÚcall_methodÚcts          rM   rM  z'SuggestionEngine.extract_from_decorator�  s^  € Ø—?‘?ó 	 ˆCØˆCÜ˜#œwÔ'¬J°s·x±xÄ#ÄwÀÔ,PÜ% c§h¡h§m¡mÓ4‘ä˜3¤Ô)Ü˜sŸz™z¬7Ô3Ü˜sŸz™zŸ™´¼GÄSÐ0IÔJÜ¬?¸3¿:¹:¿?¹?×;OÑ;OÓ+PÐ P ÔS_Ô`ä% g×&6Ñ&6Ó7�ä˜#œxÔ(Ø!Ÿh™h×1Ñ1°*Ó=�Ü˜k¬7Ô3¼
À;×CSÑCSÔUaÔ8bÜ# K×$4Ñ$4°dÓ;�Cä˜c¤<Ô0ÙØ—i‘iò  �ä˜Ÿ™Ó%¨Ò*Ü-¬o¸b¿l¹lÈ1¹oÓ.NÔOØŸ™ Q™¨2¯;©;Ó6âò ð'	 ð6 �y‰yÐrL   c                ó¬   — |j                   }d|_        ||_         	 | j                  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À   Útriggerri   )rb   r‡   r    rã   Úress        rM   r  zSuggestionEngine.try_type®  sS   € ð
 ×"Ñ"ˆð ˆŒ	Ø"ˆÔð	'Ø—.‘.×(Ñ(¨¯©Ó7ˆCð à#&ˆDÕ ø 3ˆDÕ ús   œ&A
 Á
	Ac                ó¸   — |j                   €J ‚| j                  j                  «        | j                  j                  |j                  |j                   fgg «      S )z"Recheck the module given by state.)rP   rÀ   Úflush_cacheÚupdateÚid)rb   r(   s     rM   rß   zSuggestionEngine.reloadÂ  sI   € à�z‰zÐ%Ð%Ð%Ø�‰×"Ñ"Ô$Ø�~‰~×$Ñ$ u§x¡x°·±Ð&<Ð%=¸rÓBÐBrL   c                ó    — |j                   r|j                   j                  s|r| j                  |«       |j                   €J ‚|j                   S )z?Make sure that the module represented by state is fully loaded.)rX  Úis_cache_skeletonrß   )rb   r(   r_  s      rM   rV  zSuggestionEngine.ensure_loadedÈ  s=   € à�zŠz˜UŸZ™Z×9Ò9¹UØ�K‰K˜ÔØ�z‰zÐ%Ð%Ð%Ø�z‰zÐrL   c                óL   — | j                   j                  j                  |«      S re   )rÁ   Úsemantic_analyzerræ   )rb   r  s     rM   ræ   zSuggestionEngine.named_typeÏ  s   € Ø�|‰|×-Ñ-×8Ñ8¸Ó;Ð;rL   c                ó  — |j                   s|j                  r|j                  dd«      d   }t        j                  j                  | j                  |   j                  «      }||j                  ||ddœ}t        j                  |gd¬«      S )zLProduce a json blob for a suggestion suitable for application by pyannotate.r\   r]   rT  r   )Ú	signaturerR   rP   rÑ   ÚsamplesT)Ú	sort_keys)Úis_classÚ	is_staticrW  ra  rP   ÚabspathrÃ   ÚxpathrR   rÇ   Údumps)rb   rÐ   rÑ   r“   rÒ   rP   Úobjs          rM   rÍ   z SuggestionEngine.json_suggestionÒ  sy   € ð
 �=Š=˜DŸNšNØ!Ÿ™¨¨QÓ/°Ñ3ˆIô
 �w‰w�‰˜tŸz™z¨#™×4Ñ4Ó5ˆð $Ø—I‘IØØ"Øñ
ˆô �z‰z˜3˜%¨4Ô0Ð0rL   c                ó¶   — t        |«      }|j                  |d D �cg c]  }| j                  ||«      ‘Œ c}| j                  ||j                  «      dœS c c}w )z+Format a callable type as a pyannotate dictN)rE   rC   )rQ   rE   r@  r/  )rb   Ú
cur_modulerö   r    Ústartr  s         rM   r5  z%SuggestionEngine.pyannotate_signatureè  sV   € ô �I“ˆàCFÇ=Á=ÐQVÐQWÐCXÖY¸a˜$×*Ñ*¨:°qÕ9ÒYØ×+Ñ+¨J¸¿¹ÓEñ
ð 	
ùÚYs   �Ac                ó<   — ddj                  |d   «      › d|d   › �S )zDFormat a callable type in a way suitable as an annotation... kind ofr<  r=  rE   z) -> rC   )rØ   )rb   Úsigs     rM   rÎ   z!SuggestionEngine.format_signatureò  s*   € à�4—9‘9˜S Ñ-Ó.Ð/¨u°S¸Ñ5GÐ4HÐIÐIrL   c                óØ   — | j                   r%t        t        |«      t        «      r| j                   S |j	                  t        || j                  | j                  j                  «      «      S re   )	r»   r’   r<   r,   r†   ÚTypeFormatterrÃ   rÁ   rÅ   )rb   r…  r    s      rM   r@  zSuggestionEngine.format_typeö  sG   € Ø�>Š>œj¬¸Ó)=¼wÔGØ—>‘>Ð!Ø�z‰zœ-¨
°D·J±JÀÇÁ×@TÑ@TÓUÓVÐVrL   c                ó\  — t        |«      }t        |t        «      ry|rt        |t        «      ryt        |t        «      rFt        d„ |j                  D «       «      ryt        d„ |j                  D «       «      ryt        |«      syt        |t        «      rt        |«      st        |«      ryy)zŸGenerate 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  óN   K  — | ]  }t        t        |«      t        «      –— Œ y ­wre   ©r’   r<   r,   ©r”   r  s     rM   r–   z.SuggestionEngine.score_type.<locals>.<genexpr>  s   è ø€ ÒL¸q”:œo¨aÓ0´'×:ÑLùrï   c              3  ó2   K  — | ]  }t        |«      –— Œ y ­wre   r   r�  s     rM   r–   z.SuggestionEngine.score_type.<locals>.<genexpr>  s   è ø€ Ò4 q”< —?Ñ4ùó   ‚é   é
   r   )r<   r’   r,   r0   r;   r›   ri  r=   r-   r   Úis_tricky_callable)rb   r  Úarg_poss      rM   Ú
score_typezSuggestionEngine.score_typeû  sˆ   € ô
 ˜AÓˆÜ�aœÔ!ØÙ”z !¤XÔ.ØÜ�aœÔ#ÜÑLÀAÇGÁGÔLÔLØÜÑ4¨A¯G©GÔ4Ô4ØÜ& qÔ)ØÜ�aœÔ&¬L¸¬OÔ?QÐRSÔ?TØØrL   c                ó|   ‡ — t        ˆ fd„|j                  D «       «      ‰ j                  |j                  d¬«      z   S )Nc              3  óD   •K  — | ]  }‰j                  |d ¬«      –— Œ y­w)T©r•  N)r–  )r”   r  rb   s     €rM   r–   z2SuggestionEngine.score_callable.<locals>.<genexpr>  s   øè ø€ ÒI¸�4—?‘? 1¨d�?×3ÑIùs   ƒ Fr™  )ÚsumrE   r–  r/  ©rb   r  s   ` rM   r  zSuggestionEngine.score_callable  s;   ø€ ÜÓI¸Q¿[¹[ÔIÓIÈDÏOÉOØ�J‰J ð M\ó M
ñ 
ð 	
rL   )rÀ   r'   rÇ   r0  r¸   r0  r¹   r0  rº   zfloat | Noner»   ú
str | Noner¼   z
int | Nonert   ru   )rÏ   rB   rt   rB   )rà   rB   rt   úIterator[None])rt   r�  )rç   r   rt   r-   )rö   r0  r÷   r-   rø   úlist[Type | None]rÜ   úlist[Callsite]rù   rX   rt   rX   )rç   r   rt   rž  )rö   r0  r÷   r-   rø   rž  rÜ   rŸ  rù   rX   rt   úlist[CallableType])r‡   r   rt   z tuple[list[Callsite], list[str]])r  r   rö   r0  r  r0  rt   r   )r‡   r   r  r   rt   ztuple[CallableType, int])r“   r   rt   r   )rÐ   rB   r“   r   rt   rA   )rT   rS   rW   rV   rE   rX   rt   rB   )r   rB   rt   ztuple[str, str, FuncDef])rQ  rB   rR  rB   rt   zSymbolNode | None)rO  rB   rR   rQ   rt   ztuple[str, SymbolNode])r“   r   rt   zFuncDef | None)r‡   r   r    r1   rt   rD   )r(   r   rt   rD   )F)r(   r   r_  r0  rt   r   )r  rB   rt   r/   )
rÐ   rB   rÑ   rB   r“   r   rÒ   rA   rt   rB   )r…  rœ  rö   r0  r    r-   rt   rA   )rˆ  rA   rt   rB   )r…  rœ  r    r3   rt   rB   )r  r3   r•  r0  rt   rQ   )r  r-   rt   rQ   )#rG   rH   rI   rv   rc   rÓ   rÝ   r   rÊ   rË   rè   rë   rÿ   r  r  r×   r  r$  r,  rÌ   rÚ   rÉ   rL  rK  rM  r  rß   rV  ræ   rÍ   r5  rÎ   r@  r–  r  rK   rL   rM   r·   r·   õ   s)  „ ÙBð  ØØ!%Ø $Ø"&ñ#à*ð#ð ð	#ð
 ð#ð ð#ð ð#ð ð#ð  ð#ð 
ó#ó45ó
ð ò,ó ð,ð ò
4ó ð
4ó
ó/ð4àð4ð ð4ð $ð	4ð
 "ð4ð ð4ð 
ó4ól
ð_àð_ð ð_ð $ð	_ð
 "ð_ð ð_ð 
ó_ó$5ð
Ø)ð
Ø6:ð
ØKOð
à	ó
ó	0óó"*?ðX&à&ð&ð *ð&ð $ð	&ð
 
ó&ó(*#óX!$óF!óFó<'ó(Côó<ð1Øð1Ø#&ð1Ø.5ð1ØCVð1à	ó1ð,
Ø$ð
Ø15ð
Ø<Hð
à	ó
óJóWó
ô*
rL   r·   c                ó€  — t        | «      }t        |t        «      r|j                  t        j
                  k7  ryt        |t        «      r|ryt        |t        «      r:t        d„ |j                  D «       «      ryt        d„ |j                  D «       «      ryt        |t        «      rt        |«      ryt        |«      ryy)ziGenerate a very made up number representing the Anyness of a type.

    Higher is better, 1.0 is max
    r   g      à?c              3  óN   K  — | ]  }t        t        |«      t        «      –— Œ y ­wre   rŽ  r�  s     rM   r–   z!any_score_type.<locals>.<genexpr>!  s   è ø€ ÒH¸1Œzœ/¨!Ó,¬g×6ÑHùrï   c              3  ó2   K  — | ]  }t        |«      –— Œ y ­wre   r   r�  s     rM   r–   z!any_score_type.<locals>.<genexpr>#  s   è ø€ Ò0 1Œ|˜A�Ñ0ùr‘  g      Ð?r¾   )r<   r’   r,   rª   r6   rå   r0   r;   r›   ri  r-   r”  r   )Úutr•  r  s      rM   Úany_score_typer¥    s‘   € ô
 	˜Ó€AÜ�!”WÔ !§-¡-´9×3NÑ3NÒ"NØÜ�!”XÔ¡7ØÜ�!”YÔÜÑHÀÇÁÔHÔHØÜÑ0¨¯©Ô0Ô0ØÜ�!”\Ô"Ô'9¸!Ô'<ØÜ�A„ØàrL   c                ó(  — | j                   t        |«      d  D �cg c]  }t        |d¬«      ‘Œ }}t        t	        | j
                  «      t        «      r|s"|rdnt        | j
                  d¬«      }|||gz  }t        |«      t        |«      z  S c c}w )NTr™  r¾   F)	rE   rQ   r¥  r’   r<   r/  r0   rš  rñ   )r  rö   r  r  ÚscoresÚrets         rM   r  r  -  s~   € à78·{±{Ä3ÀyÃ>ÐCSÐ7TÖU°!Œn˜Q¨Ö-ÐU€FÐUô ”o a§j¡jÓ1´8Ô<ÁFÙ"‰c¬°q·z±zÈ5Ô(QˆØ�3˜�*Ñˆäˆv‹;œ˜V›Ñ$Ð$ùò Vs   ›Bc                óV   — | j                   xs t        d„ | j                  D «       «      S )z@Is t a callable that we need to put a ... in for syntax reasons?c              3  ó`   K  — | ]&  }|j                  «       xs |j                  «       –— Œ( y ­wre   )Úis_starrA  )r”   Úks     rM   r–   z%is_tricky_callable.<locals>.<genexpr><  s$   è ø€ Ò$VÀQ Q§Y¡Y£[Ò%@°A·J±J³LÓ%@Ñ$Vùs   ‚,.)Úis_ellipsis_argsr›   rT   ©r  s    rM   r”  r”  :  s"   € à×ÑÒV¤Ñ$VÈ!Ï+É+Ô$VÓ!VÐVrL   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 typesc                óB   •— t         ‰| �  |¬«       || _        || _        y )N)rÅ   )Úsuperrc   rà   rÃ   )rb   rà   rÃ   rÅ   Ú	__class__s       €rM   rc   zTypeFormatter.__init__C  s!   ø€ Ü‰Ñ ÐÔ)ØˆŒØˆ�
rL   c                ó4   — |j                   r|j                   S y©NÚAny)Úmissing_import_namer›  s     rM   Ú	visit_anyzTypeFormatter.visit_anyH  s   € Ø× Ò Ø×(Ñ(Ð(àrL   c                óv  — |j                   j                  xs |j                   j                  xs d }|€yt        | j                  |«      }|sJ ‚|\  }}| j
                  ra|j                  d«      }| j                  | j
                     j                  }|r+|d   |j                  v r||j                  vr| j
                  }||fdk(  r'dd|j                  d   j                  | «      z   dz   }}n.|j                  r"|d| j                  |j                  «      › d	�z  }|d
k(  ry|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²   ri   r)  r?   rÃ   rà   rW  rX  r(  rœ   r†   Úlist_str)	rb   r  r  Úmod_objrÐ   rƒ  ÚpartsrX  Údelims	            rM   Úvisit_instancezTypeFormatter.visit_instanceN  s-  € Ø�F‰F�O‰OÒ2˜qŸv™vŸ{™{Ò2¨dˆØˆ9Øä˜tŸz™z¨1Ó-ˆÙˆˆwØ‰ˆˆSð
 �;Š;Ø—I‘I˜c“NˆEØ—:‘:˜dŸk™kÑ*×/Ñ/ˆDÙ˜˜a™ D§J¡JÑ.°3¸d¿j¹jÑ3HØ—k‘k�à�ˆ:Ð.Ò.Ø ¨A¯F©F°1©I×,<Ñ,<¸TÓ,BÑ!BÀXÑ!M�‰CØ�VŠVØ�Q�t—}‘} Q§V¡VÓ,Ð-¨QÐ/Ñ/ˆCàÐ-Ò-ØØ�JÒØˆJà c™>‘C¨sˆEØ˜‘; Ñ$Ð$rL   c                ó  — |j                   rV|j                   j                  r@|j                   j                  j                  }|dk7  r|j                   j                  | «      S | j	                  |j
                  «      }d|› d�S )Nzbuiltins.tupler¼  r¾  )Úpartial_fallbackr²   ri   r†   rÁ  ri  )rb   r  Úfallback_namer  s       rM   Úvisit_tuple_typezTypeFormatter.visit_tuple_typem  sp   € Ø×Ò !×"4Ñ"4×"9Ò"9Ø×.Ñ.×3Ñ3×<Ñ<ˆMØÐ 0Ò0Ø×)Ñ)×0Ñ0°Ó6Ð6Ø�M‰M˜!Ÿ'™'Ó"ˆØ˜�s˜!ˆ}ÐrL   c                 ó   — yr´  rK   r›  s     rM   Úvisit_uninhabited_typez$TypeFormatter.visit_uninhabited_typeu  s   € ØrL   c                ó8   — |j                   j                  | «      S re   )Úfallbackr†   r›  s     rM   Úvisit_typeddict_typez"TypeFormatter.visit_typeddict_typex  s   € Ø�z‰z× Ñ  Ó&Ð&rL   c                ó¦   •— t        |j                  «      dk(  r*t        |«      rt        |«      j	                  | «      }|› d�S t
        ‰| �  |«      S )NrE  z | None)rñ   ri  r=   r>   r†   r±  Úvisit_union_type)rb   r  r  r²  s      €rM   rÐ  zTypeFormatter.visit_union_type{  sL   ø€ Üˆq�w‰w‹<˜1ÒÔ!4°QÔ!7Ü Ó"×)Ñ)¨$Ó/ˆAØ�S˜�=Ð ä‘7Ñ+¨AÓ.Ð.rL   c                óæ   — t        |«      rd}n=|j                  D �cg c]  }|j                  | «      ‘Œ }}ddj                  |«      › d�}d|› d|j                  j                  | «      › d�S c c}w )Nz...r½  r=  r¾  z	Callable[)r”  rE   r†   rØ   r/  )rb   r  Úarg_strr    rœ   s        rM   Úvisit_callable_typez!TypeFormatter.visit_callable_type‚  sr   € ä˜aÔ Ø‰Gð 12·±Ö<¨�C—J‘J˜tÕ$Ð<ˆDÐ<Ø˜$Ÿ)™) D›/Ð*¨!Ð,ˆGà˜7˜) 2 a§j¡j×&7Ñ&7¸Ó&=Ð%>¸aÐ@Ð@ùò =s   �A.)rà   rœ  rÃ   r   rÅ   r#   rt   ru   )r  r,   rt   rB   )r  r/   rt   rB   )r  r2   rt   rB   )r  r:   rt   rB   )r  r5   rt   rB   )r  r;   rt   rB   )r  r-   rt   rB   )rG   rH   rI   rv   rc   r·  rÅ  rÉ  rË  rÎ  rÐ  rÓ  Ú__classcell__)r²  s   @rM   rŠ  rŠ  ?  s/   ø„ Ù&õó
ó%ó>óó'õ/÷ArL   rŠ  ÚTType)Úboundc                óP   — t        t        | j                  t        «       «      «      S )z½Make 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    rM   rê   rê   ”  s   € ô ”�q—x‘xÔ 1Ó 3Ó4Ó5Ð5rL   c                  ó   — e Zd Zdd„Zdd„Zy)rØ  c                ó^   — |j                   s |j                  t        j                  ¬«      S |S )N)rª   )r¶  r	  r6   rå   r›  s     rM   r·  zMakeSuggestionAny.visit_anyž  s'   € Ø×$Ò$Ø—?‘?¬y×/JÑ/J�?ÓKÐKàˆHrL   c                ó~   — |j                  |j                  D �cg c]  }|j                  | «      ‘Œ c}¬«      S c c}w )N©rœ   )r	  rœ   r†   )rb   r  Úas      rM   Úvisit_type_alias_typez'MakeSuggestionAny.visit_type_alias_type¤  s-   € Ø�‰¸Q¿V¹VÖ$D¸ Q§X¡X¨d¥^Ò$DˆÓEÐEùÒ$Ds   š:N)r  r,   rt   r3   )r  r4   rt   r3   )rG   rH   rI   r·  rÞ  rK   rL   rM   rØ  rØ  �  s   „ óôFrL   rØ  c                óF   — t        | «      }t        | «      }||k(  r|gS ||gS )z°Generate 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      rM   rõ   rõ   ¨  s3   € ô ! Ó'€KÜ& uÓ-€JØ�jÒ Øˆ}Ðà˜ZÐ(Ð(rL   c                óH   — t        | D �cg c]	  }d|v sŒ|‘Œ c}«      S c c}w )Nz error: )rñ   )Úmsgsr  s     rM   r  r  ¶  s!   € Ü˜4Ö3�a :°¢?’Ò3Ó4Ð4ùÒ3s   Š	”c           
     ó¨  — t        | «      }t        |«      }t        |t        «      r t        |t        «      r|j                  r|S |S t        |t        «      rvt        |t        «      rf|j
                  |j
                  k(  rM|j                  t        |j                  |j                  «      D ��cg c]  \  }}t        ||«      ‘Œ c}}¬«      S t        |t        «      r¡t        |t        «      r‘|j                  |j                  k(  rxt        |j                  «      t        |j                  «      k(  rM|j                  t        |j                  |j                  «      D ��cg c]  \  }}t        ||«      ‘Œ c}}¬«      S t        |t        «      rt        |t        «      rt        ||«      S t        |t         «      rt#        ||«      S |S c c}}w c c}}w )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]

    rÜ  )ri  )r<   r’   r,   r¶  r/   r²   r	  r?  rœ   r4  r2   rÇ  rñ   ri  r-   r  r;   Úrefine_union)ÚtiÚsir  r  ÚtaÚsas         rM   r4  r4  º  s^  € ô0 	˜Ó€AÜ˜Ó€Aä�!”WÔä˜q¤'Ô*¨q×/DÒ/DˆqÐKÈ!ÐKä�!”XÔ¤:¨a´Ô#:¸q¿v¹vÈÏÉÒ?OØ�‰ÄsÈ1Ï6É6ÐST×SYÑSYÓGZ×$[¹V¸RÀ¤[°°RÕ%8Ó$[ˆÓ\Ð\ô 	�1”iÔ Ü�qœ)Ô$Ø×Ñ !×"4Ñ"4Ò4Ü�—‘‹LœC §¡›LÒ(à�‰ÌÈAÏGÉGÐUV×U\ÑU\ÓH]×%^¹f¸bÀ"¤k°"°bÕ&9Ó%^ˆÓ_Ð_ä�!”\Ô"¤z°!´\Ô'BÜ˜q !Ó$Ð$ä�!”YÔÜ˜A˜qÓ!Ð!ð €Hùó% %\ùó &_s   Â.G
ÅG
c                ód  — | |k(  r| S t        |t        «      r|j                  n|g}g }| j                  D ]D  }d}|D ]'  }t        ||«      }||k7  sŒ|j	                  |«       d}Œ) |rŒ4|j	                  |«       ŒF t        j                  d«      5  t        |«      cddd«       S # 1 sw Y   yxY w)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;   ri  r4  ro   r(   r2  r+   )r  r  Ú	rhs_itemsÚ	new_itemsÚlhsÚrefinedÚrhsÚnews           rM   rå  rå  ï  s¸   € ð 	ˆA‚vØˆä% a¬Ô3�—’¸!¸€Ià€IØ�w‰wò "ˆØˆØò 	ˆCÜ˜c 3Ó'ˆCØ�c‹zØ× Ñ  Ô%Ø‘ð		ò
 Ø×Ñ˜SÕ!ð"ô 
×	"Ñ	" 4Ó	(ñ 0Ü$ YÓ/÷0÷ 0ò 0ús   ÂB&Â&B/c           
     óö  — | j                   |j                   k7  r| S | j                  r;t        |«      s0|j                  t	        | j
                  |j
                  «      ¬«      S t        | «      s| j                  |j                  k7  r| S | j                  t        | j                  |j                  «      D ��cg c]  \  }}t	        ||«      ‘Œ c}}t	        | j
                  |j
                  «      ¬«      S c c}}w )zKRefine a callable based on another.

    See comments for refine_type.
    r.  )rE   r/  )	rÍ  r­  r”  r	  r4  r/  rT   r?  rE   )r  r  rè  ré  s       rM   r  r    s¹   € ð
 	‡z�z�Q—Z‘ZÒØˆà×ÒÔ"4°QÔ"7Ø�‰¬°A·J±JÀÇ
Á
Ó(KˆÓLÐLä˜!Ô §¡¨q¯{©{Ò :Øˆà�?‰?Ü58¸¿¹ÀaÇkÁkÓ5R×S©6¨2¨r”;˜r 2Õ&ÓSÜ˜QŸZ™Z¨¯©Ó4ð ó ð ùÛSs   Â7C5
ÚTc                óD   — g }| D ]  }||vsŒ|j                  |«       Œ |S re   )ro   )rã   rð  r  s      rM   rÙ   rÙ   '  s/   € Ø€CØò ˆØ�CŠ<Ø�J‰J�q�Mðð €JrL   )rz   rƒ   r‡   r   rt   ú
list[Type])rz   rƒ   r‡   r   rt   rX   )r    r,   rt   r0  )r    r3   rt   r0  )r    r1   rt   r0  )r¤  r3   r•  r0  rt   Úfloat)r  r-   rö   r0  r  r0  rt   rõ  )r  r-   rt   r0  )r  rÕ  rt   rÕ  )rú   rô  rt   rô  )rã  rD   rt   rQ   )ræ  r3   rç  r3   rt   r3   )r  r;   r  r1   rt   r3   )r  r-   r  r-   rt   r-   )rã   úlist[T]rt   rö  )qrv   Ú
__future__r   r  rÇ   ra  rF  Úcollections.abcr   r   Ú
contextlibr   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"   Úmypy.optionsr#   Úmypy.pluginr$   r%   r&   Úmypy.server.updater'   Ú
mypy.stater(   Úmypy.traverserr)   Úmypy.typeopsr*   r+   Ú
mypy.typesr,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   Úmypy.types_utilsr=   r>   Ú	mypy.utilr?   rA   rO   rZ   rx   r‰   r‹   r¦   Ú	Exceptionr¨   r­   r¯   rµ   r·   r¥  r  r”  rŠ  rÕ  rê   rØ  rõ   r  r4  rå  r  rò  rÙ   rK   rL   rM   ú<module>r     sl  ðñõ0 #ã Û Û 	Û 
ß .Ý %ß 7Ó 7å .ß #Ý 'ß =Ý $Ý $Ý /÷÷ ÷ ÷ õ  !ß >Ñ >Ý 6Ý Ý +ß 9÷÷ ÷ ÷ õ ÷& BÝ "ô˜)ô ô
 ˆzô  ô#'�vô #'ôPÐ#ô ó ôFÐ#ô FóBFô"	˜	ô 	ó1óAó
	÷^
ñ ^
óBó.
%óWô
OA�Nô OAñd 	�˜tÔ$€ó6ôF˜ô Fó)ó5ó2ój0óBñ( ˆCƒL€ôrL   