B
    A!p\9K  ã               @   sr   d Z ddlmZmZmZ ddlZddlmZ ddlm	Z	 ddlm
Z ddlm  mZ
 G dd„ de	ƒZd	d
„ ZdS )zHPylint plugin for checking in Sphinx, Google, or Numpy style docstrings
é    )Úprint_functionÚdivisionÚabsolute_importN)ÚIAstroidChecker)ÚBaseChecker)Úutilsc               @   s>  e Zd ZdZeZdZddddddd	d
dgifddddd
dgifddddd
dgifdddd
dgifdddœZdddd d!d"œfd#ddd d$d"œfd%ddd d&d"œfd'ddd d(d"œfd)d*d+ee	j
ƒd,d-œffZd.Zd/d0hZd1d2hZd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Zd?d@„ ZdAdB„ ZdLdDdE„ZdFdG„ ZdHdI„ ZdJdK„ ZdCS )MÚDocstringParameterCheckeraÜ  Checker for Sphinx, Google, or Numpy style docstrings

    * Check that all function, method and constructor parameters are mentioned
      in the params and types part of the docstring.  Constructor parameters
      can be documented in either the class docstring or ``__init__`` docstring,
      but not both.
    * Check that there are no naming inconsistencies between the signature and
      the documentation, i.e. also report documented parameters that are missing
      in the signature. This is important to find cases where parameters are
      renamed only in the code, not in the documentation.
    * Check that all explicitly raised exceptions in a function are documented
      in the function docstring. Caught exceptions are ignored.

    Activate this checker by adding the line::

        load-plugins=pylint.extensions.docparams

    to the ``MASTER`` section of your ``.pylintrc``.

    :param linter: linter object
    :type linter: :class:`pylint.lint.PyLinter`
    Zparameter_documentation)z@"%s" has constructor parameters documented in class and __init__zmultiple-constructor-doczAPlease remove parameter declarations in the class or constructor.)z#"%s" not documented as being raisedzmissing-raises-docz:Please document exceptions for all raised exception types.)zRedundant returns documentationzredundant-returns-docz>Please remove the return/rtype documentation from this method.)zRedundant yields documentationzredundant-yields-docz8Please remove the yields documentation from this method.zMissing return documentationzmissing-return-docz8Please add documentation about what this method returns.Z	old_names)ZW9007zmissing-returns-doc)z!Missing return type documentationzmissing-return-type-docz1Please document the type returned by this method.zMissing yield documentationzmissing-yield-docz:Please add documentation about what this generator yields.)ZW9009zmissing-yields-doc)z Missing yield type documentationzmissing-yield-type-docz0Please document the type yielded by this method.z'"%s" missing in parameter documentationzmissing-param-docz5Please add parameter declarations for all parameters.)ZW9003zmissing-param-docz,"%s" missing in parameter type documentationzmissing-type-docz:Please add parameter type declarations for all parameters.)ZW9004zmissing-type-doc)z)"%s" differing in parameter documentationzdiffering-param-docz-Please check parameter names in declarations.)z."%s" differing in parameter type documentationzdiffering-type-docz2Please check parameter names in type declarations.)ZW9005ZW9006ZW9008ZW9010ZW9011ZW9012ZW9013ZW9014ZW9015ZW9016ZW9017ZW9018zaccept-no-param-docTZynz<y or n>zmWhether to accept totally missing parameter documentation in the docstring of a function that has parameters.)ÚdefaultÚtypeÚmetavarÚhelpzaccept-no-raise-doczoWhether to accept totally missing raises documentation in the docstring of a function that raises an exception.zaccept-no-return-doczoWhether to accept totally missing return documentation in the docstring of a function that returns a statement.zaccept-no-yields-doczWWhether to accept totally missing yields documentation in the docstring of a generator.zdefault-docstring-typeZchoicer	   zRIf the docstring type cannot be guessed the specified docstring type will be used.)r
   r	   Úchoicesr   éþÿÿÿÚ__init__Ú__new__ÚselfÚclsc             C   s:   t  |j| jj¡}|  ||¡ |  ||¡ |  ||¡ dS )z¿Called for function and method definitions (def).

        :param node: Node for a function or method definition in the AST
        :type node: :class:`astroid.scoped_nodes.Function`
        N)r   ÚdocstringifyÚdocÚconfigÚdefault_docstring_typeÚcheck_functiondef_paramsÚcheck_functiondef_returnsÚcheck_functiondef_yields)r   ÚnodeÚnode_doc© r   ú:lib/python3.7/site-packages/pylint/extensions/docparams.pyÚvisit_functiondef¼   s    z+DocstringParameterChecker.visit_functiondefc             C   s’   d }|j | jkr|t |¡}|d k	r|t |j| jj¡}|  	|||¡ | 
¡ pT| ¡ pTd }| 
¡ ph| ¡ phd }|  ||j||¡ |  ||j||¡ d S )N)ÚnameÚconstructor_namesÚchecker_utilsZnode_frame_classr   r   r   r   r   Úcheck_single_constructor_paramsÚ
has_paramsÚparams_documented_elsewhereÚcheck_arguments_in_docstringÚargs)r   r   r   Znode_allow_no_paramÚ
class_nodeÚ	class_docZclass_allow_no_paramr   r   r   r   Ç   s"    
z2DocstringParameterChecker.check_functiondef_paramsc             C   sZ   |j s| ¡ s| ¡ rd S | tj¡}| ¡ s6| ¡ rVtdd„ |D ƒƒsV| j	d|d d S )Nc             s   s   | ]}t  |¡V  qd S )N)r   Úreturns_something)Ú.0Zret_noder   r   r   ú	<genexpr>ì   s    zFDocstringParameterChecker.check_functiondef_returns.<locals>.<genexpr>zredundant-returns-doc)r   )
Úsupports_yieldsÚis_generatorÚis_abstractZnodes_of_classÚastroidZReturnÚhas_returnsÚ	has_rtypeÚanyÚadd_message)r   r   r   Zreturn_nodesr   r   r   r   æ   s    z3DocstringParameterChecker.check_functiondef_returnsc             C   s<   |j r| ¡ rd S | ¡ s"| ¡ r8| ¡ s8| jd|d d S )Nzredundant-yields-doc)r   )r,   r.   Ú
has_yieldsÚhas_yields_typer-   r3   )r   r   r   r   r   r   r   ð   s
    z2DocstringParameterChecker.check_functiondef_yieldsc       	      C   s    |  ¡ }t|tjƒsd S t |¡}|s*d S |jsBt |¡}|rB|}t |j| j	j
¡}| ¡ sr|jrn|  ||¡ d S | ¡ }dd„ |D ƒ}|| }|  ||¡ d S )Nc             S   s   h | ]}|  d ¡d ’qS )Ú.éÿÿÿÿ)Úsplit)r*   Úexcr   r   r   ú	<setcomp>  s    z8DocstringParameterChecker.visit_raise.<locals>.<setcomp>)ÚframeÚ
isinstancer/   ÚFunctionDefr   Zpossible_exc_typesr   Zget_setters_propertyr   r   r   Úis_validÚ_handle_no_raise_docÚ
exceptionsÚ_add_raise_message)	r   r   Ú	func_nodeZexpected_excsZ	property_r   Zfound_excs_full_namesZfound_excs_class_namesÚmissing_excsr   r   r   Úvisit_raiseù   s&    

z%DocstringParameterChecker.visit_raisec             C   s¨   t  |¡sd S | ¡ }t|tjƒs&d S t  |j| jj	¡}| 
¡ sL| jjrLd S t |¡}| ¡ sx| ¡ rj|sx| jd|d |jr‚d S | ¡ s¤| ¡ r–|s¤| jd|d d S )Nzmissing-return-doc)r   zmissing-return-type-doc)r   r)   r;   r<   r/   r=   r   r   r   r   r>   Zaccept_no_return_docr!   Zdecorated_with_propertyr0   Zhas_property_returnsr3   Zreturnsr1   Zhas_property_type)r   r   rB   r   Zis_propertyr   r   r   Úvisit_return  s    

z&DocstringParameterChecker.visit_returnc             C   sŽ   |  ¡ }t|tjƒsd S t |j| jj¡}| 	¡ s>| jj
r>d S |jrV| ¡ }| ¡ }n| ¡ }| ¡ }|sx| jd|d |sŠ| jd|d d S )Nzmissing-yield-doc)r   zmissing-yield-type-doc)r;   r<   r/   r=   r   r   r   r   r   r>   Zaccept_no_yields_docr,   r4   r5   r0   r1   r3   )r   r   rB   r   Zdoc_has_yieldsZdoc_has_yields_typer   r   r   Úvisit_yield.  s    
z%DocstringParameterChecker.visit_yieldc             C   s   |   |¡ d S )N)rF   )r   r   r   r   r   Úvisit_yieldfromD  s    z)DocstringParameterChecker.visit_yieldfromNc                s:  |j s
dS |dkrˆjj}| ¡ ‰dd„ |jD ƒ‰ ˆ  dd„ |jD ƒ¡ ˆj ¡ }|j	dk	rtˆ  
|j	¡ | 
|j	¡ |jdk	r–ˆ  
|j¡ | 
|j¡ | ¡ \}}|s²|s²|r²d‰‡ ‡‡‡fdd„}‡ ‡‡fd	d
„}	||dˆjƒ x,t|jƒD ]\}
}|j|
 rî| 
|j¡ qîW ||d|ƒ |	|dˆjƒ |	|d|ƒ dS )aª  Check that all parameters in a function, method or class constructor
        on the one hand and the parameters mentioned in the parameter
        documentation (e.g. the Sphinx tags 'param' and 'type') on the other
        hand are consistent with each other.

        * Undocumented parameters except 'self' are noticed.
        * Undocumented parameter types except for 'self' and the ``*<args>``
          and ``**<kwargs>`` parameters are noticed.
        * Parameters mentioned in the parameter documentation that don't or no
          longer exist in the function parameter list are noticed.
        * If the text "For the parameters, see" or "For the other parameters,
          see" (ignoring additional whitespace) is mentioned in the docstring,
          missing parameter documentation is tolerated.
        * If there's no Sphinx style, Google style or NumPy style parameter
          documentation at all, i.e. ``:param`` is never mentioned etc., the
          checker assumes that the parameters are documented in another format
          and the absence is tolerated.

        :param doc: Docstring for the function, method or class.
        :type doc: str

        :param arguments_node: Arguments node for the function, method or
            class constructor.
        :type arguments_node: :class:`astroid.scoped_nodes.Arguments`

        :param warning_node: The node to assign the warnings to
        :type warning_node: :class:`astroid.scoped_nodes.Node`

        :param accept_no_param_doc: Whether or not to allow no parameters
            to be documented.
            If None then this value is read from the configuration.
        :type accept_no_param_doc: bool or None
        Nc             S   s   h | ]
}|j ’qS r   )r   )r*   Úargr   r   r   r:   u  s    zIDocstringParameterChecker.check_arguments_in_docstring.<locals>.<setcomp>c             s   s   | ]}|j V  qd S )N)r   )r*   rH   r   r   r   r+   v  s    zIDocstringParameterChecker.check_arguments_in_docstring.<locals>.<genexpr>Tc                s4   ˆs0ˆ |  | }|r0ˆj |d t|ƒ¡fˆd dS )a  Compare the found argument names with the expected ones and
            generate a message if there are arguments missing.

            :param set found_argument_names: argument names found in the
                docstring

            :param str message_id: pylint message id

            :param not_needed_names: names that may be omitted
            :type not_needed_names: set of str
            z, )r&   r   N)r3   ÚjoinÚsorted)Úfound_argument_namesÚ
message_idÚnot_needed_namesZmissing_argument_names)Úexpected_argument_namesr   Útolerate_missing_paramsÚwarning_noder   r   Ú_compare_missing_args…  s    zUDocstringParameterChecker.check_arguments_in_docstring.<locals>._compare_missing_argsc                s4   ˆ | A | ˆ  }|r0ˆj |d t|ƒ¡fˆd dS )a”  Compare the found argument names with the expected ones and
            generate a message if there are extra arguments found.

            :param set found_argument_names: argument names found in the
                docstring

            :param str message_id: pylint message id

            :param not_needed_names: names that may be omitted
            :type not_needed_names: set of str
            z, )r&   r   N)r3   rI   rJ   )rK   rL   rM   Zdiffering_argument_names)rN   r   rP   r   r   Ú_compare_different_argsœ  s    zWDocstringParameterChecker.check_arguments_in_docstring.<locals>._compare_different_argszmissing-param-doczmissing-type-doczdiffering-param-doczdiffering-type-doc)r   r   Úaccept_no_param_docr$   r&   ÚupdateZ
kwonlyargsÚnot_needed_param_in_docstringÚcopyZvarargÚaddZkwargZmatch_param_docsÚ	enumerateZannotationsr   )r   r   Zarguments_noderP   rS   Znot_needed_type_in_docstringZparams_with_docZparams_with_typerQ   rR   ÚindexZarg_namer   )rN   r   rO   rP   r   r%   G  s<    &




z6DocstringParameterChecker.check_arguments_in_docstringc             C   s(   |  ¡ r$|  ¡ r$| jd|jf|d d S )Nzmultiple-constructor-doc)r&   r   )r#   r3   r   )r   r(   Zinit_docr'   r   r   r   r"   È  s    z9DocstringParameterChecker.check_single_constructor_paramsc             C   s   | j jrd S |  ||¡ d S )N)r   Zaccept_no_raise_docrA   )r   Zexcsr   r   r   r   r?   Î  s    z.DocstringParameterChecker._handle_no_raise_docc             C   sT   |  ¡ r,y| d¡ W n tk
r*   Y nX |s4dS | jdd t|ƒ¡f|d dS )a  
        Adds a message on :param:`node` for the missing exception type.

        :param missing_excs: A list of missing exception types.
        :type missing_excs: set(str)

        :param node: The node show the message on.
        :type node: astroid.node_classes.NodeNG
        ÚNotImplementedErrorNzmissing-raises-docz, )r&   r   )r.   ÚremoveÚKeyErrorr3   rI   rJ   )r   rC   r   r   r   r   rA   Ô  s    
z,DocstringParameterChecker._add_raise_message)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Z__implements__r   ZmsgsÚlistr   ZDOCSTRING_TYPESZoptionsZpriorityr    rU   r   r   r   r   rD   rE   rF   rG   r%   r"   r?   rA   r   r   r   r   r      sˆ   




	
 r   c             C   s   |   t| ƒ¡ dS )zRequired method to auto register this checker.

    :param linter: Main interface object for Pylint plugins
    :type linter: Pylint object
    N)Zregister_checkerr   )Zlinterr   r   r   Úregisterì  s    rb   )r`   Z
__future__r   r   r   r/   Zpylint.interfacesr   Zpylint.checkersr   r   r!   Z#pylint.extensions._check_docs_utilsÚ
extensionsZ_check_docs_utilsr   rb   r   r   r   r   Ú<module>   s      R