o
    ï›h`&  ã                   @   sú   d 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	 ddl
mZmZmZ ddlmZ g d¢Ze d	ej¡e d
ej¡gZe d¡Zda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e	ƒ dd„ ƒZe	ƒ ed d!„ ƒƒZ dS )"a©  
Docstrings are another source of information for functions and classes.
:mod:`jedi.inference.dynamic_params` tries to find all executions of functions,
while the docstring parsing is much easier. There are three different types of
docstrings that |jedi| understands:

- `Sphinx <http://sphinx-doc.org/markup/desc.html#info-field-lists>`_
- `Epydoc <http://epydoc.sourceforge.net/manual-fields.html>`_
- `Numpydoc <https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt>`_

For example, the sphinx annotation ``:type foo: str`` clearly states that the
type of ``foo`` is ``str``.

As an addition to parameter searching, this module also provides return
annotations.
é    N)ÚparseÚParserSyntaxError)Údebug)Úinference_state_method_cache)Úiterator_to_value_setÚValueSetÚ	NO_VALUES)ÚLazyKnownValues)z\s*:type\s+%s:\s*([^\n]+)z\s*:param\s+(\w+)\s+%s:[^\n]*z\s*@type\s+%s:\s*([^\n]+)z\s*:rtype:\s*([^\n]+)z\s*@rtype:\s*([^\n]+)z:[^`]+:`([^`]+)`c                  C   s&   t tttfƒr	t‚ddlm}  | atS )Nr   ©ÚNumpyDocString)Ú
isinstanceÚ_numpy_doc_string_cacheÚImportErrorÚSyntaxErrorZnumpydoc.docscraper   r
   © r   úOc:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/inference/docstrings.pyÚ_get_numpy_doc_string_cls/   s
   r   c              	   C   s²   t  ¡ �* t  d¡ z
tƒ | ƒjd }W n ty&   g  Y W  d  ƒ S w W d  ƒ n1 s1w   Y  |D ]\}}}||krVt d|¡}|rN| d¡}t	t
|ƒƒ  S q8g S )zASearch `docstr` (in numpydoc format) for type(-s) of `param_str`.ÚignoreZ
ParametersNz"([^,]+(,[^,]+)*?)(,[ ]*optional)?$é   )ÚwarningsÚcatch_warningsÚsimplefilterr   Ú_parsed_dataÚ	ExceptionÚreÚmatchÚgroupÚlistÚ_expand_typestr)ÚdocstrÚ	param_strÚparamsZp_nameÚp_typeZp_descrÚmr   r   r   Ú_search_param_in_numpydocstr8   s$   

ùÿû
ür$   c              	   c   sÀ   � t  ¡ �% t  d¡ ztƒ | ƒ}W n ty"   Y W d  ƒ dS w W d  ƒ n1 s-w   Y  z|jd }||jd 7 }W n
 tyJ   Y dS w |D ]\}}}|sV|}t|ƒE dH  qMdS )zP
    Search `docstr` (in numpydoc format) for type(-s) of function returns.
    r   NZReturnsZYields)r   r   r   r   r   r   r   )r   ÚdocÚreturnsZr_nameZr_typeZr_descrr   r   r   Ú_search_return_in_numpydocstrK   s,   €

ûÿý
ÿür'   c                 c   sð   � t  d| ¡r|  d¡D ]}| d¡d  ¡ V  qdS t  d| ¡r+|  d¡d V  dS |  d¡rst| dd�jd }|jd	krot|jd
 dg ƒD ])}|jdkr[d|j	v rWdV  qGdV  qG|jdkrnd|j
 ¡ v rkdV  qGdV  qGdS dS | V  dS )z@
    Attempts to interpret the possible types in `type_str`
    z\bor\bÚorZofr   z\bof\bÚ{z3.7)ÚversionÚatomr   ÚchildrenÚnumberÚ.ÚfloatÚintÚstringÚbÚbytesÚstrN)r   ÚsearchÚsplitÚstripÚ
startswithr   r,   ÚtypeÚgetattrÚvalueZstring_prefixÚlower)Útype_strÚtÚnodeÚleafr   r   r   r   c   s.   €ÿ




€õ
r   c                    sH   ‡ fdd„t D ƒ}|D ]}| | ¡}|rt| d¡ƒg  S qt| ˆ ƒS )aä  
    Search `docstr` for type(-s) of `param_str`.

    >>> _search_param_in_docstr(':type param: int', 'param')
    ['int']
    >>> _search_param_in_docstr('@type param: int', 'param')
    ['int']
    >>> _search_param_in_docstr(
    ...   ':type param: :class:`threading.Thread`', 'param')
    ['threading.Thread']
    >>> bool(_search_param_in_docstr('no document', 'param'))
    False
    >>> _search_param_in_docstr(':param int param: some description', 'param')
    ['int']

    c                    s    g | ]}t  |t  ˆ ¡ ¡‘qS r   )r   ÚcompileÚescape©Ú.0Úp©r    r   r   Ú
<listcomp>–   s    ÿz+_search_param_in_docstr.<locals>.<listcomp>r   )ÚDOCSTRING_PARAM_PATTERNSr5   Ú_strip_rst_roler   r$   )r   r    ÚpatternsÚpatternr   r   rF   r   Ú_search_param_in_docstr„   s   
ÿ
ÿ
rL   c                 C   s   t  | ¡}|r| d¡S | S )a   
    Strip off the part looks like a ReST role in `type_str`.

    >>> _strip_rst_role(':class:`ClassName`')  # strip off :class:
    'ClassName'
    >>> _strip_rst_role(':py:obj:`module.Object`')  # works with domain
    'module.Object'
    >>> _strip_rst_role('ClassName')  # do nothing when not ReST role
    'ClassName'

    See also:
    http://sphinx-doc.org/domains.html#cross-referencing-python-objects

    r   )ÚREST_ROLE_PATTERNr   r   )r=   r   r   r   r   rI       s   

rI   c           	   	   C   sä   |d u rg S t  d|¡}d dd„ |D ƒ¡}|› d|› �}tjd|dd� | jj}z	|j|dd	�}W n ty=   g  Y S w z|j	d
 }W n t
tfyR   g  Y S w |jdvrZg S ddlm} || | j|g d�}tt| ¡ |ƒƒS )Nz((?:\w+\.)*\w+)\.Ú
c                 s   s   � | ]}d |› �V  qdS )zimport Nr   rC   r   r   r   Ú	<genexpr>½   s   € z._infer_for_statement_string.<locals>.<genexpr>zParse docstring code %sÚBLUE©ÚcolorF)Zerror_recoveryéþÿÿÿ)Únamer+   Z	atom_exprr   )ÚDocstringModule)Zin_module_contextÚinference_stateZmodule_nodeZ
code_lines)r   ÚfindallÚjoinr   ÚdbgrV   Úgrammarr   r   r,   ÚAttributeErrorÚ
IndexErrorr9   Zjedi.inference.docstring_utilsrU   r   Ú_execute_types_in_stmtZ
as_context)	Úmodule_contextr1   Zpotential_importsZimportsrZ   ÚmoduleÚstmtrU   r#   r   r   r   Ú_infer_for_statement_string¶   s6   ÿÿ
üra   c                    s"   ˆ   |¡}t ‡ fdd„|D ƒ¡S )zÒ
    Executing all types or general elements that we find in a statement. This
    doesn't include tuple, list and dict literals, because the stuff they
    contain is executed. (Used as type information).
    c                 3   s   � | ]	}t ˆ j|ƒV  qd S ©N)Ú_execute_array_valuesrV   )rD   Úd©r^   r   r   rO   â   s
   € 
ÿ
ÿz)_execute_types_in_stmt.<locals>.<genexpr>)Z
infer_noder   Ú	from_sets)r^   r`   Zdefinitionsr   re   r   r]   Û   s   
þr]   c           	         sŠ   ddl m}m}m} t||ƒrA|jdv rAg }| ¡ D ]}t ‡ fdd„| 	¡ D ƒ¡}| 
t|ƒ¡ q|jdkr9|n|}|ˆ |ƒhS | ¡ S )z—
    Tuples indicate that there's not just one return value, but the listed
    ones.  `(str, int)` means that it returns a tuple with both types.
    r   )ÚSequenceLiteralValueÚ	FakeTupleÚFakeList)Útupler   c                 3   s   � | ]}t ˆ |ƒV  qd S rb   )rc   )rD   Útyp©rV   r   r   rO   ñ   s
   € ÿ
ÿz(_execute_array_values.<locals>.<genexpr>rj   )Zjedi.inference.value.iterablerg   rh   ri   r   Z
array_typeZ
py__iter__r   rf   ZinferÚappendr	   Zexecute_annotation)	rV   Úarrayrg   rh   ri   ÚvaluesZ
lazy_valueÚobjectsÚclsr   rl   r   rc   è   s   þrc   c                    sr   ‡ ‡fdd„}|   ¡ ‰ ˆ ¡ }|jdkrtS ||  ¡ ƒ}|  ¡ r/|  ¡ dkr/||| j ¡ ƒO }tj	d|dd� |S )Nc                    s    t ‡ fdd„t| ˆjjƒD ƒƒS )Nc                 3   s$   � | ]}t ˆ |ƒD ]}|V  q	qd S rb   )ra   )rD   r    rE   re   r   r   rO   ÿ   s   € ýþÿz7infer_param.<locals>.infer_docstring.<locals>.<genexpr>)r   rL   rT   r;   )Ú	docstring©r^   Úparamr   r   Úinfer_docstringþ   s   þz$infer_param.<locals>.infer_docstringZlambdefÚ__init__z#Found param types for docstring: %srP   rQ   )
Úget_root_contextZget_parent_functionr9   r   Ú	py__doc__Zis_bound_methodZ
py__name__Zclass_contextr   rY   )Úfunction_valuert   ru   ÚfuncÚtypesr   rs   r   Úinfer_paramü   s   
r|   c                 c   s4   � dd„ }||   ¡ ƒD ]}t|  ¡ |ƒE d H  qd S )Nc                 s   s<   � t D ]}| | ¡}|rt| d¡ƒV  qt| ƒE d H  d S )Nr   )ÚDOCSTRING_RETURN_PATTERNSr5   rI   r   r'   )ÚcoderE   r   r   r   r   Úsearch_return_in_docstr  s   €
€z3infer_return_types.<locals>.search_return_in_docstr)rx   ra   rw   )ry   r   r=   r   r   r   Úinfer_return_types  s
   €ÿr€   )!Ú__doc__r   r   Zparsor   r   Zjedir   Zjedi.inference.cacher   Zjedi.inference.base_valuer   r   r   Zjedi.inference.lazy_valuer	   rH   rA   ÚMr}   rM   r   r   r$   r'   r   rL   rI   ra   r]   rc   r|   r€   r   r   r   r   Ú<module>   s8    þ
	!%
