a
    ÷£BeÚ  ã                   @   sè   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	l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Zdd„ Zejedd„ ƒƒZedd�edd„ ƒƒZdd„ Zdd„ Zdd„ Z dS )aß  
One of the really important features of |jedi| is to have an option to
understand code like this::

    def foo(bar):
        bar. # completion here
    foo(1)

There's no doubt wheter bar is an ``int`` or not, but if there's also a call
like ``foo('str')``, what would happen? Well, we'll just show both. Because
that's what a human would expect.

It works as follows:

- |Jedi| sees a param
- search for function calls named ``foo``
- execute these calls and check the input.
é    )Úsettings)Údebug)Úget_parent_scope)Úinference_state_method_cache)ÚTreeArguments)Úget_executed_param_names)Úis_stdlib_path)Úto_list)Úinstance)ÚValueSetÚ	NO_VALUES)Ú#get_module_contexts_containing_name)Ú	recursioné   c                    s   ‡ fdd„}|S )Nc              
      sˆ   | j }t || j¡�`}|rb| jd7  _z(ˆ | |ƒW | jd8  _W  d   ƒ S | jd8  _0 tW  d   ƒ S 1 sz0    Y  d S )Né   )Úinference_stater   Zexecution_allowedÚ	tree_nodeÚdynamic_params_depthr   )Úfunction_valueÚparam_indexÚinfÚallowed©Úfunc© ú`/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/inference/dynamic_params.pyÚwrapper&   s    
þz"_avoid_recursions.<locals>.wrapperr   )r   r   r   r   r   Ú_avoid_recursions%   s    r   c                    sª   ˆ j jstS ˆ j}ˆ  ¡  ¡ }|dur2t|ƒr2tS |jdkrRt|ƒ}|du rZtS n|j	j
}tjd|dd� ˆ  ¡ }t|||ƒ}t ‡ ‡fdd„|D ƒ¡}tjddd� |S )	a:  
    A dynamic search for param values. If you try to complete a type:

    >>> def func(foo):
    ...     foo
    >>> func(1)
    >>> func("")

    It is not known what the type ``foo`` without analysing the whole code. You
    have to look for all calls to ``func`` to find out what ``foo`` possibly
    is.
    NZlambdefzDynamic param search in %s.ZMAGENTA)Úcolorc                 3   s    | ]}t ˆ |ƒˆ  ¡ V  qd S )N)r   Úinfer)Ú.0Ú	arguments©r   r   r   r   Ú	<genexpr>\   s   ýÿþz'dynamic_param_lookup.<locals>.<genexpr>zDynamic param result finished)r   Zdo_dynamic_params_searchr   r   Úget_root_contextZ
py__file__r   ÚtypeÚ_get_lambda_nameÚnameÚvaluer   ZdbgÚ_search_function_argumentsr   Z	from_sets)r   r   ÚfuncdefÚpathÚstring_nameÚmodule_contextZarguments_listÚvaluesr   r"   r   Údynamic_param_lookup6   s&    
ür/   N)Údefaultc                 c   sÌ   |}|dkr*t |ƒ}|jdkr*|jj}|}d}d}| j}tjrRt|| g|dd�}n| g}|D ]j}	t|	|ƒD ]P\}
}|d7 }||j	 t
kr�  dS |	 |
¡}t||||
|ƒD ]}d	}|V  qªqj|r\ dS q\dS )
z(
    Returns a list of param names.
    Ú__init__ÚclassdefFr   é   )Zlimit_reductionr   NT)r   r%   r'   r(   r   r   Z dynamic_params_for_other_modulesr   Ú_get_potential_nodesr   ÚMAX_PARAM_SEARCHESÚcreate_contextÚ_check_name_for_execution)r-   r*   r,   Úcompare_nodeÚclsZfound_argumentsÚir   Zmodule_contextsZfor_mod_contextr'   ÚtrailerÚrandom_contextr!   r   r   r   r)   f   s8    
ý

ÿ
r)   c                 C   sD   | j }|jdkr@t| ¡ d ƒ}|dkr@|jd }|jdkr@|jS d S )NZ	expr_stmtú=r   r'   )Úparentr%   ÚnextZyield_operatorsÚchildrenr(   )ÚnodeÚstmtZfirst_operatorÚfirstr   r   r   r&   –   s    


r&   c                 c   s`   z| j  ¡ | }W n ty&   Y d S 0 |D ].}| ¡ }|j}|jdkr,|dkr,||fV  q,d S )Nr;   ú()r   Zget_used_namesÚKeyErrorZget_next_leafr>   r%   )Zmodule_valueZfunc_string_nameÚnamesr'   Zbracketr;   r   r   r   r4   ¢   s    r4   c                 #   s  ddl m} ‡ ‡‡fdd„}ˆ ˆ |¡D ]Þ}|j}||krF||ƒV  q(t|j|ƒr(|jdkr(|j ¡ }	t|	ƒdkrtq(|	d  ¡ }
dd„ |
D ƒ|gkr(ˆ  	¡ }| 
||ƒ¡}t||	d jƒ}|D ]F\}‰|j|j  k rà|jk r¾n q¾| |¡}tˆ|||ˆƒE d H  q¾q(d S )	Nr   )ÚBaseFunctionExecutionContextc                    st   ˆj d }|dkrd }tˆˆ |ˆƒ}ddlm} | jjdkrXt ˆ| j| |¡}|||ƒS |  	¡ rl|| j|ƒ}|S d S )Nr   ú)r   )ÚInstanceArgumentsr2   )
r@   r   Zjedi.inference.value.instancerI   r   r%   r
   ZTreeInstanceÚparent_contextZis_bound_method)r(   ZarglistÚargsrI   Zcreated_instance©Úcontextr   r;   r   r   Úcreate_args²   s     
ü
z._check_name_for_execution.<locals>.create_argsr*   r   c                 S   s   g | ]
}|j ‘qS r   )r   )r    Úvr   r   r   Ú
<listcomp>Ò   ó    z-_check_name_for_execution.<locals>.<listcomp>)Zjedi.inference.value.functionrG   r   r   Ú
isinstancerJ   r%   Zget_param_namesÚlenr$   Z
as_contextr4   r,   Ú	start_posÚend_posr6   r7   )r   rM   r8   r'   r;   rG   rN   r(   Z
value_nodeZparam_namesr.   r-   Zexecution_contextZpotential_nodesr<   r   rL   r   r7   ¯   s6    ÿ

ûr7   )!Ú__doc__Zjedir   r   Zjedi.parser_utilsr   Zjedi.inference.cacher   Zjedi.inference.argumentsr   Zjedi.inference.paramr   Zjedi.inference.helpersr   Zjedi.inference.utilsr	   Zjedi.inference.valuer
   Zjedi.inference.base_valuer   r   Zjedi.inference.referencesr   Zjedi.inferencer   r5   r   Zincrease_indentr/   r)   r&   r4   r7   r   r   r   r   Ú<module>   s.   ..