a
    ÷£Be%  ã                   @   sê   d dl 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mZ d d	lmZ g d
¢Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeee  dœdd„Zeƒ d!dd„ƒZG dd „ d eƒZdS )"é    N)ÚList)ÚPath)Úsearch_ancestor)Úinference_state_method_cache)Úgoto_importÚload_module_from_path)ÚParserTreeFilter)Ú	NO_VALUESÚValueSet)Úinfer_call_of_leaf))Ú_pytestZmonkeypatch)r   Úcapture)r   Úlogging)r   Ztmpdir)r   Zpytesterc                    s   ‡ fdd„}|S )Nc                    s(   |   ¡ dkr| j  ¡ dkrtS ˆ | |ƒS )NÚfixturez_pytest.fixtures)Z
py__name__Úparent_contextr	   )ÚvalueÚ	arguments©Úcallback© úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/plugins/pytest.pyÚwrapper   s
    ÿzexecute.<locals>.wrapperr   )r   r   r   r   r   Úexecute   s    r   c                    s   dd„ ‰‡ ‡fdd„}|S )Nc                 S   sb   | j jd ur>|  ¡ }tdd„ |D ƒƒr:t dd„ |D ƒ¡S |S |  ¡ }| ¡ rV| ¡ S | 	¡ S d S )Nc                 s   s    | ]}|j jd d�dkV  qdS )T©Zinclude_module_names)ÚtypingÚ	GeneratorN)ÚnameÚget_qualified_names©Ú.0Úvr   r   r   Ú	<genexpr>%   s   þÿz=infer_anonymous_param.<locals>.get_returns.<locals>.<genexpr>c                 s   s   | ]}|  d ¡ ¡ V  qdS )Ú__next__N)Zpy__getattribute__Zexecute_annotationr   r   r   r   r!   (   s   ÿ)
Z	tree_nodeÚ
annotationZexecute_with_valuesÚanyr
   Ú	from_setsÚ
as_contextZis_generatorZmerge_yield_valuesZget_return_values)r   ÚresultZfunction_contextr   r   r   Úget_returns"   s    þ
þz*infer_anonymous_param.<locals>.get_returnsc                    sZ   | j rˆ | ƒS t| ƒ\}}|rR|  ¡ }t|| j|d�}|rRt ‡fdd„|D ƒ¡S ˆ | ƒS )N©Úskip_own_modulec                 3   s$   | ]}|  ¡ D ]}ˆ |ƒV  qqd S ©N)Zinfer)r   r   r   )r(   r   r   r!   G   s   þz9infer_anonymous_param.<locals>.wrapper.<locals>.<genexpr>)Zannotation_nodeÚ _is_a_pytest_param_and_inheritedÚget_root_contextÚ_goto_pytest_fixtureÚstring_namer
   r%   )Ú
param_nameÚis_pytest_paramÚparam_name_is_function_nameÚmoduleÚfixtures©Úfuncr(   r   r   r   7   s     ÿûþz&infer_anonymous_param.<locals>.wrapperr   ©r6   r   r   r5   r   Úinfer_anonymous_param!   s    r8   c                    s   ‡ fdd„}|S )Nc                    s4   t | ƒ\}}|r,t|  ¡ | j|d�}|r,|S ˆ | ƒS ©Nr)   )r,   r.   r-   r/   )r0   r1   r2   Únames©r6   r   r   r   Q   s    ÿýz%goto_anonymous_param.<locals>.wrapperr   r7   r   r;   r   Úgoto_anonymous_paramP   s    r<   c                    s   ‡ fdd„}|S )Nc                    sH   |   ¡ }t||ƒr<g }t|ƒD ]}|t|ƒ ¡ 7 }q|r<|S ˆ | ||ƒS r+   )r-   Ú_is_pytest_funcÚ_iter_pytest_modulesÚFixtureFilterÚvalues)ÚcontextÚ	func_nameÚdecorator_nodesÚmodule_contextr:   r;   r   r   r   a   s    
z%complete_param_names.<locals>.wrapperr   r7   r   r;   r   Úcomplete_param_names`   s    	rE   c                 C   s0   t | |d�D ]} t| ƒ |¡}|r|  S qd S r9   )r>   r?   Úget)rD   r   r*   r:   r   r   r   r.   m   s    r.   c                 C   s<   t | jdƒ}|du rdS | ¡ }t|jj|ƒ|jj| jkfS )z°
    Pytest params are either in a `test_*` function or have a pytest fixture
    with the decorator @pytest.fixture.

    This is a heuristic and will work in most cases.
    ÚfuncdefN)FF)r   Ú	tree_nameZget_decoratorsr=   r   r   r/   )r0   rG   Ú
decoratorsr   r   r   r,   t   s    ÿr,   c                 C   s   |   d¡ptdd„ |D ƒƒS )NÚtestc                 s   s   | ]}d |  ¡ v V  qdS )r   N)Úget_code)r   Únr   r   r   r!   …   ó    z"_is_pytest_func.<locals>.<genexpr>)Ú
startswithr$   )rB   rC   r   r   r   r=   ƒ   s    
ÿr=   )Úreturnc                  C   s”   t jdkrpddlm}  t jdkr,| dd�}n| ƒ  dd¡}t jdkrRd	d
„ |D ƒS dd
„ |D ƒ}dd
„ |D ƒS n ddlm} dd
„ |dd�D ƒS dS )z¦
    Finds pytest plugin modules hooked by setuptools entry points

    See https://docs.pytest.org/en/stable/how-to/writing_plugins.html#setuptools-entry-points
    )é   é   r   )Úentry_points)rP   é
   Zpytest11)Úgroupr   )rP   é	   c                 S   s   g | ]}|j  d ¡‘qS ©Ú.)r3   Úsplit©r   Úepr   r   r   Ú
<listcomp>—   rM   z/_find_pytest_plugin_modules.<locals>.<listcomp>c                 S   s   g | ]}|j  |j¡‘qS r   )ÚpatternÚmatchr   rY   r   r   r   r[   ›   s   ÿc                 S   s    g | ]}|r|  d ¡ d¡‘qS )r3   rW   )rT   rX   )r   Úxr   r   r   r[   Ÿ   rM   )Úiter_entry_pointsc                 S   s   g | ]}|j  d ¡‘qS rV   )Úmodule_namerX   rY   r   r   r   r[   £   rM   N)ÚsysÚversion_infoÚimportlib.metadatarR   rF   Úpkg_resourcesr_   )rR   Zpytest_entry_pointsÚmatchesr_   r   r   r   Ú_find_pytest_plugin_modulesˆ   s    


þrf   Fc                 #   sæ   |s
| V  |   ¡ j}|d ur¶| ¡ ‰ | j ¡ }d }t‡ fdd„|D ƒƒr¶ˆ  d¡}t|jƒ|  	¡ kr’zt
| j|ƒ}| ¡ V  W n ty�   Y n0 ˆ  ¡ ‰ |d ur°ˆ j|jkr°q¶ˆ }q2ttƒ  D ] }| j |¡D ]}| ¡ V  qÐqÀd S )Nc                 3   s   | ]}ˆ j  |¡V  qd S r+   )ÚpathrN   )r   Úp©Úfolderr   r   r!   ³   rM   z'_iter_pytest_modules.<locals>.<genexpr>zconftest.py)Ú	get_valueÚfile_ioZget_parent_folderZinference_stateZget_sys_pathr$   Zget_file_ior   rg   Z
py__file__r   r&   ÚFileNotFoundErrorÚ_PYTEST_FIXTURE_MODULESrf   Úimport_module)rD   r*   rl   Úsys_pathZlast_folderÚmr:   Zmodule_valuer   ri   r   r>   ¦   s,    


r>   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )r?   c                 #   s`   t ƒ  |¡D ]N}|jjdkrFtˆ j|ƒ}t‡ fdd„|D ƒƒrZ|V  qˆ  ˆ j|¡r|V  qd S )NZimport_fromc                 3   s$   | ]}|j rˆ  |j|j ¡V  qd S r+   )rH   Ú_is_fixturer   )r   Ziname©Úselfr   r   r!   Í   s   ýz(FixtureFilter._filter.<locals>.<genexpr>)ÚsuperÚ_filterÚparentÚtyper   r   r$   rr   )rt   r:   r   Zimported_names©Ú	__class__rs   r   rv   È   s    þzFixtureFilter._filterc                 C   sÜ   |j }|jdkrdS |j }|jdkr(dS |jd }|jdkrD|j}n|g}|D ]ˆ}|jd }d| ¡ v rN|jdkr¨|jd	 }| ¡ }	|	d
krœt||	dd�}
q²| |¡}
n
| |¡}
|
D ]}|jjdd�dkr¶  dS q¶qNdS )NrG   FÚ	decoratedr   rI   é   r   Z	atom_expréÿÿÿÿú)T)Zcut_own_trailerr   )r   r4   r   )	rw   rx   ÚchildrenrK   Zget_last_leafr   Z
infer_noder   r   )rt   rA   r   rG   r{   rI   Ú	decoratorZdotted_nameZlast_trailerZ	last_leafr@   r   r   r   r   rr   Ø   s8    






ÿ
ÿzFixtureFilter._is_fixture)Ú__name__Ú
__module__Ú__qualname__rv   rr   Ú__classcell__r   r   ry   r   r?   Ç   s   r?   )F)ra   r   r   Úpathlibr   Z
parso.treer   Zjedi.inference.cacher   Zjedi.inference.importsr   r   Zjedi.inference.filtersr   Zjedi.inference.base_valuer	   r
   Zjedi.inference.helpersr   rn   r   r8   r<   rE   r.   r,   r=   Ústrrf   r>   r?   r   r   r   r   Ú<module>   s(   	/ 