B
    b2                 @   s   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	d
ddgZdd Zdd Zdd Zdd Zdd Zdd Zdd Ze d!ddZG dd de	Zd S )"    )Path)search_ancestor)inference_state_method_cache)load_module_from_path)ParserTreeFilter)	NO_VALUESValueSet)infer_call_of_leaf)_pytestZmonkeypatch)r
   Zcapture)r
   Z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   )valueZ	arguments)callback 2lib/python3.7/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 k	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)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_annotation)r   r   r   r   r   r   '   s   )
Z	tree_nodeZ
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   F   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   5   s    

z&infer_anonymous_param.<locals>.wrapperr   )r+   r   r   )r+   r    r   infer_anonymous_param   s    r,   c                s    fdd}|S )Nc                s4   t | \}}|r,t|  | j|d}|r,|S  | S )N)r!   )r"   r$   r#   r%   )r&   r'   r(   names)r+   r   r   r   O   s    z%goto_anonymous_param.<locals>.wrapperr   )r+   r   r   )r+   r   goto_anonymous_paramN   s    r.   c                s    fdd}|S )Nc                sL   |   }t||r@g }x t|D ]}|t| 7 }q W |r@|S  | ||S )N)r#   _is_pytest_func_iter_pytest_modulesFixtureFiltervalues)context	func_namedecorator_nodesmodule_contextr-   )r+   r   r   r   _   s    
z%complete_param_names.<locals>.wrapperr   )r+   r   r   )r+   r   complete_param_names^   s    	r7   c             C   s0   x*t | |dD ]} t| |}|r|S qW d S )N)r!   )r0   r1   get)r6   r   r!   r-   r   r   r   r$   k   s    r$   c             C   s<   t | jd}|dk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   Z	tree_nameZget_decoratorsr/   r   r   r%   )r&   r9   
decoratorsr   r   r   r"   r   s    r"   c             C   s   |  dptdd |D S )NZtestc             s   s   | ]}d |  kV  qdS )r   N)get_code)r   nr   r   r   r      s    z"_is_pytest_func.<locals>.<genexpr>)
startswithr   )r4   r5   r   r   r   r/      s    
r/   Fc             #   s   |s
| V  |   j}|d k	r|  | j }d }xt fdd|D r d}t|j| 	 kryt
| j|}| V  W n tk
r   Y nX    |d k	r j|jkrP  }q4W x,tD ]$}x| j|D ]}| V  qW qW d S )Nc             3   s   | ]} j |V  qd S )N)pathr=   )r   p)folderr   r   r      s    z'_iter_pytest_modules.<locals>.<genexpr>zconftest.py)Z	get_valuefile_ioZget_parent_folderZinference_stateZget_sys_pathr   Zget_file_ior   r>   Z
py__file__r   r   FileNotFoundError_PYTEST_FIXTURE_MODULESimport_module)r6   r!   rA   Zsys_pathZlast_foldermr-   Zmodule_valuer   )r@   r   r0      s,    



r0   c                   s$   e Zd Z fddZdd Z  ZS )r1   c             #   sJ   xDt  |D ]4}|j}|jdkr|j}|jdkr| |r|V  qW d S )Nr9   	decorated)super_filterparenttype_is_fixture)selfr-   r   r9   rF   )	__class__r   r   rH      s    
zFixtureFilter._filterc       	      C   s   |j d }|jdkr|j }n|g}x|D ]}|j d }d| kr(|jdkr|j d }| }|dkrxt| j|dd	}q| j|}n| j|}x"|D ]}|jjdd
dkrdS qW q(W dS )Nr   r:      r   Z	atom_expr)T)Zcut_own_trailer)r   )r
   r*   r   F)	ZchildrenrJ   r;   Zget_last_leafr	   r   Z
infer_noder   r   )	rL   rF   r:   Z	decoratorZdotted_nameZlast_trailerZ	last_leafr2   r   r   r   r   rK      s(    






zFixtureFilter._is_fixture)__name__
__module____qualname__rH   rK   __classcell__r   r   )rM   r   r1      s   	r1   N)F)Zpathlibr   Z
parso.treer   Zjedi.inference.cacher   Zjedi.inference.importsr   Zjedi.inference.filtersr   Zjedi.inference.base_valuer   r   Zjedi.inference.helpersr	   rC   r   r,   r.   r7   r$   r"   r/   r0   r1   r   r   r   r   <module>   s*   / 