3
tûœ])^ ã               @   sâ  d Z ddlZddlZddlZddlZddlZddlmZ ddlZddl	Zddl
ZddlmZ ddljZddlmZmZmZ ddlmZ ddlmZmZ ddlmZ G d	d
„ d
ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ eeeee dœZ!ej"dƒZ#dZ$dsZ%e%e$ffZ&e'dtƒZ(ej)ej*ej+ej,fZ-d Z.ej/Z0d!e0 Z1d"d#hZ2d$d%hZ3e4e5d&d'„ duD ƒd+d,d-gƒƒZ6d.d/d0d1d2œZ7e'dvƒZ8e'dwƒZ9d4Z:d5d6„ Z;d7d8„ Z<d9d:„ Z=d;d<„ Z>d=d>„ Z?d?d@„ Z@dAZAdBdC„ ZBdxdDdE„ZCdFdG„ ZDdHdI„ ZEdJdK„ ZFG dLdM„ dMejGƒZHG dNdO„ dOeHƒZIG dPdQ„ dQeHƒZJdRdSdTdUdVdWdXdYdZd[h
ZKdRd\dTdUdVd]dXdYd^d_d`œ
ZLdadbdcdadadadadaddddd`œ
ZMdedf„ ZNG dgdh„ dheHƒZOG didj„ djeHƒZPG dkdl„ dleHƒZQdmdn„ ZRG dodp„ dpeHƒZSdqdr„ ZTdS )yzbasic checker for Python codeé    N)ÚPattern)ÚCallSite)ÚcheckersÚ
exceptionsÚ
interfaces)Úutils)Úis_property_deleterÚis_property_setter)Únodesc               @   s0   e Zd ZdZdZdZdZdZdZe	dd„ ƒZ
dS )ÚNamingStyleNc             C   s2   | j | j| j| j| j| j| j| j| j| jdœ
| S )N)
ÚmoduleÚconstÚclassÚfunctionÚmethodÚattrÚargumentÚvariableÚclass_attributeÚ	inlinevar)ÚMOD_NAME_RGXÚCONST_NAME_RGXÚCLASS_NAME_RGXÚDEFAULT_NAME_RGXÚCLASS_ATTRIBUTE_RGXÚCOMP_VAR_RGX)ÚclsÚ	name_type© r   ú6/tmp/pip-build-fn42xldr/pylint/pylint/checkers/base.pyÚ	get_regexG   s    zNamingStyle.get_regex)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   Úclassmethodr    r   r   r   r   r   ;   s   r   c               @   sL   e Zd ZdZejdƒZejdƒZejdƒZejdƒZ	ejdƒZ
ejdƒZdS )	ÚSnakeCaseStylez(Regex rules for snake_case naming style.z[a-z_][a-z0-9_]+$z([a-z_][a-z0-9_]*)$z(([a-z_][a-z0-9_]*)|(__.*__))$z[a-z_][a-z0-9_]*$z<(([a-z_][a-z0-9_]{2,})|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$z!(([a-z_][a-z0-9_]{2,}|(__.*__)))$N)r!   r"   r#   Ú__doc__ÚreÚcompiler   r   r   r   r   r   r   r   r   r   r%   W   s   



r%   c               @   sL   e Zd ZdZejdƒZejdƒZejdƒZejdƒZ	ejdƒZ
ejdƒZdS )	ÚCamelCaseStylez'Regex rules for camelCase naming style.z[a-z_][a-zA-Z0-9]+$z([a-z_][a-zA-Z0-9]*)$z (([a-z_][A-Za-z0-9]*)|(__.*__))$z[a-z_][A-Za-z0-9]*$z3(([a-z_][a-zA-Z0-9]{2,})|(__[a-z][a-zA-Z0-9_]+__))$z!([a-z_][A-Za-z0-9]{2,}|(__.*__))$N)r!   r"   r#   r&   r'   r(   r   r   r   r   r   r   r   r   r   r   r)   d   s   




r)   c               @   sL   e Zd ZdZejdƒZejdƒZejdƒZejdƒZ	ejdƒZ
ejdƒZdS )ÚPascalCaseStylez(Regex rules for PascalCase naming style.z[A-Z_][a-zA-Z0-9]+$z (([A-Z_][A-Za-z0-9]*)|(__.*__))$z0[A-Z_][a-zA-Z0-9]{2,}$|(__[a-z][a-zA-Z0-9_]+__)$z[A-Z_][a-zA-Z0-9]{2,}$N)r!   r"   r#   r&   r'   r(   r   r   r   r   r   r   r   r   r   r   r*   o   s   




r*   c               @   sL   e Zd ZdZejdƒZejdƒZejdƒZejdƒZ	ejdƒZ
ejdƒZdS )ÚUpperCaseStylez(Regex rules for UPPER_CASE naming style.z[A-Z_][A-Z0-9_]+$z(([A-Z_][A-Z0-9_]*)|(__.*__))$z/([A-Z_][A-Z0-9_]{2,})|(__[a-z][a-zA-Z0-9_]+__)$z[A-Z_][A-Z0-9_]{2,}$N)r!   r"   r#   r&   r'   r(   r   r   r   r   r   r   r   r   r   r   r+   z   s   




r+   c               @   s   e Zd Zedd„ ƒZdS )ÚAnyStylec             C   s
   t jdƒS )Nz.*)r'   r(   )r   r   r   r   r   r    †   s    zAnyStyle.get_regexN)r!   r"   r#   r$   r    r   r   r   r   r,   …   s   r,   )Ú
snake_caseZ	camelCaseÚ
PascalCaseÚ
UPPER_CASEÚanyz^_Ú__reversed__Ú__getitem__Ú__len__Úisúis notú==ú!=Úinúnot inzunittest.casez%s.typez_py_abc.ABCMetazabc.ABCMetaZexemptÚignorec             C   s   g | ]}d j t|gƒ‘qS )Ú.)ÚjoinÚBUILTINS)Ú.0Úxr   r   r   ú
<listcomp>¦   s    r@   ÚsetÚdictÚlistzset()z{}z[]ú>ú>=ú<ú<=)rF   z<=rD   z>=r"   ztyping.ForwardRefc             C   sœ   | }x|r"t |jtjƒ r"|j}qW | s:tj|jtƒ r>dS |jj}xP|jtjtj	fƒD ]:}x4|j
D ]*\}}|r‚|| jkr�dS qf|| jkrfdS qfW qZW dS )zÄ Detect that the given node (AssignName) is inside an
    exception handler and redefines an import from the tryexcept body.
    Returns True if the node redefines an import, False otherwise.
    FT)Ú
isinstanceÚparentÚastroidÚExceptHandlerr   Zerror_of_typeÚImportErrorÚnodes_of_classÚ
ImportFromÚImportÚnamesÚname)ÚnodeÚcurrentZ	try_blockZimport_noderQ   Úaliasr   r   r   Ú_redefines_import±   s    


rU   c             C   s@   | j }x4|dk	r:t|tjtjtjtjtjfƒr2dS |j }qW dS )z4return True if the node is inside a kind of for loopNTF)rI   rH   rJ   ÚForÚListCompÚSetCompÚDictCompÚGeneratorExp)rR   rI   r   r   r   Úin_loopÆ   s    


r[   c             C   s<   x6| D ].}t |ttfƒr(t||ƒr4dS q||krdS qW dS )zUreturn true if the object is an element of <nested_list> or of a nested
    list
    TF)rH   rC   ÚtupleÚin_nested_list)Znested_listÚobjZelmtr   r   r   r]   Ù   s    

r]   c             C   sL   t jt jf}| j}x4t||ƒ s0| t|dg ƒkrF|} |j}|dkrP qW |S )zì
    Returns the loop node that holds the break node in arguments.

    Args:
        break_node (astroid.Break): the break node of interest.

    Returns:
        astroid.For or astroid.While: the loop node holding the break node.
    ÚorelseN)rJ   rV   ÚWhilerI   rH   Úgetattr)Z
break_nodeÚ
loop_nodesrI   r   r   r   Ú_get_break_loop_nodeæ   s    
rc   c                sV   t jt jf}t jt jf}‡fdd„ˆj||d�D ƒ‰ t‡ fdd„ˆjt j|d�D ƒƒS )zñ
    Returns true if a loop may ends up in a break statement.

    Args:
        loop (astroid.For, astroid.While): the loop node inspected.

    Returns:
        bool: True if the loop may ends up in a break statement, False otherwise.
    c                s   g | ]}|ˆ kr|‘qS r   r   )r>   Ú_node)Úloopr   r   r@   	  s   z%_loop_exits_early.<locals>.<listcomp>)Ú
skip_klassc             3   s   | ]}t |ƒˆ kr|V  qd S )N)rc   )r>   rd   )Úinner_loop_nodesr   r   ú	<genexpr>  s   z$_loop_exits_early.<locals>.<genexpr>)rJ   rV   r`   ÚFunctionDefÚClassDefrM   r0   ÚBreak)re   rb   Zdefinition_nodesr   )rg   re   r   Ú_loop_exits_earlyü   s    


rl   c             C   s.   | d k	o,| j d k	o,| j tko,|dkp,|tjkS )Nr   )Ú	lastgroupÚEXEMPT_NAME_CATEGORIESr   ÚINFERENCE_FAILURE)ÚmatchÚ	node_typeÚ
confidencer   r   r   Ú_is_multi_naming_match  s    

rs   zbuiltins.propertyc             C   s>   t h}tƒ }| dk	r6|j| jƒ |jdd„ | jD ƒƒ ||fS )zÇReturns a tuple of property classes and names.

    Property classes are fully qualified, such as 'abc.abstractproperty' and
    property names are the actual names, such as 'abstract_property'.
    Nc             s   s   | ]}|j d dƒd V  qdS )r;   é   Néÿÿÿÿ)Úrsplit)r>   Úpropr   r   r   rh   +  s    z"_get_properties.<locals>.<genexpr>)ÚBUILTIN_PROPERTYrA   ÚupdateÚproperty_classes)Úconfigrz   Úproperty_namesr   r   r   Ú_get_properties   s    r}   c             C   s’   t |ƒ\}}| jƒ sdS t| ƒs(t| ƒr,dS | jr<| jj}ng }xL|D ]D}t|tjƒslt|tj	ƒrF|j
|krFtj|ƒ}|rF|jƒ |krFdS qFW dS )a\  Determine the name type whose regex the a function's name should match.

    :param node: A function node.
    :type node: astroid.node_classes.NodeNG
    :param config: Configuration from which to pull additional property classes.
    :type config: :class:`optparse.Values`

    :returns: One of ('function', 'method', 'attr')
    :rtype: str
    r   r   r   )r}   Ú	is_methodr	   r   Ú
decoratorsr
   rH   rJ   ÚNameÚ	AttributeÚattrnamer   Ú
safe_inferÚqname)rR   r{   rz   r|   r   Ú	decoratorÚinferredr   r   r   Ú_determine_function_name_type0  s     



r‡   c             C   s   t tj| ƒƒdkS )z”
    Determine if the given `node` has abstract methods.

    The methods should be made abstract by decorating them
    with `abc` decorators.
    r   )Úlenr   Zunimplemented_abstract_methods)rR   r   r   r   Ú_has_abstract_methodsU  s    r‰   c       
      C   sN  i }xâdD ]Ú}y|| }W n t k
r6   tjƒ ‚Y nX i ||< |dkr
y0||d|   }|d | }d| || d	< W n  t k
r˜   d
|| d	< Y nX y(|d|  d | }d| || d< W q
 t k
râ   d
|| d< Y q
X q
W d}xFdD ]>}|| }	||t|	ƒd
d
|| jd	dƒ|| jddƒf7 }� qòW | jtj|ddd�ƒ dS )zymake a report of

    * percentage of different types documented
    * percentage of different types with a bad name
    r   r   r   r   r   Úundocumented_g      Y@z%.2fZpercent_documentedZNCÚbadname_Zpercent_badnameÚtypeÚnumberú
old numberÚ
differenceú%documentedú%badnameÚ0é   rt   )ÚchildrenÚcolsZrheadersN)r   r   r   r   )rŒ   r�   rŽ   r�   r�   r‘   )r   r   r   r   )ÚKeyErrorr   ZEmptyReportErrorÚstrÚgetÚappendÚreporter_nodesZTable)
ÚsectÚstatsÚ_Z
nice_statsrq   ÚtotalZ
documentedÚpercentÚlinesÚnewr   r   r   Úreport_by_type_stats_  s<    

r¢   c             C   s@   | j r<x4| j jD ](}t|tjƒrt|jddƒ| jkrdS qW dS )zÉreturn True if the object is a method redefined via decorator.

    For example:
        @property
        def x(self): return self._x
        @x.setter
        def x(self, value): self._x = value
    rQ   NTF)r   r
   rH   rJ   r�   ra   ÚexprrQ   )rR   r…   r   r   r   Úredefined_by_decorator‡  s    	r¤   c               @   s   e Zd ZejZdZdS )Ú_BasicCheckerÚbasicN)r!   r"   r#   r   ÚIAstroidCheckerÚ__implements__rQ   r   r   r   r   r¥   š  s   r¥   c               @   s¦  e Zd Zdkdldmdndodpdddddqifdrdsdtdudvdwdxdydzd{d6d7d8d9d|ifd;œZejdƒd<d=„ ƒZd>d?„ Zejd%d(ƒd@dA„ ƒZejd+ƒdBdC„ ƒZ	ejdddddd.d7ƒdDdE„ ƒZ
e
ZdFdG„ ZdHdI„ ZejdƒdJdK„ ƒZejdƒdLdM„ ƒZejdƒdNdO„ ƒZejdd1ƒdPdQ„ ƒZejdƒdRdS„ ƒZejd"ƒdTdU„ ƒZejd"ƒdVdW„ ƒZejdƒdXdY„ ƒZdZd[„ Zejd4ƒd\d]„ ƒZejdƒd^d_„ ƒZd`da„ Zdbdc„ Zddde„ Zdfdg„ Zdhdi„ ZdjS )}ÚBasicErrorCheckerú__init__ method is a generatorúinit-is-generatorú^Used when the special class method __init__ is turned into a generator by a yield in its body.úExplicit return in __init__úreturn-in-initúIUsed when the special class method __init__ has an explicit return value.ú%s already defined line %súfunction-redefinedú3Used when a function / class / method is redefined.ú%r not properly in loopúnot-in-loopú=Used when break or continue keywords are used outside a loop.úReturn outside functionúreturn-outside-functionúEUsed when a "return" statement is found outside a function or method.úYield outside functionúyield-outside-functionúDUsed when a "yield" statement is found outside a function or method.z%Return with argument inside generatorzreturn-arg-in-generatorz‡Used when a "return" statement with an argument is found outside in a generator function or method (e.g. with some "yield" statements).Z
maxversioné   ú#Use of the non-existent %s operatorúnonexistent-operatorúzUsed when you attempt to use the C-style pre-increment or pre-decrement operator -- and ++, which doesn't exist in Python.ú1Duplicate argument name %s in function definitionúduplicate-argument-nameúCDuplicate argument names in function definitions are syntax errors.ú4Abstract class %r with abstract methods instantiatedúabstract-class-instantiatedúeUsed when an abstract class with `abc.ABCMeta` as metaclass has abstract methods and is instantiated.ú-Else clause on loop without a break statementúuseless-else-on-loopú¨Loops should only have an else clause if they can exit early with a break statement, otherwise the statements under else should be on the same scope as the loop itself.ú.More than one starred expression in assignmentútoo-many-star-expressionsúhEmitted when there are more than one starred expressions (`*x`) in an assignment. This is a SyntaxError.ú4Starred assignment target must be in a list or tupleúinvalid-star-assignment-targetúFEmitted when a star expression is used as a starred assignment target.ú4Can use starred expression only in assignment targetústar-needs-assignment-targetúCEmitted when a star expression is not used in an assignment target.úName %r is nonlocal and globalúnonlocal-and-globalú0Emitted when a name is both nonlocal and global.ú0'continue' not supported inside 'finally' clauseúcontinue-in-finallyú]Emitted when the `continue` keyword is found inside a finally clause, which is a SyntaxError.ú&nonlocal name %s found without bindingúnonlocal-without-bindingú^Emitted when a nonlocal variable does not have an attached name somewhere in the parent scopesz+Name %r is used prior to global declarationzused-prior-global-declarationzcEmitted when a name is used prior a global declaration, which results in an error since Python 3.6.Z
minversionr“   )ZE0100ZE0101ZE0102ZE0103ZE0104ZE0105ZE0106ZE0107ZE0108ZE0110ZW0120ZE0112ZE0113ZE0114ZE0115ZE0116ZE0117ZE0118c             C   s   | j d|ƒ d S )Nr   )Ú_check_redefinition)ÚselfrR   r   r   r   Úvisit_classdef  s    z BasicErrorChecker.visit_classdefc             C   sH   d}x:|j ƒ D ].}t|tjƒr(| j|ƒS t|tjƒr|d7 }qW |dkS )Nr   rt   )ZiteredrH   rJ   ÚTupleÚ_too_many_starred_for_tupleÚStarred)rÜ   Zassign_tupleZstarred_countÚelemr   r   r   rß     s    
z-BasicErrorChecker._too_many_starred_for_tuplec             C   sV   |j d }t|j d tjƒr*| jd|d� t|tjƒs:d S | j|ƒrR| jd|d� d S )Nr   zinvalid-star-assignment-target)rR   ztoo-many-star-expressions)ÚtargetsrH   rJ   rà   Úadd_messagerÞ   rß   )rÜ   rR   Zassign_targetr   r   r   Úvisit_assign  s    

zBasicErrorChecker.visit_assignc             C   sr   t |jtjƒrdS t |jtjtjtjtjfƒr2dS |jƒ }t |tj	ƒsJdS |j
|ks`|j
j|ƒrn| jd|d� dS )z@Check that a Starred expression is used in an assignment target.Nzstar-needs-assignment-target)rR   )rH   rI   rJ   ÚCallÚListrÞ   ÚSetÚDictZ	statementÚAssignÚvalueZ	parent_ofrã   )rÜ   rR   Ústmtr   r   r   Úvisit_starred%  s    zBasicErrorChecker.visit_starredc       	      C   sF  | j |ƒ | j|ƒ t|ƒ rBtj|ƒ rB| j|jƒ r:dp<d|ƒ |jtj	tj
tjfd�}|jƒ r²|jdkr²|jƒ r„| jd|d� n.dd„ |D ƒ}td	d
„ |D ƒƒr²| jd|d� tjtƒ}td |jj|jjgƒ}x$tjj|ƒD ]}||j j|ƒ qàW xH|jƒ D ]<}t|ƒdk�rx&|D ]}| jd|j||jfd� �qW �qW d S )Nr   r   )rf   Ú__init__zinit-is-generator)rR   c             S   s   g | ]
}|j ‘qS r   )rê   )r>   Úrr   r   r   r@   Q  s    z7BasicErrorChecker.visit_functiondef.<locals>.<listcomp>c             s   s   | ]}t j|ƒs|V  qd S )N)r   Zis_none)r>   Úvr   r   r   rh   S  s    z6BasicErrorChecker.visit_functiondef.<locals>.<genexpr>zreturn-in-initrt   zduplicate-argument-name)ÚlinerR   Úargs)Ú_check_nonlocal_and_globalÚ_check_name_used_prior_globalr¤   r   Z(is_registered_in_singledispatch_functionrÛ   r~   rM   rJ   ÚReturnri   rj   rQ   Zis_generatorrã   r0   ÚcollectionsÚdefaultdictrC   Úfilterrñ   Ú
kwonlyargsÚ	itertoolsÚchainÚfrom_iterabler™   Úvaluesrˆ   Úlineno)	rÜ   rR   Zreturnsrü   Zarg_clustersÚ	argumentsÚargZargument_duplicatesr   r   r   r   Úvisit_functiondef9  s4    




z#BasicErrorChecker.visit_functiondefc                s†   ‡ fdd„ˆ j tjƒD ƒ}|s"d S x^ˆ j tjƒD ]N}|jƒ ˆ k	rBq0|j}|j|ƒ}|sXq0|j}|r0||jkr0| jd||fd� q0W d S )Nc                s*   i | ]"}|j D ]}|jƒ ˆ kr||“qqS r   )rP   Úscope)r>   ÚchildrQ   )rR   r   r   ú
<dictcomp>k  s   
zCBasicErrorChecker._check_name_used_prior_global.<locals>.<dictcomp>zused-prior-global-declaration)rR   rñ   )	rM   rJ   ÚGlobalr€   r  rQ   r˜   Ú
fromlinenorã   )rÜ   rR   Zscope_globalsÚ	node_namerQ   Zcorresponding_globalZglobal_linenor   )rR   r   ró   i  s    

z/BasicErrorChecker._check_name_used_prior_globalc                sŠ   ‡ fdd„‰t jj}t|‡fdd„ˆ jtjƒD ƒƒƒ}|s>dS t|‡fdd„ˆ jtjƒD ƒƒƒ}x$|j|ƒD ]}| j	d|fˆ d� qlW dS )	z.Check that a name is both nonlocal and global.c                s   | j ƒ ˆ kS )N)r  )rS   )rR   r   r   Ú
same_scope‡  s    z@BasicErrorChecker._check_nonlocal_and_global.<locals>.same_scopec             3   s   | ]}ˆ |ƒr|j V  qd S )N)rP   )r>   r  )r  r   r   rh   �  s   z?BasicErrorChecker._check_nonlocal_and_global.<locals>.<genexpr>Nc             3   s   | ]}ˆ |ƒr|j V  qd S )N)rP   )r>   r  )r  r   r   rh   ˜  s   znonlocal-and-global)rñ   rR   )
rù   rú   rû   rA   rM   rJ   ÚNonlocalr  Úintersectionrã   )rÜ   rR   Z	from_iterÚ	nonlocalsÚglobal_varsrQ   r   )rR   r  r   rò   „  s    

z,BasicErrorChecker._check_nonlocal_and_globalc             C   s"   t |jƒ tjƒs| jd|d� d S )Nzreturn-outside-function)rR   )rH   ÚframerJ   ri   rã   )rÜ   rR   r   r   r   Úvisit_return   s    zBasicErrorChecker.visit_returnc             C   s   | j |ƒ d S )N)Ú_check_yield_outside_func)rÜ   rR   r   r   r   Úvisit_yield¥  s    zBasicErrorChecker.visit_yieldc             C   s   | j |ƒ d S )N)r  )rÜ   rR   r   r   r   Úvisit_yieldfrom©  s    z!BasicErrorChecker.visit_yieldfromc             C   s   | j |dƒ d S )NÚcontinue)Ú_check_in_loop)rÜ   rR   r   r   r   Úvisit_continue­  s    z BasicErrorChecker.visit_continuec             C   s   | j |dƒ d S )NÚbreak)r  )rÜ   rR   r   r   r   Úvisit_break±  s    zBasicErrorChecker.visit_breakc             C   s   | j |ƒ d S )N)Ú_check_else_on_loop)rÜ   rR   r   r   r   Ú	visit_forµ  s    zBasicErrorChecker.visit_forc             C   s   | j |ƒ d S )N)r  )rÜ   rR   r   r   r   Úvisit_while¹  s    zBasicErrorChecker.visit_whilec             C   s@   |j dkr<t|jtjƒr<|jj |j kr<| jd||j d d� dS )z9check use of the non-existent ++ and -- operator operatorz+-znonexistent-operatoré   )rR   rñ   N)ÚoprH   ÚoperandrJ   ÚUnaryOprã   )rÜ   rR   r   r   r   Úvisit_unaryop½  s    
zBasicErrorChecker.visit_unaryopc             C   sz   |j ƒ }xN|jd krP t|tjtjfƒs>| jd|f|d� d S ||jkrT|jj ƒ }q
d S t|tjƒsv| jd|f|d� d S )Nznonlocal-without-binding)rñ   rR   )r  rI   rH   rJ   rj   ri   rã   Úlocals)rÜ   rR   rQ   Zcurrent_scoper   r   r   Ú_check_nonlocal_without_bindingÇ  s    


z1BasicErrorChecker._check_nonlocal_without_bindingc             C   s    x|j D ]}| j||ƒ qW d S )N)rP   r  )rÜ   rR   rQ   r   r   r   Úvisit_nonlocalÛ  s    z BasicErrorChecker.visit_nonlocalc             C   s@   y$x|j jƒ D ]}| j||ƒ qW W n tjk
r:   dS X dS )zS Check instantiating abstract class with
        abc.ABCMeta as metaclass.
        N)ÚfuncÚinferÚ!_check_inferred_class_is_abstractrJ   ÚInferenceError)rÜ   rR   r†   r   r   r   Ú
visit_callà  s
    zBasicErrorChecker.visit_callc             C   s¢   t |tjƒsd S tj|ƒ}||kr&d S t|ƒ}|s6d S |jƒ }|d kr~x2|jƒ D ]&}|jƒ dkrP| j	d|j
f|d� P qPW d S |jƒ tkrž| j	d|j
f|d� d S )Nzabc.ABCzabstract-class-instantiated)rñ   rR   )rH   rJ   rj   r   Znode_frame_classr‰   Ú	metaclassÚ	ancestorsr„   rã   rQ   ÚABC_METACLASSES)rÜ   r†   rR   ÚklassZabstract_methodsr&  Úancestorr   r   r   r#  ë  s&    
z3BasicErrorChecker._check_inferred_class_is_abstractc             C   s(   t |jƒ tjtjfƒs$| jd|d� d S )Nzyield-outside-function)rR   )rH   r  rJ   ri   ÚLambdarã   )rÜ   rR   r   r   r   r    s    z+BasicErrorChecker._check_yield_outside_funcc             C   s0   |j r,t|ƒ r,| jd||j d jd d� dS )z>Check that any loop with an else clause has a break statement.zuseless-else-on-loopr   rt   )rR   rð   N)r_   rl   rã   rý   )rÜ   rR   r   r   r   r    s
    z%BasicErrorChecker._check_else_on_loopc             C   sŽ   |j }xr|rxt|tjtjfƒr,||jkr,dS t|tjtjfƒr@P t|tjƒrp||j	krpt|tj
ƒrp| jd|d� |j }qW | jd||d� dS )z/check that a node is inside a for or while loopNzcontinue-in-finally)rR   znot-in-loop)rR   rñ   )rI   rH   rJ   rV   r`   r_   rj   ri   Z
TryFinallyÚ	finalbodyÚContinuerã   )rÜ   rR   r  rd   r   r   r   r     s    


z BasicErrorChecker._check_in_loopc       
      C   s  |j jƒ }|j|j }tdd„ |D ƒ|ƒ}||k	o>tj||ƒ �rt|tjƒr\|jt	kr\dS t
j|ƒrjdS y|j|ƒ}W n tk
rŒ   Y nBX x>|d|… D ].}t
j|ƒ}|rœt|tjƒrœ|jƒ tkrœdS qœW tj| ddd�}	|	rò|	j|jƒròdS | jd|||jfd� dS )z:check for redefinition of a function / method / class namec             s   s   | ]}t j|ƒs|V  qd S )N)r   Úis_overload_stub)r>   Úlocalr   r   r   rh   <  s    z8BasicErrorChecker._check_redefinition.<locals>.<genexpr>Nzdummy-variables-rgx)Údefaultzfunction-redefined)rR   rñ   )rI   r  r  rQ   ÚnextrJ   Zare_exclusiverH   rj   ÚREDEFINABLE_METHODSr   r.  ÚindexÚ
ValueErrorrƒ   ÚInstancer„   ÚTYPING_FORWARD_REF_QNAMEÚ
lint_utilsZget_global_optionrp   rã   r  )
rÜ   Z	redeftyperR   Zparent_frameZredefinitionsZdefined_selfZredefinition_indexZredefinitionr†   Zdummy_variables_rgxr   r   r   rÛ   5  s:    



z%BasicErrorChecker._check_redefinitionN)rª   r«   r¬   )r­   r®   r¯   )r°   r±   r²   )r³   r´   rµ   )r¶   r·   r¸   )r¹   rº   r»   )r¼   r¼   )r½   r¾   r¿   )rÀ   rÁ   rÂ   )rÃ   rÄ   rÅ   )rÆ   rÇ   rÈ   )rÉ   rÊ   rË   )rÌ   rÍ   rÎ   )rÏ   rÐ   rÑ   )rÒ   rÓ   rÔ   )rÕ   rÖ   r×   )rØ   rÙ   rÚ   )r¼   r“   )r!   r"   r#   Úmsgsr   Úcheck_messagesrÝ   rß   rä   rì   r   Úvisit_asyncfunctiondefró   rò   r  r  r  r  r  r  r  r  r  r   r%  r#  r  r  r  rÛ   r   r   r   r   r©   Ÿ  s®                                   	'
%r©   c               @   s(  e Zd ZdZejZdZdd€d�d‚dƒd„d…d†d‡dˆd‰dŠd‹dŒd�dŽd�d�d9œZd:d;e	ffZ
d<d=„ Zd>d?„ Zejd(d+ƒd@dA„ ƒZejd(d+ƒdBdC„ ƒZejd(d+ƒdDdE„ ƒZdFdG„ ZdHdI„ ZdJdK„ Zejd
ddƒdLdM„ ƒZedNdO„ ƒZedPdQ„ ƒZejdƒdRdS„ ƒZejdƒdTdU„ ƒZeZdVdW„ ZejddƒdXdY„ ƒZejdƒdZd[„ ƒZejddƒd\d]„ ƒZejdƒd^d_„ ƒZ ejdƒd`da„ ƒZ!dbdc„ Z"ejddd4d7ƒddde„ ƒZ#ejd"ƒdfdg„ ƒZ$ejdƒdhdi„ ƒZ%djdk„ Z&dldm„ Z'dndo„ Z(f fdpdq„Z)drds„ Z*ejd%ƒdtdu„ ƒZ+dvdw„ Z,dxdy„ Z-ejd.d1ƒdzd{„ ƒZ.ejd1ƒd|d}„ ƒZ/d~S )‘ÚBasicCheckera*  checks for :
    * doc strings
    * number of arguments, local variables, branches, returns and statements in
    functions, methods
    * required module attributes
    * dangerous default values as arguments
    * redefinition of function / method / class
    * uses of the global statement
    r¦   úUnreachable codeÚunreachableúbUsed when there is some code behind a "return" or "raise" statement, which will never be accessed.ú&Dangerous default value %s as argumentúdangerous-default-valueú_Used when a mutable value as list or dictionary is detected in a default value for an argument.ú!Statement seems to have no effectúpointless-statementúEUsed when a statement doesn't have (or at least seems to) any effect.úString statement has no effectúpointless-string-statementúäUsed when a string is used as a statement (which of course has no effect). This is a particular case of W0104 with its own message so you can easily disable it if you're using those strings as documentation, instead of comments.ú&Expression "%s" is assigned to nothingúexpression-not-assignedúqUsed when an expression that is not a function call is assigned to nothing. Probably something else was intended.úLambda may not be necessaryúunnecessary-lambdaúåUsed when the body of a lambda expression is a function call on the same argument list as the lambda itself; such lambda expressions are in all but a few cases replaceable with the function being called in the body of the lambda.úDuplicate key %r in dictionaryúduplicate-keyúDUsed when a dictionary expression binds the same key multiple times.úUse of execú	exec-usedú~Used when you use the "exec" statement (function for Python 3), to discourage its usage. That doesn't mean you cannot use it !úUse of evalú	eval-usedú¶Used when you use the "eval" function, to discourage its usage. Consider using `ast.literal_eval` for safely evaluating strings containing Python expressions from untrusted sources. ú3%s statement in finally block may swallow exceptionúlost-exceptionúÃUsed when a break or a return statement is found inside the finally clause of a try...finally block: the exceptions raised in the try clause will be silently swallowed instead of being re-raised.ú;Assert called on a 2-item-tuple. Did you mean 'assert x,y'?úassert-on-tupleúA call of assert on a tuple will always evaluate to true if the tuple is not empty, and will always evaluate to false if it is.ú?Following "as" with another context manager looks like a tuple.úconfusing-with-statementá2  Emitted when a `with` statement component returns multiple values and uses name binding with `as` only for a part of those values, as in with ctx() as a, b. This can be misleading, since it's not clear if the context manager returns a tuple or if the node without a name binding is another context manager.ú3Using a conditional statement with a constant valueúusing-constant-testú‹Emitted when a conditional statement (If or ternary if) uses a constant value for its test. This might not be what the user intended to do.úgUsing a conditional statement with potentially wrong function or method call due to missing parenthesesú$missing-parentheses-for-call-in-testúsEmitted when a conditional statement (If or ternary if) seems to wrongly call a function due to missing parenthesesú(Assigning the same variable %r to itselfúself-assigning-variableú<Emitted when we detect that a variable is assigned to itselfú$Redeclared variable %r in assignmentúredeclared-assigned-nameúMEmitted when we detect that a variable was redeclared in the same assignment.ú/The first reversed() argument is not a sequenceúbad-reversed-sequenceú�Used when the first argument to reversed() builtin isn't a sequence (does not implement __reversed__, nor __getitem__ and __len__ú$format function is not called on strúmisplaced-format-functionú½Emitted when format function is not called on str object. e.g doing print("value: {}").format(123) instead of print("value: {}".format(123)). This might not be what the user intended to do.)ZW0101ZW0102ZW0104ZW0105ZW0106ZW0108ZW0109ZW0122ZW0123ZW0150ZW0199ZW0124ZW0125ZW0126ZW0127ZW0128ZE0111ZE0119ZRP0101zStatistics by typec             C   s   t j| |ƒ d | _d | _d S )N)r¥   rí   rœ   Ú_tryfinallys)rÜ   Úlinterr   r   r   rí   ó  s    zBasicChecker.__init__c             C   s    g | _ | jjddddd�| _dS )z2initialize visit variables and statistics
        r   )r   r   r   Úclass_N)rr  rs  Ú	add_statsrœ   )rÜ   r   r   r   Úopenø  s    zBasicChecker.openc             C   s   | j ||jƒ d S )N)Ú_check_using_constant_testÚtest)rÜ   rR   r   r   r   Úvisit_ifþ  s    zBasicChecker.visit_ifc             C   s   | j ||jƒ d S )N)rw  rx  )rÜ   rR   r   r   r   Úvisit_ifexp  s    zBasicChecker.visit_ifexpc             C   s&   |j r"x|j D ]}| j||ƒ qW d S )N)Zifsrw  )rÜ   rR   Zif_testr   r   r   Úvisit_comprehension  s    z BasicChecker.visit_comprehensionc       
      C   s0  t jt jjt jt jt jt jjt j	t j
t jf	}t jt jt jf}t jt jt jt jt jf}d }t|t jf| | ƒ}t||ƒs‚tj|ƒ}|r–| jd|d� n–t||ƒ�r,d }t|t jƒr¼|jƒ }nt|t jƒrÒ|j|ƒ}|�ry,x&|D ]}	|	t jkrà| jd|d� P qàW W n t jk
�r   Y nX | jd|d� d S )Nzusing-constant-test)rR   z$missing-parentheses-for-call-in-test)rJ   ÚModuleZscoped_nodesrZ   r+  ri   rj   ÚbasesÚ	GeneratorZUnboundMethodÚBoundMethodrè   rÞ   rç   rå   ÚBinOpÚBoolOpr  Ú	SubscriptrH   ÚConstr   rƒ   rã   Zinfer_call_resultÚUninferabler$  )
rÜ   rR   rx  Zconst_nodesÚstructsZexcept_nodesr†   ÚemitZcall_inferredZinf_callr   r   r   rw    sJ    







z'BasicChecker._check_using_constant_testc             C   s   | j d  d7  < dS )z<check module name, docstring and required arguments
        r   rt   N)rœ   )rÜ   r�   r   r   r   Úvisit_module>  s    zBasicChecker.visit_modulec             C   s   | j d  d7  < dS )zWcheck module name, docstring and redefinition
        increment branch counter
        r   rt   N)rœ   )rÜ   rR   r   r   r   rÝ   C  s    zBasicChecker.visit_classdefc             C   s"  |j }t|tjƒr˜t|j tƒr˜|jƒ }t|tjtjtjfƒr†t|tjƒrT|j	dkrTn2|j
ƒ }|dk	r†|jƒ |kr†t|tjtjfƒr†dS | jd|d� dS t|tjtjtjtjfƒsät|jtjƒrÎ|jj|gksät|tjƒrè|j tkrèdS t|jtjƒƒ�r| jd||jƒ d� n| jd|d� dS )z3Check for various kind of statements without effectrí   Nzpointless-string-statement)rR   zexpression-not-assigned)rR   rñ   zpointless-statement)rê   rH   rJ   rƒ  r—   r  rj   r|  ri   rQ   Zprevious_siblingré   Ú	AnnAssignrã   ÚYieldÚAwaitÚEllipsisrå   rI   Z	TryExceptÚbodyr0   rM   Ú	as_string)rÜ   rR   r£   r  Zsiblingr   r   r   Ú
visit_exprI  s0    
zBasicChecker.visit_exprc             c   sJ   xD|D ]<}t |tjƒr<t |jtjƒrB|jj| jjkrB|V  q|V  qW d S )N)rH   rJ   rà   rê   r€   rQ   rñ   Úvararg)rR   Ú	call_argsrÿ   r   r   r   Ú_filter_vararg~  s    
zBasicChecker._filter_varargc             C   s>   | sdS x0| D ](}t |jtjƒr2|jj|kr6dS qdS qW dS )NTF)rH   rê   rJ   r€   rQ   )rñ   Zvariadic_namerÿ   r   r   r   Ú_has_variadic_argumentŒ  s    
z#BasicChecker._has_variadic_argumentc       	      C   sd  |j jrdS |j}t|tjƒs"dS t|jjtjƒrHt|jjjtjƒrHdS t	j
|ƒ}t|j j ƒ}t| j||j ƒƒ}|j jrŽ| j|j|j jƒrŽdS |j jr®| j|j|j jƒr¸dS n
|jr¸dS |jrúdd„ |j jD ƒ}t|ƒt|jƒkrædS t|jƒj|ƒrúdS t|ƒt|ƒk�rdS x<t||ƒD ].\}}t|tjƒ�s6dS |j|jk�rdS �qW | jd|j|d� dS )z6check whether or not the lambda is suspicious
        Nc             S   s   h | ]
}|j ’qS r   )rQ   )r>   Úkeywordr   r   r   ú	<setcomp>Â  s    z,BasicChecker.visit_lambda.<locals>.<setcomp>zunnecessary-lambda)rð   rR   )rñ   ÚdefaultsrŒ  rH   rJ   rå   r!  r�   r£   r   Z	from_callrC   r‘  Úkwargr’  Úkwargsr�  ZstarargsÚkeywordsrˆ   Zkeyword_argumentsrA   r�   Úzipr€   rQ   rã   r  )	rÜ   rR   ÚcallZ	call_siteZordinary_argsZnew_call_argsZlambda_kwargsrÿ   Z
passed_argr   r   r   Úvisit_lambda˜  sB    

zBasicChecker.visit_lambdac             C   s,   | j |jƒ rdnd  d7  < | j|ƒ dS )zdcheck function name, docstring, arguments, redefinition,
        variable names, max locals
        r   r   rt   N)rœ   r~   Ú_check_dangerous_default)rÜ   rR   r   r   r   r   Õ  s    zBasicChecker.visit_functiondefc             C   sü   dd„ }xî|j jD ]â}yt|jƒ ƒ}W n tjk
r>   wY nX t|tjƒr|jƒ t	kr||krnt	|jƒ  }ntt|tjƒs‚||ƒrÊ||ƒr”|j
ƒ }qât|tjƒr´d|j|jƒ f }qâd|jƒ |jƒ f }nd|jƒ t	|jƒ  f }| jd||fd� qW d S )Nc             S   s   t | tjtjtjfƒS )N)rH   rJ   ræ   rç   rè   )Únr   r   r   Ú<lambda>á  s    z7BasicChecker._check_dangerous_default.<locals>.<lambda>z	%s() (%s)z%s (%s)zdangerous-default-value)rR   rñ   )rñ   r•  r1  r"  rJ   r$  rH   r5  r„   ÚDEFAULT_ARGUMENT_SYMBOLSZpytyperå   rQ   r�  rã   )rÜ   rR   Zis_iterabler0  rê   Úmsgr   r   r   rœ  ß  s(    
z%BasicChecker._check_dangerous_defaultc             C   s    | j |ƒ | j|dtjfƒ dS )z¼1 - check is the node has a right sibling (if so, that's some
        unreachable code)
        2 - check is the node is inside the finally clause of a try...finally
        block
        ÚreturnN)Ú_check_unreachableÚ_check_not_in_finallyrJ   ri   )rÜ   rR   r   r   r   r    s    
zBasicChecker.visit_returnc             C   s   | j |ƒ dS )z\check is the node has a right sibling (if so, that's some unreachable
        code)
        N)r¢  )rÜ   rR   r   r   r   r    s    zBasicChecker.visit_continuec             C   s$   | j |ƒ | j|dtjtjfƒ dS )z¼1 - check is the node has a right sibling (if so, that's some
        unreachable code)
        2 - check is the node is inside the finally clause of a try...finally
        block
        r  N)r¢  r£  rJ   rV   r`   )rÜ   rR   r   r   r   r    s    
zBasicChecker.visit_breakc             C   s   | j |ƒ dS )z\check if the node has a right sibling (if so, that's some unreachable
        code)
        N)r¢  )rÜ   rR   r   r   r   Úvisit_raise#  s    zBasicChecker.visit_raisec             C   s   | j d|d� dS )z'just print a warning on exec statementsz	exec-used)rR   N)rã   )rÜ   rR   r   r   r   Ú
visit_exec*  s    zBasicChecker.visit_execc             C   s†   t |jtjƒsd S |jjdkr"d S tj|jjƒ}|tjkr>d S |s‚|jj}t |tj	ƒsZd S t |jtj
ƒr‚|jjdkr‚| jd|d� d S )NÚformatÚprintzmisplaced-format-function)rR   )rH   r!  rJ   r�   r‚   r   rƒ   r£   r„  rå   r€   rQ   rã   )rÜ   Z	call_noder£   Z	call_exprr   r   r   Ú _check_misplaced_format_function/  s    
z-BasicChecker._check_misplaced_format_functionc             C   s~   | j |ƒ t|jtjƒrz|jj}||jƒ kp6||jƒ ksz|dkrP| jd|d� n*|dkrd| j	|ƒ n|dkrz| jd|d� dS )zovisit a Call node -> check if this is not a blacklisted builtin
        call and check for * or ** use
        Úexecz	exec-used)rR   ÚreversedÚevalz	eval-usedN)
r¨  rH   r!  rJ   r€   rQ   r  Úrootrã   Ú_check_reversed)rÜ   rR   rQ   r   r   r   r%  D  s    
zBasicChecker.visit_callc             C   s:   |j dkr6t|jtjƒr6t|jjƒdkr6| jd|d� dS )z0check the use of an assert statement on a tuple.Nr  zassert-on-tuple)rR   )ZfailrH   rx  rJ   rÞ   rˆ   Úeltsrã   )rÜ   rR   r   r   r   Úvisit_assertX  s    
zBasicChecker.visit_assertc             C   sR   t ƒ }xF|jD ]<\}}t|tjƒr|j}||kr@| jd||d� |j|ƒ qW dS )z!check duplicate key in dictionaryzduplicate-key)rR   rñ   N)rA   ÚitemsrH   rJ   rƒ  rê   rã   Úadd)rÜ   rR   ÚkeysÚkr�   Úkeyr   r   r   Ú
visit_dictb  s    zBasicChecker.visit_dictc             C   s   | j j|ƒ dS )zupdate try...finally flagN)rr  r™   )rÜ   rR   r   r   r   Úvisit_tryfinallym  s    zBasicChecker.visit_tryfinallyc             C   s   | j jƒ  dS )zupdate try...finally flagN)rr  Úpop)rÜ   rR   r   r   r   Úleave_tryfinallyq  s    zBasicChecker.leave_tryfinallyc             C   s"   |j ƒ }|dk	r| jd|d� dS )zcheck unreachable codeNr=  )rR   )Znext_siblingrã   )rÜ   rR   Zunreach_stmtr   r   r   r¢  u  s    zBasicChecker._check_unreachablec             C   s`   | j s
dS |j}|}xF|rZt||ƒ rZt|dƒrN||jkrN| jd||d� dS |}|j}qW dS )zÖcheck that a node is not inside a finally clause of a
        try...finally statement.
        If we found before a try...finally bloc a parent which its type is
        in breaker_classes, we skip the whole check.Nr,  zlost-exception)rR   rñ   )rr  rI   rH   Úhasattrr,  rã   )rÜ   rR   r  Zbreaker_classesÚ_parentrd   r   r   r   r£  {  s    z"BasicChecker._check_not_in_finallyc          %   C   sÒ  yt jt j|dd�ƒ}W n t jk
r0   Y �nžX |tjkr@dS |dkr¶t|jd tjƒr²yt	|jd j
jƒ ƒ}W n tjk
rˆ   dS X t|ddƒdkr²t j|ƒr²| jd|d� dS t|tjtjfƒrÌdS t|tjƒ�rX|jjdkoðt j|jƒ�r| jd|d� dS td	d
„ |jjƒ D ƒƒ�rXy|jt  W n$ tk
�rR   | jd|d� Y nX dS t|dƒ�rÀxhtD ]B}x:|D ]0}y|j|ƒ W n tjk
�r    P Y nX �qtW P �qjW | jd|d� n| jd|d� dS )z5 check that the argument to `reversed` is a sequence r   )ÚpositionNrQ   Úiterzbad-reversed-sequence)rR   rB   c             s   s"   | ]}|j d kotj|ƒV  qdS )rB   N)rQ   r   Úis_builtin_object)r>   r*  r   r   r   rh   ®  s   z/BasicChecker._check_reversed.<locals>.<genexpr>ra   )r   rƒ   Zget_argument_from_callZNoSuchArgumentErrorrJ   r„  rH   rñ   rå   r1  r!  r"  r$  ra   r½  rã   ræ   rÞ   r5  Z_proxiedrQ   r0   r'  r  ÚREVERSED_PROTOCOL_METHODr–   r¹  ÚREVERSED_METHODSZNotFoundError)rÜ   rR   r   r!  ÚmethodsÚmethr   r   r   r­  �  sT    



zBasicChecker._check_reversedc             C   sj   |j }|rfxZt||dd … ƒD ]D\}}t|d tjƒr|d d krt|d tjƒ r| jd|d� qW d S )Nrt   r   zconfusing-with-statement)rR   )r°  r™  rH   rJ   Ú
AssignNamerå   rã   )rÜ   rR   ÚpairsZ	prev_pairÚpairr   r   r   Ú
visit_withÈ  s    zBasicChecker.visit_withc       	      C   s  |j ƒ }|j}g }|j}t|d tjƒrBt|ƒdkr8d S |d j}t|jtj	ƒrjt|ƒdkr`d S |jg}n:t|jtjƒr¤t|jjƒ}t|ƒ|ks˜|dkrœd S |jj}xjt
||ƒD ]\\}}t|tj	ƒsÆq°t|tjƒsÔq°t|tjƒrì|j|krìq°|j|jkr°| jd|jf|d� q°W d S )Nr   rt   zself-assigning-variable)rñ   rR   )r  r  râ   rH   rJ   rÞ   rˆ   r®  rê   r€   r™  rÂ  rj   rQ   rã   )	rÜ   rR   r  Zscope_localsZ	rhs_namesrâ   Z	rhs_countÚtargetZlhs_namer   r   r   Ú_check_self_assigning_variableØ  s6    

z+BasicChecker._check_self_assigning_variablec             C   sª   x¤|D ]œ}t |tjƒsqg }xJ|jD ]@}t |tjƒrB| j|gƒ q$t |tjƒr$|jdkr$|j|jƒ q$W tj	|ƒ}x.|j
ƒ D ]"\}}|dkr|| jd|f|d� q|W qW d S )Nr�   rt   zredeclared-assigned-name)rñ   rR   )rH   rJ   rÞ   r®  Ú_check_redeclared_assign_namerÂ  rQ   r™   rõ   ÚCounterÚmost_commonrã   )rÜ   râ   rÆ  Zfound_namesÚelementrP   rQ   Úcountr   r   r   rÈ  þ  s    

z*BasicChecker._check_redeclared_assign_namec             C   s   | j |ƒ | j|jƒ d S )N)rÇ  rÈ  râ   )rÜ   rR   r   r   r   rä     s    
zBasicChecker.visit_assignc             C   s   | j |jgƒ d S )N)rÈ  rÆ  )rÜ   rR   r   r   r   r    s    zBasicChecker.visit_forN)r<  r=  r>  )r?  r@  rA  )rB  rC  rD  )rE  rF  rG  )rH  rI  rJ  )rK  rL  rM  )rN  rO  rP  )rQ  rR  rS  )rT  rU  rV  )rW  rX  rY  )rZ  r[  r\  )r]  r^  r_  )r`  ra  rb  )rc  rd  re  )rf  rg  rh  )ri  rj  rk  )rl  rm  rn  )ro  rp  rq  )0r!   r"   r#   r&   r   r§   r¨   rQ   r8  r¢   Zreportsrí   rv  r   r9  ry  rz  r{  rw  r‡  rÝ   rŽ  Ústaticmethodr‘  r’  r›  r   r:  rœ  r  r  r  r¤  r¥  r¨  r%  r¯  rµ  r¶  r¸  r¢  r£  r­  rÅ  rÇ  rÈ  rä   r  r   r   r   r   r;  g  s¼   	                                    24=&
;&r;  r   r   r   r   r   r   r   r   r   r   ZconstantÚ	attributezclass attributezinline iteration)
r   r   r   r   r   r   r   r   r   r   r-   r/   r.   r0   c           	   C   sŽ   g } x€t tƒD ]t}t| }t| }|jddƒ}| jd|f |dttjƒ ƒdd|f dœfƒ | jd|f d d	d
d||f dœfƒ qW t	| ƒS )Nr�   ú-z%s-naming-styleÚchoicez<style>z'Naming style matching correct %s names.)r0  rŒ   ÚchoicesÚmetavarÚhelpz%s-rgxÚregexpz<regexp>zHRegular expression matching correct %s names. Overrides %s-naming-style.)r0  rŒ   rÒ  rÓ  )
ÚsortedÚKNOWN_NAME_TYPESÚHUMAN_READABLE_TYPESÚDEFAULT_NAMING_STYLESÚreplacer™   rC   ÚNAMING_STYLESr²  r\   )Zname_optionsr   Zhuman_readable_nameZdefault_styler   r   r   Ú_create_naming_optionsD  s*    
rÛ  c            
   @   sF  e Zd ZdMdNdOd
œZddPddddœfddQddddœfdf ddd dœfd!d"d#d$d%dœfd&dRdd(d)dœffeƒ  ZdSd,d-hiZd.d/„ Zd0d1„ Zd2d3„ Z	e
jddƒd4d5„ ƒZd6d7„ Ze
jdddƒd8d9„ ƒZe
jdddƒd:d;„ ƒZeZe
jddƒd<d=„ ƒZe
jdddƒd>d?„ ƒZd@dA„ ZdBdC„ ZdDdE„ ZejfdFdG„ZdHdI„ ZedJdK„ ƒZdLS )TÚNameCheckerúBlack listed name "%s"úblacklisted-nameúDUsed when the name is listed in the black list (unauthorized names).ú"%s name "%s" doesn't conform to %súinvalid-nameúiUsed when the name doesn't conform to naming rules associated to its type (constant, variable, class...).ú*Name %s will become a keyword in Python %súassign-to-new-keywordúaUsed when assignment will become invalid in future Python release due to introducing new keyword.)ZC0102ZC0103ZW0111z
good-namesÚiÚjr³  ÚexÚRunr�   Úcsvz<names>zJGood variable names which should always be accepted, separated by a comma.)r0  rŒ   rÒ  rÓ  z	bad-namesÚfooÚbarÚbazÚtotoÚtutuÚtatazHBad variable names which should always be refused, separated by a comma.z
name-groupz<name1:name2>zrColon-delimited sets of names that determine each other's naming style when the name regexes allow several styles.zinclude-naming-hintFZynz<y_or_n>z?Include a hint for the correct naming format with invalid-name.zproperty-classesúabc.abstractpropertyz<decorator names>zÙList of decorators that produce properties, such as abc.abstractproperty. Add to this list to register other decorators that produce valid properties. These decorators are taken in consideration only for invalid-name.r¼   é   ÚasyncÚawaitc             C   s.   t j| |ƒ i | _i | _i | _i | _i | _d S )N)r¥   rí   Z_name_categoryÚ_name_groupÚ
_bad_namesÚ_name_regexpsÚ_name_hints)rÜ   rs  r   r   r   rí   ·  s    zNameChecker.__init__c             C   st   | j jddddddddddd�
| _x4| jjD ](}x"|jdƒD ]}d|f | j|< q<W q,W | jƒ \}}|| _|| _	d S )Nr   )
Zbadname_moduleZbadname_classZbadname_functionZbadname_methodZbadname_attrZbadname_constZbadname_variableZbadname_inlinevarZbadname_argumentZbadname_class_attributeú:zgroup_%s)
rs  ru  rœ   r{   Ú
name_groupÚsplitrõ  Ú_create_naming_rulesr÷  rø  )rÜ   Úgroupr   ÚregexpsÚhintsr   r   r   rv  ¿  s"    
zNameChecker.openc             C   s’   i }i }x€t D ]x}d|f }t| j|ƒ}t| j|ƒ||< d|f }t| j|d ƒ}|d k	rb|||< |d k	rzd|j ||< qd| ||< qW ||fS )Nz%s_naming_stylez%s_rgxz
%r patternz%s naming style)rÖ  ra   r{   rÚ  r    Úpattern)rÜ   rþ  rÿ  r   Znaming_style_option_nameZnaming_style_nameZcustom_regex_setting_nameZcustom_regexr   r   r   rü  Ô  s    


z NameChecker._create_naming_rulesc             C   s$   | j d|jjdƒd |ƒ i | _d S )Nr   r;   rt   ru   )Ú_check_namerQ   rû  rö  )rÜ   rR   r   r   r   r‡  ê  s    zNameChecker.visit_modulec       	      C   sÄ   x¾| j jƒ D ]°}t|ƒdk rqtjtƒ}tj}x0|jƒ D ]$}|t|ƒ j|ƒ t	t|ƒ|ƒ}q8W t|| ƒdkr˜t
|| dd„ d�}tj|dd … Ž }n|| d }x|D ]}| j|Ž  qªW qW d S )Nr  rt   c             S   s   t dd„ | D ƒƒS )Nc             s   s   | ]}|d  j V  qdS )r   N)rý   )r>   Úwarningr   r   r   rh   û  s    z=NameChecker.leave_module.<locals>.<lambda>.<locals>.<genexpr>)Úmin)rý  r   r   r   rž  û  s    z*NameChecker.leave_module.<locals>.<lambda>)r´  r   )rö  rü   rˆ   rõ   rö   rC   ÚsysÚmaxsizer™   r  rÕ  rù   rú   Ú_raise_name_warning)	rÜ   rR   Z
all_groupsÚgroupsZmin_warningsrý  Zby_lineÚwarningsrñ   r   r   r   Úleave_moduleï  s     

zNameChecker.leave_modulec             C   sZ   | j |j|ƒ | jd|j|ƒ x6|jjƒ D ](\}}t|j|ƒƒs*| jd||d ƒ q*W d S )Nr   r   r   )Ú&_check_assign_to_new_keyword_violationrQ   r  Zinstance_attrsr°  r0   Zinstance_attr_ancestors)rÜ   rR   r   Zanodesr   r   r   rÝ     s
    zNameChecker.visit_classdefc             C   sŒ   | j |j|ƒ tj}|jƒ rPtj|jjƒ |jƒr4d S tj	|jjƒ ƒrJtj
ntj}| jt|| jd�|j||ƒ |jj}|d k	rˆ| j||ƒ d S )N)r{   )r
  rQ   r   ÚHIGHr~   r   Zoverrides_a_methodrI   r  Úhas_known_basesÚ	INFERENCEro   r  r‡   r{   rñ   Ú_recursive_check_names)rÜ   rR   rr   rñ   r   r   r   r     s    zNameChecker.visit_functiondefc             C   s"   x|j D ]}| jd||ƒ qW d S )Nr   )rP   r  )rÜ   rR   rQ   r   r   r   Úvisit_global'  s    zNameChecker.visit_globalc             C   s.  | j |j|ƒ |jƒ }|jƒ }t|tjƒr<| jd|j|ƒ nît|tjƒr¼t|tj	ƒržt
|ƒ ržttj|jƒtjƒr„| jd|j|ƒ qºt|ƒsº| jd|j|ƒ nt|tjƒrº| jd|j|ƒ nnt|tjƒrú|j|krø|j|jƒ krøt|ƒsø| jd|j|ƒ n0t|tjƒ�r*t|j|jƒƒ�s*| jd|j|ƒ dS )z!check module level assigned namesr   r   r   r   r   N)r
  rQ   r  Úassign_typerH   rJ   ZComprehensionr  r|  ré   r[   r   rƒ   rê   rj   rU   rK   ri   ÚargnamesrC   Zlocal_attr_ancestors)rÜ   rR   r  r  r   r   r   Úvisit_assignname,  s(    zNameChecker.visit_assignnamec             C   s>   x8|D ]0}t |tjƒr(| jd|j|ƒ q| j|j|ƒ qW dS )z.check names in a possibly recursive list <arg>r   N)rH   rJ   rÂ  r  rQ   r  r®  )rÜ   rñ   rR   rÿ   r   r   r   r  H  s    
z"NameChecker._recursive_check_namesc             C   s   | j j||ƒS )N)rõ  r˜   )rÜ   rq   r   r   r   Ú_find_name_groupP  s    zNameChecker._find_name_groupc             C   sh   t | }| j| }| jjr.|d| j| j 7 }|jƒ ||f}| jd|||d� | jd|   d7  < d S )Nz (%r pattern)zinvalid-name)rR   rñ   rr   r‹   rt   )	r×  rø  r{   Zinclude_naming_hintr÷  r   Ú
capitalizerã   rœ   )rÜ   rR   rq   rQ   rr   Z
type_labelZhintrñ   r   r   r   r  S  s    
zNameChecker._raise_name_warningc                sð   ‡ fdd„}t j|ƒr,t j|ƒ\}}|r,dS || jjkr<dS || jjkrr| jdˆ    d7  < | jd||d� dS | jˆ  }|j	|ƒ}	t
|	ˆ |ƒrÊ| jˆ ƒ}
| jj|
i ƒ}|j|	jg ƒ}|j|ˆ ||fƒ |	dkrì||ƒ rì| j|ˆ ||ƒ dS )z(check for a name using the type's regexpc                s&   ˆ dkr"t j| ƒ}t|tjƒr"dS dS )Nr   TF)r   rƒ   rH   rJ   rj   )rR   r†   )rq   r   r   Ú _should_exempt_from_invalid_name`  s
    
zANameChecker._check_name.<locals>._should_exempt_from_invalid_nameNr‹   rt   zblacklisted-name)rR   rñ   )r   Zis_inside_exceptZclobber_in_exceptr{   Z
good_namesZ	bad_namesrœ   rã   r÷  rp   rs   r  rö  Ú
setdefaultrm   r™   r  )rÜ   rq   rQ   rR   rr   r  Z
clobberingr�   rÔ  rp   rú  Zbad_name_groupr  r   )rq   r   r  ]  s(    



zNameChecker._check_namec             C   s2   | j || jƒ}|d k	r.| jd|||ftjd� d S )Nzassign-to-new-keyword)rR   rñ   rr   )Ú_name_became_keyword_in_versionÚKEYWORD_ONSETrã   r   r  )rÜ   rQ   rR   Zkeyword_first_versionr   r   r   r
  }  s    
z2NameChecker._check_assign_to_new_keyword_violationc             C   s<   x6|j ƒ D ]*\}}| |kr
tj|k r
djtt|ƒƒS q
W d S )Nr;   )r°  r  Úversion_infor<   Úmapr—   )rQ   ÚrulesÚversionr˜  r   r   r   r  ‰  s    z+NameChecker._name_became_keyword_in_versionN)rÝ  rÞ  rß  )rà  rá  râ  )rã  rä  rå  )ræ  rç  r³  rè  ré  r�   )rë  rì  rí  rî  rï  rð  )rñ  )r¼   rò  )r!   r"   r#   r8  rÛ  Úoptionsr  rí   rv  rü  r   r9  r‡  r	  rÝ   r   r:  r  r  r  r  r  r   r  r  r
  rÍ  r  r   r   r   r   rÜ  f  sh         

 rÜ  c            	   @   sÎ   e Zd Zddddd)gifddd	dd*gifddddd+gifddddd,gifdœZdeddddœfdd-ddddœffZdd„ Zejddƒdd „ ƒZ	ejddƒd!d"„ ƒZ
ejddƒd#d$„ ƒZeZd%ejfd&d'„Zd(S ).ÚDocStringCheckerzEmpty %s docstringzempty-docstringz^Used when a module, function, class or method has an empty docstring (it would be too easy ;).Ú	old_namesÚW0132úold-empty-docstringzMissing module docstringzmissing-module-docstringzMUsed when a module has no docstring.Empty modules do not require a docstring.ÚC0111úmissing-docstringzMissing class docstringzmissing-class-docstringzMUsed when a class has no docstring.Even an empty class must have a docstring.z$Missing function or method docstringzmissing-function-docstringznUsed when a function or method has no docstring.Some special methods like __init__ do not require a docstring.)ZC0112ZC0114ZC0115ZC0116zno-docstring-rgxrÔ  z<regexp>zcRegular expression which should only match function or class names that do not require a docstring.)r0  rŒ   rÒ  rÓ  zdocstring-min-lengthrt   Úintz<int>z[Minimum line length for functions/classes that require docstrings, shorter ones are exempt.c             C   s   | j jddddd�| _d S )Nr   )Zundocumented_moduleZundocumented_functionZundocumented_methodZundocumented_class)rs  ru  rœ   )rÜ   r   r   r   rv  Ë  s
    zDocStringChecker.openc             C   s   | j d|ƒ d S )Nr   )Ú_check_docstring)rÜ   rR   r   r   r   r‡  Ó  s    zDocStringChecker.visit_modulec             C   s$   | j jj|jƒd kr | jd|ƒ d S )Nr   )r{   Úno_docstring_rgxrp   rQ   r%  )rÜ   rR   r   r   r   rÝ   ×  s    zDocStringChecker.visit_classdefc             C   sâ   | j jj|jƒd krÞ|jƒ r dnd}t|ƒs4t|ƒr8d S t|jj	ƒ t
jƒrºd}tj|jj	ƒ ƒrdtjntj}x8|jj	ƒ jƒ D ]&}|j|krzt||j t
jƒrzd}P qzW | j||| |d� n$t|jj	ƒ t
jƒrÚ| j||ƒ nd S d S )Nr   r   FT)Úreport_missingrr   )r{   r&  rp   rQ   r~   r	   r   rH   rI   r  rJ   rj   r   r  r   r  ro   r'  ri   r%  r|  )rÜ   rR   ZftypeZ
overriddenrr   r*  r   r   r   r   Ü  s$    z"DocStringChecker.visit_functiondefTc       
      C   s\  |j }|dk�r$|sdS tj|ƒ|j }|dkr:| r:dS | jj}|dkr^|dkr^||k r^dS | jd|   d7  < |jrît|jd t	j
ƒrît|jd jt	jƒrîtj|jd jjƒ}t|t	jƒrît|jt	jƒrî|jjdkrÞdS |jjdkrîdS |dk� rþd	}	n|d
k�rd}	nd}	| j|	||d� n4|jƒ �sX| jd|   d7  < | jd||f|d� dS )z(check the node has a non empty docstringNr   rt   rŠ   r   r—   ÚunicodeÚbyteszmissing-module-docstringr   zmissing-class-docstringzmissing-function-docstring)rR   rr   zempty-docstring)rR   rñ   rr   ru   )r—   r(  r)  )Údocr   Zget_node_last_linenorý   r{   Zdocstring_min_lengthrœ   rŒ  rH   rJ   ÚExprrê   rå   rƒ   r!  r  Zboundr5  rQ   rã   Ústrip)
rÜ   rq   rR   r'  rr   Z	docstringr    Ú	max_linesr!  Úmessager   r   r   r%  û  s>    



z!DocStringChecker._check_docstringN)r   r!  )r"  r#  )r"  r#  )r"  r#  ru   )r!   r"   r#   r8  ÚNO_REQUIRED_DOC_RGXr  rv  r   r9  r‡  rÝ   r   r:  r   r  r%  r   r   r   r   r  ‘  s@   


r  c               @   s*   e Zd ZdZdd	iZejdƒdd„ ƒZdS )
ÚPassCheckerz/check if the pass statement is really necessaryZW0107úUnnecessary pass statementúunnecessary-passú@Used when a "pass" statement that can be avoided is encountered.c             C   sF   t |jj|ƒƒdks4t|jtjtjfƒrB|jjd k	rB| jd|d� d S )Nrt   zunnecessary-pass)rR   )	rˆ   rI   Zchild_sequencerH   rJ   rj   ri   r*  rã   )rÜ   rR   r   r   r   Ú
visit_pass6  s    zPassChecker.visit_passN)r1  r2  r3  )r!   r"   r#   r&   r8  r   r9  r4  r   r   r   r   r0  +  s     r0  c             C   s"   t | tjƒo t| jƒdko | j S )zSIs this a call with exactly 1 argument,
    where that argument is positional?
    rt   )rH   rJ   rå   rˆ   rñ   r˜  )rš  r   r   r   Ú_is_one_arg_pos_call?  s    r5  c               @   s†   e Zd ZdZd*d+dd	d
dd,gifd-d.d/dœZd0dd„Zdd„ Zdd„ Zdd „ Zd!d"„ Z	e
jddd	dddƒd#d$„ ƒZd%d&„ Zd'd(„ Zd)S )1ÚComparisonCheckera  Checks for comparisons

    - singleton comparison: 'expr == True', 'expr == False' and 'expr == None'
    - yoda condition: 'const "comp" right' where comp can be '==', '!=', '<',
      '<=', '>' or '>=', and right can be a variable, an attribute, a method or
      a function
    úComparison to %s should be %súsingleton-comparisonúQUsed when an expression is compared to singleton values like True, False or None.úComparison should be %súmisplaced-comparison-constantú˜Used when the constant is placed on the left side of a comparison. It is usually clearer in intent to place it in the right hand side of the comparison.z5Using type() instead of isinstance() for a typecheck.zunidiomatic-typecheckzÇThe idiomatic way to perform an explicit typecheck in Python is to use isinstance(x, Y) rather than type(x) == Y, type(x) is Y. Though there are unusual situations where these give different results.r  ÚW0154úold-unidiomatic-typecheckúComparison to literalúliteral-comparisonú¨Used when comparing an object to a literal, which is usually what you do not want to do, since you can compare to a different literal than what was expected altogether.úRedundant comparison - %súcomparison-with-itselfú/Used when something is compared against itself.ú;Comparing against a callable, did you omit the parenthesis?úcomparison-with-callableúºThis message is emitted when pylint detects that a comparison with a callable was made, which might suggest that some parenthesis were omitted, resulting in potential unwanted behaviour.)ZC0121ZC0122ZC0123ZR0123ZR0124ZW0143Fc             C   sŒ   |j dkr.|sd}nd}| jd|d|fd� nZ|j dkr\|sBd}nd}| jd|d|fd� n,|j d krˆ|spd	}nd
}| jd|d |fd� d S )NTzjust 'expr'zjust 'not expr'zsingleton-comparison)rR   rñ   Fz
'not expr'z'expr'z'expr is None'z'expr is not None')rê   rã   )rÜ   Z	singletonZ	root_nodeÚnegative_checkÚ
suggestionr   r   r   Ú_check_singleton_comparison{  s$    


z-ComparisonChecker._check_singleton_comparisonc             C   sv   t jt jt jt jf}t||ƒ}d}t|t jƒr\t|jtƒsD|jdkrHdS t|jt	t
ttfƒ}|sd|rr| jd|d� dS )zMCheck if we compare to a literal, which is usually what we do not want to do.FNzliteral-comparison)rR   )rJ   ræ   rÞ   rè   rç   rH   rƒ  rê   Úboolr)  r—   r$  Úfloatrã   )rÜ   ÚliteralrR   r
   Zis_other_literalZis_constr   r   r   Ú_check_literal_comparison•  s    
z+ComparisonChecker._check_literal_comparisonc             C   sF   t |tjƒrd S tj||ƒ}d|jƒ ||jf }| jd||fd� d S )Nz%s %s %rzmisplaced-comparison-constant)rR   rñ   )rH   rJ   rƒ  ÚREVERSED_COMPSr˜   r�  rê   rã   )rÜ   rR   ÚleftÚrightÚoperatorrI  r   r   r   Ú_check_misplaced_constant£  s
    z+ComparisonChecker._check_misplaced_constantc             C   s˜   |j }|jd d }|jd d }t|tjƒrHt|tjƒrH|j}|j}n$t|tjƒrlt|tjƒrl|j}|j}||kr”d|||f }| jd||fd� dS )zóCheck if identifier is compared against itself.
        :param node: Compare node
        :type node: astroid.node_classes.Compare
        :Example:
        val = 786
        if val == val:  # [comparison-with-itself]
            pass
        r   rt   z%s %s %szcomparison-with-itself)rR   rñ   N)	rP  ÚopsrH   rJ   rƒ  rê   r€   rQ   rã   )rÜ   rR   Úleft_operandÚright_operandrR  rI  r   r   r   Ú_check_logical_tautologyª  s    	

z*ComparisonChecker._check_logical_tautologyc                sl   |j d d }|tkrd S tjtjf‰ |j|j d d  }}t‡ fdd„||fD ƒƒdkrh| jd|d� d S )Nr   rt   c             3   s"   | ]}t tj|ƒˆ ƒrd V  qdS )rt   N)rH   r   rƒ   )r>   r  )Úbare_callablesr   r   rh   Ð  s   z?ComparisonChecker._check_callable_comparison.<locals>.<genexpr>zcomparison-with-callable)rR   )rT  ÚCOMPARISON_OPERATORSrJ   ri   r  rP  Úsumrã   )rÜ   rR   rR  rU  rV  r   )rX  r   Ú_check_callable_comparisonÅ  s    
z,ComparisonChecker._check_callable_comparisonc             C   sÞ   | j |ƒ | j|ƒ | j|ƒ t|jƒdkr0d S |j}|jd \}}|tkrht|tj	ƒrh| j
||||ƒ |dkr¢t|tj	ƒrŠ| j||ƒ nt|tj	ƒr¢| j||ƒ |dkrÆt|tj	ƒrÆ| j||dd� |d	krÚ| j||ƒ d S )
Nrt   r   z==z!=T)rH  r4   úis not)r4   r\  )r[  rW  Ú_check_unidiomatic_typecheckrˆ   rT  rP  rY  rH   rJ   rƒ  rS  rJ  rN  )rÜ   rR   rP  rR  rQ  r   r   r   Úvisit_compareØ  s&    	


zComparisonChecker.visit_comparec             C   s8   |j d \}}|tkr4|j}t|ƒr4| j||||ƒ d S )Nr   )rT  ÚTYPECHECK_COMPARISON_OPERATORSrP  r5  Ú_check_type_x_is_y)rÜ   rR   rR  rQ  rP  r   r   r   r]  ù  s
    z.ComparisonChecker._check_unidiomatic_typecheckc             C   sŒ   t j|jƒ}t|tjƒo"|jƒ tks(dS |dkrzt|ƒrzt j|jƒ}t|tjƒrz|jƒ tkrzt j|j	d ƒ}t|t
ƒszdS | jd|d� dS )z(Check for expressions like type(x) == Y.Nr4   úis notr   zunidiomatic-typecheck)rR   )r4   ra  )r   rƒ   r!  rH   rJ   rj   r„   Ú
TYPE_QNAMEr5  rñ   ÚLITERAL_NODE_TYPESrã   )rÜ   rR   rP  rR  rQ  Z	left_funcZ
right_funcZ	right_argr   r   r   r`   	  s    
z$ComparisonChecker._check_type_x_is_yN)r7  r8  r9  )r:  r;  r<  )r=  r>  )r?  r@  rA  )rB  rC  rD  )rE  rF  rG  )F)r!   r"   r#   r&   r8  rJ  rN  rS  rW  r[  r   r9  r^  r]  r`  r   r   r   r   r6  F  sD       
      
r6  c             C   sX   | j t| ƒƒ | j t| ƒƒ | j t| ƒƒ | j t| ƒƒ | j t| ƒƒ | j t| ƒƒ dS )z-required method to auto register this checkerN)Zregister_checkerr©   r;  rÜ  r  r0  r6  )rs  r   r   r   Úregister	  s    rd  )r2   r3   )r4   r5   r6   r7   r8   r9   )rA   rB   rC   )r6   r7   rF   rD   rG   rE   )r"   )N)Ur&   Úbuiltinsrõ   rù   r'   r  Útypingr   rJ   Zastroid.basesZastroid.scoped_nodesZastroid.argumentsr   Zpylint.utilsr   r7  Zpylintr   r   r   Zpylint.checkersZpylint.checkers.utilsr   r	   Zpylint.reporters.ureportsr
   rš   r   r%   r)   r*   r+   r,   rÚ  r(   r/  r¾  ZSEQUENCE_PROTOCOL_METHODSr¿  Ú	frozensetr_  rƒ  rè   ræ   rç   rc  ZUNITTEST_CASEr!   r=   rb  r(  rn   rB   r™  rŸ  rO  rY  r2  r6  rU   r[   r]   rc   rl   rs   rx   r}   r‡   r‰   r¢   r¤   ZBaseCheckerr¥   r©   r;  rÖ  r×  rØ  rÛ  rÜ  r  r0  r5  r6  rd  r   r   r   r   Ú<module>&   sÚ   


	
%
(   K     :"  -  Q