3
tûœ].ì  ã               @   sö   d 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mZ ddl	m
Z
mZ ddl	mZ ddlmZ dhZedƒZd
d„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ de
jƒZG dd„ de
jƒZG dd„ de
jƒZG dd„ de
jƒZdd„ ZdS )z'Looks for code which can be refactored.é    N)Úreduce)Ú
decorators)ÚcheckersÚ
interfaces)Úutilszitertools.countÚquitÚexitc             C   s"   x| j D ]}t||ƒrdS qW dS )NTF)ÚbodyÚ
isinstance)Zif_nodeÚreturning_node_classÚnode© r   ú=/tmp/pip-build-fn42xldr/pylint/pylint/checkers/refactoring.pyÚ!_if_statement_is_always_returning)   s    
r   c             C   s&   t | tjƒo$t | jtjƒo$| jjdkS )z!Checks if node is len(SOMETHING).Úlen)r
   ÚastroidÚCallÚfuncÚNameÚname)r   r   r   r   Ú_is_len_call0   s    r   c             C   s   t | tjƒo| jdkS )Nr   )r
   r   ÚConstÚvalue)r   r   r   r   Ú_is_constant_zero9   s    r   c             C   s   t | tjtjtjtjfƒS )zC Checks if node is an if, while, assert or if expression statement.)r
   r   ÚIfÚWhileÚAssertÚIfExp)r   r   r   r   Ú_node_is_test_condition=   s    r   c       
         s²   ˆˆ  }|j tjkrdS tjˆˆ d dƒ}ttj|fdd„|ƒƒ}tdd„ |D ƒƒ}| s`| rddS ‡ ‡fdd	„}|ƒ }d
dh}x,ˆ|ˆ … D ]}	d|	jks¦|	j|krŽdS qŽW dS )a  Check if the given token is a trailing comma

    :param tokens: Sequence of modules tokens
    :type tokens: list[tokenize.TokenInfo]
    :param int index: Index of token under check in tokens
    :returns: True if the token is a comma which trails an expression
    :rtype: bool
    Fé   Nc             S   s   | j d |j d kS )Nr   )Ústart)Úother_tokenÚ_tokenr   r   r   Ú<lambda>R   s    z$_is_trailing_comma.<locals>.<lambda>c             s   s    | ]}|j tjtjfkV  qd S )N)ÚtypeÚtokenizeÚNEWLINEÚCOMMENT)Ú.0r!   r   r   r   ú	<genexpr>Y   s   z%_is_trailing_comma.<locals>.<genexpr>c                 s@   x:t tˆdˆ … ƒƒD ]"\} }|jtjtjfkrˆ |  S qW dS )z4Get the index denoting the start of the current lineNr   )Ú	enumerateÚreversedr$   r%   r&   ÚNL)ZsubindexÚtoken)ÚindexÚtokensr   r   Úget_curline_index_start_   s    z3_is_trailing_comma.<locals>.get_curline_index_startÚreturnÚyieldú=T)	Ú
exact_typer%   ÚCOMMAÚ	itertoolsÚisliceÚlistÚ	takewhileÚallÚstring)
r/   r.   r-   Zleft_tokensZsame_line_remaining_tokensZis_last_elementr0   Zcurline_startZexpected_tokensZ	prevtokenr   )r.   r/   r   Ú_is_trailing_commaB   s(    	

r<   c               @   s  e Zd ZdZejejfZdZdÆdÇdÈdddddÉgifdddddÊgifdËdÌdÍdÎdÏdÐdÑdÒdÓdÔdÕdÖd×dØdÙdÚdÛdÜdÝdPœZ	dQdRdSdTdUdVœfdWdÞdYdZd[œffZ
d\Zdßd^d_„Zd`da„ Zdbdc„ Zejddde„ ƒZedfdg„ ƒZdhdi„ Zdjdk„ Zdldm„ Zdndo„ Zejdƒdpdq„ ƒZeZeZdrds„ Zejddƒdtdu„ ƒZejdƒdvdw„ ƒZejdƒdxdy„ ƒZ dzd{„ Z!d|d}„ Z"d~d„ Z#d€d�„ Z$d‚dƒ„ Z%d„d…„ Z&ejddddBdKdNd3ƒd†d‡„ ƒZ'ejd?ƒdˆd‰„ ƒZ(dŠd‹„ Z)ejdd$d'ƒdŒd�„ ƒZ*ejd!ƒdŽd�„ ƒZ+d�d‘„ Z,ed’d“„ ƒZ-d”d•„ Z.ejd!d9d<dHƒd–d—„ ƒZ/d˜d™„ Z0dšd›„ Z1dœd�„ Z2dždŸ„ Z3ed d¡„ ƒZ4d¢d£„ Z5d¤d¥„ Z6d¦d§„ Z7ejdd0d6ƒd¨d©„ ƒZ8edªd«„ ƒZ9d¬d­„ Z:ejd
dd*ƒd®d¯„ ƒZ;e;Z<d°d±„ Z=ejd-ƒd²d³„ ƒZ>ejdEƒd´dµ„ ƒZ?d¶d·„ Z@ed¸d¹„ ƒZAedºd»„ ƒZBd¼d½„ ZCd¾d¿„ ZDdÀdÁ„ ZEdÂdÃ„ ZFdÄdÅ„ ZGd]S )àÚRefactoringCheckerzßLooks for code which can be refactored

    This checker also mixes the astroid and the token approaches
    in order to create knowledge about whether an "else if" node
    is a true "else if" node, or an "elif" node.
    Úrefactoringú?Consider merging these isinstance calls to isinstance(%s, (%s))úconsider-merging-isinstanceúGUsed when multiple consecutive isinstance calls can be merged into one.úConsider using ternary (%s)úconsider-using-ternaryú=Used when one of known pre-python 2.5 ternary syntax is used.ú*Boolean expression may be simplified to %súsimplify-boolean-expressionú=Emitted when redundant pre-python 2.5 ternary syntax is used.zToo many nested blocks (%s/%s)ztoo-many-nested-blockszvUsed when a function or a method has too many nested blocks. This makes the code less understandable and maintainable.Z	old_namesÚR0101úold-too-many-nested-blocksz(The if statement can be replaced with %szsimplifiable-if-statementz=Used when an if statement can be replaced with 'bool(test)'. ÚR0102úold-simplifiable-if-statementú*Redefining argument with the local name %rúredefined-argument-from-localúóUsed when a local name is redefining an argument, which might suggest a potential error. This is taken in account only for a handful of name binding operations, such as for iteration, with statement assignment and exception handler assignment.úUnnecessary "%s" after "return"úno-else-returnúÜUsed in order to highlight an unnecessary block of code following an if containing a return statement. As such, it will warn when it encounters an else following a chain of ifs, all of them containing a return statement.úDisallow trailing comma tupleútrailing-comma-tupleá  In Python, a tuple is actually created by the comma symbol, not by the parentheses. Unfortunately, one can actually create a tuple by misplacing a trailing comma, which can lead to potential weird bugs in your code. You should always use parentheses explicitly for creating a tuple.úEDo not raise StopIteration in generator, use return statement insteadústop-iteration-returnúÇAccording to PEP479, the raise of StopIteration to end the loop of a generator may lead to hard to find bugs. This PEP specify that raise StopIteration has to be replaced by a simple return statementú_Either all return statements in a function should return an expression, or none of them should.úinconsistent-return-statementsúüAccording to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable)ú+Useless return at end of function or methodúuseless-returnú¿Emitted when a single "return" or "return None" statement is found at the end of function or method definition. This statement can safely be removed because Python will implicitly return Noneú5Consider using tuple unpacking for swapping variablesúconsider-swap-variablesúšYou do not have to use a temporary variable in order to swap variables. Using "tuple unpacking" to directly swap variables makes the intention more clear.úLConsider using str.join(sequence) for concatenating strings from an iterableúconsider-using-joinúnUsing str.join(sequence) is faster, uses less memory and increases readability compared to for-loop iteration.ú2Consider merging these comparisons with "in" to %rúconsider-using-inúáTo check if a variable is equal to one of many values,combine the values into a tuple and check if the variable is contained "in" it instead of checking for equality against each of the values.This is faster and less verbose.ú^Consider using dict.get for getting values from a dict if a key is present or a default if notúconsider-using-getú³Using the builtin dict.get for getting a value from a dictionary if a key is present or a default if not, is simpler and considered more idiomatic, although sometimes a bit slowerú0Simplify chained comparison between the operandsúchained-comparisonúˆThis message is emitted when pylint encounters boolean operation like"a < b and b < c", suggesting instead to refactor it to "a < b < c"ú)Consider using a dictionary comprehensionú!consider-using-dict-comprehensioná*  Emitted when we detect the creation of a dictionary using the dict() callable and a transient list. Although there is nothing syntactically wrong with this code, it is hard to read and can be simplified to a dict comprehension.Also it is faster since you don't need to create another transient listú"Consider using a set comprehensionú consider-using-set-comprehensionúÅAlthough there is nothing syntactically wrong with this code, it is hard to read and can be simplified to a set comprehension.Also it is faster since you don't need to create another transient listú)The if expression can be replaced with %súsimplifiable-if-expressionú>Used when an if expression can be replaced with 'bool(test)'. úUnnecessary "%s" after "raise"úno-else-raiseúÚUsed in order to highlight an unnecessary block of code following an if containing a raise statement. As such, it will warn when it encounters an else following a chain of ifs, all of them containing a raise statement.ú"Unnecessary use of a comprehensionúunnecessary-comprehensionúxInstead of using an identitiy comprehension, consider using the list, dict or set constructor. It is faster and simpler.úConsider using sys.exit()úconsider-using-sys-exitúAInstead of using exit() or quit(), consider using the sys.exit().úUnnecessary "%s" after "break"úno-else-breakúÚUsed in order to highlight an unnecessary block of code following an if containing a break statement. As such, it will warn when it encounters an else following a chain of ifs, all of them containing a break statement.ú!Unnecessary "%s" after "continue"úno-else-continueúàUsed in order to highlight an unnecessary block of code following an if containing a continue statement. As such, it will warn when it encounters an else following a chain of ifs, all of them containing a continue statement.)ZR1701ZR1706ZR1709ZR1702ZR1703ZR1704ZR1705ZR1707ZR1708ZR1710ZR1711ZR1712ZR1713ZR1714ZR1715ZR1716ZR1717ZR1718ZR1719ZR1720ZR1721ZR1722ZR1723ZR1724zmax-nested-blocksé   Úintz<int>z:Maximum number of nested blocks for function / method body)Údefaultr$   ÚmetavarÚhelpznever-returning-functionsúsys.exitÚcsvzâComplete name of functions that never returns. When checking for inconsistent-return-statements if a never returning function is called then it will be considered as an explicit return statement and no message will be printed.)r‡   r$   r‰   r   Nc             C   s&   t jj| |ƒ i | _| jƒ  d | _d S )N)r   ÚBaseTokenCheckerÚ__init__Ú_return_nodesÚ_initÚ_never_returning_functions)ÚselfÚlinterr   r   r   r�   F  s    zRefactoringChecker.__init__c             C   s   g | _ g | _d | _tƒ | _d S )N)Ú_nested_blocksÚ_elifsZ_nested_blocks_msgÚsetÚ_reported_swap_nodes)r‘   r   r   r   r�   L  s    zRefactoringChecker._initc             C   s   t | jjƒ| _d S )N)r•   ÚconfigZnever_returning_functionsr�   )r‘   r   r   r   ÚopenR  s    zRefactoringChecker.openc             C   s   t j| dd d�S )Nzdummy-variables-rgx)r‡   )Ú
lint_utilsZget_global_option)r‘   r   r   r   Ú
_dummy_rgxV  s    zRefactoringChecker._dummy_rgxc             C   s   t | jtjƒot | jjtƒS )N)r
   r   r   r   Úbool)r   r   r   r   Ú_is_bool_constZ  s    z!RefactoringChecker._is_bool_constc             C   s>   t |jtjƒr:|jj}|r:||gkr:|j|jf| jkr:dS dS )a	  Check if the given node is an actual elif

        This is a problem we're having with the builtin ast module,
        which splits `elif` branches into a separate if statement.
        Unfortunately we need to know the exact type in certain
        cases.
        TF)r
   Úparentr   r   ÚorelseÚlinenoÚ
col_offsetr”   )r‘   r   rž   r   r   r   Ú_is_actual_elif`  s    z"RefactoringChecker._is_actual_elifc       	      C   s.  | j |ƒrdS t|jƒdks*t|jƒdkr.dS |jd }|jd }t|tjƒrxt|tjƒs^dS | j|ƒ}| j|ƒ}d}n~t|tjƒròt|tjƒs”dS dd„ |j	D ƒ}dd„ |j	D ƒ}| sÀ| rÄdS t
|ƒt
|ƒkrØdS | j|ƒ}| j|ƒ}d}ndS | �s| �r
dS |jj�sdS | jd	||fd
� dS )au  Check if the given if node can be simplified.

        The if statement can be reduced to a boolean expression
        in some cases. For instance, if there are two branches
        and both of them return a boolean value that depends on
        the result of the statement's test, then this can be reduced
        to `bool(test)` without losing any functionality.
        Nr   r   z'return bool(test)'c             S   s   g | ]}t |tjƒr|j‘qS r   )r
   r   Ú
AssignNamer   )r(   Útargetr   r   r   ú
<listcomp>�  s   z=RefactoringChecker._check_simplifiable_if.<locals>.<listcomp>c             S   s   g | ]}t |tjƒr|j‘qS r   )r
   r   r¢   r   )r(   r£   r   r   r   r¤   ”  s   z'var = bool(test)'zsimplifiable-if-statement)r   Úargs)r¡   r   rž   r	   r
   r   ÚReturnrœ   ÚAssignÚtargetsÚsortedr   Úadd_message)	r‘   r   Zfirst_branchZelse_branchZfirst_branch_is_boolZelse_branch_is_boolÚ
reduced_toZfirst_branch_targetsZelse_branch_targetsr   r   r   Ú_check_simplifiable_ifp  s@    










z)RefactoringChecker._check_simplifiable_ifc             C   sz   xtt |ƒD ]h\}}|d }|dkrH| jj|| d ||d  d gƒ q
t||ƒr
| jjdƒr
| jd|jd d� q
W d S )Nr   Úelifé   ztrailing-comma-tupler   )Úline)r*   r”   Úextendr<   r’   Zis_message_enabledrª   r    )r‘   r/   r.   r-   Ztoken_stringr   r   r   Úprocess_tokens·  s    &
z!RefactoringChecker.process_tokensc             C   s   | j ƒ  d S )N)r�   )r‘   Ú_r   r   r   Úleave_moduleÇ  s    zRefactoringChecker.leave_modulec             C   s   | j |ƒ d S )N)Ú_check_nested_blocks)r‘   r   r   r   r   Úvisit_tryexceptÊ  s    z"RefactoringChecker.visit_tryexceptc             C   s~   | j r| j j|jƒrd S |js"d S |jƒ }t|tjƒs:d S x>|jj	tj
tjfd�D ]$}|j|jkrR| jd||jfd� qRW d S )N)Ú
skip_klasszredefined-argument-from-local)r   r¥   )rš   Úmatchr   rŸ   Úscoper
   r   ÚFunctionDefr¥   Únodes_of_classr¢   ÚLambdarª   )r‘   Ú	name_noder¸   Zdefined_argumentr   r   r   Ú$_check_redefined_argument_from_localÑ  s    z7RefactoringChecker._check_redefined_argument_from_localc             C   s0   | j |ƒ x |jjtjƒD ]}| j|ƒ qW d S )N)r´   r£   rº   r   r¢   r½   )r‘   r   r   r   r   r   Ú	visit_foræ  s    
zRefactoringChecker.visit_forc             C   s$   |j r t|j tjƒr | j|j ƒ d S )N)r   r
   r   r¢   r½   )r‘   r   r   r   r   Úvisit_excepthandlerí  s    z&RefactoringChecker.visit_excepthandlerc             C   s>   x8|j D ].\}}|sqx|jtjƒD ]}| j|ƒ q$W qW d S )N)Úitemsrº   r   r¢   r½   )r‘   r   r²   Únamesr   r   r   r   Ú
visit_withò  s
    zRefactoringChecker.visit_withc             C   sZ   |j s
d S | j|ƒrd S t||ƒrV|j d }|j|jf| jk}| j|||rNdndd� d S )Nr   r­   Úelse)r   r¥   )rž   r¡   r   rŸ   r    r”   rª   )r‘   r   Úmsg_idr   rž   Zfollowed_by_elifr   r   r   Ú_check_superfluous_elseú  s    


z*RefactoringChecker._check_superfluous_elsec             C   s   | j |dtjd�S )Nzno-else-return)rÄ   r   )rÅ   r   r¦   )r‘   r   r   r   r   Ú_check_superfluous_else_return
  s    z1RefactoringChecker._check_superfluous_else_returnc             C   s   | j |dtjd�S )Nzno-else-raise)rÄ   r   )rÅ   r   ÚRaise)r‘   r   r   r   r   Ú_check_superfluous_else_raise  s    z0RefactoringChecker._check_superfluous_else_raisec             C   s   | j |dtjd�S )Nzno-else-break)rÄ   r   )rÅ   r   ÚBreak)r‘   r   r   r   r   Ú_check_superfluous_else_break  s    z0RefactoringChecker._check_superfluous_else_breakc             C   s   | j |dtjd�S )Nzno-else-continue)rÄ   r   )rÅ   r   ÚContinue)r‘   r   r   r   r   Ú _check_superfluous_else_continue  s    z3RefactoringChecker._check_superfluous_else_continuec             C   sx  dd„ }t |jtjƒoât|jƒdkoât |jd tjƒoât |jd jtjƒoâ||jd jj|jj	d d ƒoât |jd jj
tjƒoâ||jd jj
j|jjƒoât|jd jƒdkoât |jd jd tjƒoât tj|jj	d d ƒtjƒ}|oî|j �r| jd|d� nr|�rtt|jƒdk�rtt |jd tjƒ�rt||jd jd |jd jd ƒ�rtt|jd jƒdk�rt| jd|d� d S )Nc                sb   x:t jt jgD ]*‰ t‡ fdd„| |gD ƒƒr| j|jkS qW tdd„ | |gD ƒƒr^| j|jkS dS )Nc             3   s   | ]}t |ˆ ƒV  qd S )N)r
   )r(   Ú_node)Ú_typer   r   r)   !  s    zZRefactoringChecker._check_consider_get.<locals>.type_and_name_are_equal.<locals>.<genexpr>c             s   s   | ]}t |tjƒV  qd S )N)r
   r   r   )r(   rÍ   r   r   r   r)   #  s    F)r   r   r¢   r:   r   r   )Znode_aZnode_br   )rÎ   r   Útype_and_name_are_equal  s    zGRefactoringChecker._check_consider_get.<locals>.type_and_name_are_equalr   r   zconsider-using-get)r   )r
   Útestr   ÚComparer   r	   r§   r   Ú	SubscriptÚopsÚsliceÚIndexÚleftr¨   r¢   r   Ú
safe_inferÚDictrž   rª   )r‘   r   rÏ   Zif_block_okr   r   r   Ú_check_consider_get  s(    	 "z&RefactoringChecker._check_consider_getc             C   sJ   | j |ƒ | j|ƒ | j|ƒ | j|ƒ | j|ƒ | j|ƒ | j|ƒ d S )N)r¬   r´   rÆ   rÈ   rÊ   rÌ   rÙ   )r‘   r   r   r   r   Úvisit_ifA  s    






zRefactoringChecker.visit_ifc             C   s   | j |ƒ d S )N)Ú_check_simplifiable_ifexp)r‘   r   r   r   r   Úvisit_ifexpS  s    zRefactoringChecker.visit_ifexpc             C   s´   t |jtjƒ s t |jtjƒ r$d S t |jjtƒ sDt |jjtƒ rHd S t |jtjƒr\d}nd}|jj|jjfd	kr€dj	|ƒ}n|jj|jjfd
kršd}nd S | j
d||fd� d S )NrÐ   z
bool(test)TFz'{}'z
'not test'zsimplifiable-if-expression)r   r¥   )TF)FT)r
   r	   r   r   rž   r   r›   rÐ   rÑ   Úformatrª   )r‘   r   Ztest_reduced_tor«   r   r   r   rÛ   W  s    z,RefactoringChecker._check_simplifiable_ifexpc             C   s6   | j | jƒ g | _| j|ƒ | j|ƒ g | j|j< d S )N)Ú%_emit_nested_blocks_message_if_neededr“   Ú_check_consistent_returnsÚ_check_return_at_the_endrŽ   r   )r‘   r   r   r   r   Úleave_functiondefp  s
    

z$RefactoringChecker.leave_functiondefc             C   s   | j |ƒ d S )N)Ú&_check_stop_iteration_inside_generator)r‘   r   r   r   r   Úvisit_raise~  s    zRefactoringChecker.visit_raisec             C   s|   |j ƒ }t|tjƒ s |jƒ  r$dS tj|tƒr4dS |js>dS tj	|jƒ}|dks\|tj
kr`dS | j|ƒrx| jd|d� dS )zHCheck if an exception of type StopIteration is raised inside a generatorNzstop-iteration-return)r   )Úframer
   r   r¹   Úis_generatorr   Únode_ignores_exceptionÚStopIterationÚexcr×   ÚUninferableÚ+_check_exception_inherit_from_stopiterationrª   )r‘   r   rä   rè   r   r   r   râ   ‚  s    
z9RefactoringChecker._check_stop_iteration_inside_generatorc                s&   dj tjƒ‰ t‡ fdd„| jƒ D ƒƒS )zHReturn True if the exception node in argument inherit from StopIterationz{}.StopIterationc             3   s   | ]}|j ƒ ˆ kV  qd S )N)Úqname)r(   Ú_class)Ústopiteration_qnamer   r   r)   •  s    zQRefactoringChecker._check_exception_inherit_from_stopiteration.<locals>.<genexpr>)rÝ   r   ZEXCEPTIONS_MODULEÚanyÚmro)rè   r   )rí   r   rê   ‘  s    z>RefactoringChecker._check_exception_inherit_from_stopiterationc             C   s~   t |jtjƒrz|jrzt |jd tjƒrz|jjdkr\t |jd jtjƒ r\d}| j	||d� n|jjdkrzd}| j	||d� d S )Nr   Údictz!consider-using-dict-comprehension)r   r•   z consider-using-set-comprehension)
r
   r   r   r   r¥   ÚListCompr   Úeltr   rª   )r‘   r   Zmessage_namer   r   r   Ú/_check_consider_using_comprehension_constructor—  s    zBRefactoringChecker._check_consider_using_comprehension_constructorc             C   s"   | j |ƒ | j|ƒ | j|ƒ d S )N)Ú3_check_raising_stopiteration_in_generator_next_callró   Ú_check_quit_exit_call)r‘   r   r   r   r   Ú
visit_call¦  s    

zRefactoringChecker.visit_callc             C   s^   t |jtjƒrZ|jjtkrZ|jƒ }|jjdƒ}|rLt |d tj	tj
fƒrLd S | jd|d� d S )Nr   r   zconsider-using-sys-exit)r   )r
   r   r   r   r   ÚBUILTIN_EXIT_FUNCSÚrootÚlocalsÚgetÚ
ImportFromÚImportrª   )r‘   r   r¸   Z	exit_funcr   r   r   rõ   ±  s    z(RefactoringChecker._check_quit_exit_callc             C   s–   dd„ }t |jtjƒrdS tj|jƒ}t|ddƒdkr’|jƒ }t|j	ƒdk}t |tj
ƒr’|jƒ r’| r’tj|tƒ r’||j	d ƒ r’| jd	|d
� dS )a  Check if a StopIteration exception is raised by the call to next function

        If the next value has a default value, then do not add message.

        :param node: Check to see if this Call node is a next function
        :type node: :class:`astroid.node_classes.Call`
        c             S   s   t j| ƒ}|r|jƒ tkS dS )NF)r   r×   rë   ÚKNOWN_INFINITE_ITERATORS)ÚparamÚinferredr   r   r   Ú_looks_like_infinite_iteratorÅ  s    
zmRefactoringChecker._check_raising_stopiteration_in_generator_next_call.<locals>._looks_like_infinite_iteratorNr   Ú Únextr   r   zstop-iteration-return)r   )r
   r   r   Ú	Attributer   r×   Úgetattrrä   r   r¥   r¹   rå   ræ   rç   rª   )r‘   r   r   rÿ   rä   Zhas_sentinel_valuer   r   r   rô   ¼  s    	zFRefactoringChecker._check_raising_stopiteration_in_generator_next_callc             C   s¶   t |jƒ tjƒsdS | jdd… }|j|jƒ kr:|g| _n\x(t| jƒD ]}||jkrVP | jjƒ  qFW t |tjƒrŠ| j	|ƒrŠ| jrŠ| jjƒ  | jj
|ƒ t|ƒt| jƒkr²| j|ƒ dS )z5Update and check the number of nested blocks
        N)r
   r¸   r   r¹   r“   r�   r+   Úpopr   r¡   Úappendr   rÞ   )r‘   r   Únested_blocksZancestor_noder   r   r   r´   Þ  s    


z'RefactoringChecker._check_nested_blocksc             C   s4   t |ƒ| jjkr0| jd|d t |ƒ| jjfd� d S )Nztoo-many-nested-blocksr   )r   r¥   )r   r—   Zmax_nested_blocksrª   )r‘   r  r   r   r   rÞ   ù  s
    z8RefactoringChecker._emit_nested_blocks_message_if_neededc                sâ   t ƒ ‰ tjt ƒ}xº| jD ]°}t|tjƒ st|jƒdkr:qt	j
|jƒ}| st	j|ƒ rZq|jdkrfq|jd jƒ }|jd }||kr�ˆ j|ƒ t|tjƒr°dd„ |jƒ D ƒ}n
|jƒ g}|| j|ƒ qW ‡ fdd„|jƒ D ƒS )	aH  Get the duplicated types from the underlying isinstance calls.

        :param astroid.BoolOp node: Node which should contain a bunch of isinstance calls.
        :returns: Dictionary of the comparison objects from the isinstance calls,
                  to duplicate values from consecutive calls.
        :rtype: dict
        r®   r
   r   r   c             S   s   g | ]}|j ƒ ‘qS r   )Ú	as_string)r(   Z
class_typer   r   r   r¤      s    zCRefactoringChecker._duplicated_isinstance_types.<locals>.<listcomp>c                s   i | ]\}}|ˆ kr||“qS r   r   )r(   Úkeyr   )Úduplicated_objectsr   r   ú
<dictcomp>'  s   zCRefactoringChecker._duplicated_isinstance_types.<locals>.<dictcomp>)r•   ÚcollectionsÚdefaultdictÚvaluesr
   r   r   r   r¥   r   r×   r   Úis_builtin_objectr   r  ÚaddÚTupleZiteredÚupdaterÀ   )r   Z	all_typesÚcallrÿ   Zisinstance_objectZisinstance_typesÚelemsr   )r
  r   Ú_duplicated_isinstance_types  s(    	





z/RefactoringChecker._duplicated_isinstance_typesc             C   s^   |j dkrdS | j|ƒ}x@|jƒ D ]4\}}tdd„ |D ƒƒ}| jd||dj|ƒfd� q"W dS )z4Check isinstance calls which can be merged together.ÚorNc             s   s   | ]
}|V  qd S )Nr   )r(   r   r   r   r   r)   2  s    zHRefactoringChecker._check_consider_merging_isinstance.<locals>.<genexpr>zconsider-merging-isinstancez, )r   r¥   )Úopr  rÀ   r©   rª   Újoin)r‘   r   Z
first_argsZduplicated_nameZclass_namesrÁ   r   r   r   Ú"_check_consider_merging_isinstance+  s    

z5RefactoringChecker._check_consider_merging_isinstancec             C   sª  dddœ}|j |ks"t|jƒdk r&d S xt|jD ]j}t|tjƒ sft|jƒdksf|jd d ||j  krjd S x,|j|jd d fD ]}t|tjƒr€d S q€W q.W g g  }}xd|jD ]Z}t	ƒ }xD|j|jd d fD ],}t|tj
ƒrì|j|jƒ ƒ |j|jƒ ƒ qÎW |j|ƒ q®W tdd„ |ƒ}|�s$d S tt|ƒƒd }	|j d	k�rDd
nd}
ttjj|ƒƒ}|j|	ƒ t|ƒdk�rzdj|ƒn
|d d }d|	|
|f }| jd||fd� d S )Nz==z!=)r  Úandr®   r   r   c             S   s
   | j |ƒS )N)Úintersection)ÚaÚbr   r   r   r#   U  s    z=RefactoringChecker._check_consider_using_in.<locals>.<lambda>r  Úinznot inz, ú,z
%s %s (%s)zconsider-using-in)r   r¥   )r  r   r  r
   r   rÑ   rÓ   rÖ   r   r•   r   r  r  r  r   r©   r8   r  ÚOrderedDictÚfromkeysÚremover  rª   )r‘   r   Zallowed_opsr   Z
comparableÚ	variablesr  Zvariable_setZcommon_variablesZcommon_variableÚcomprehensionZvalues_stringÚ
suggestionr   r   r   Ú_check_consider_using_in9  s:    


$z+RefactoringChecker._check_consider_using_inc       
      C   sÀ   |j dkst|jƒdk rdS dd„ }tjdd„ ƒ}x$|jD ]}t|tjƒr:|||ƒ q:W xb|jƒ D ]V\}}t|d j	|d	 ƒƒ}t|d ƒ}t|d	 ƒ}	||k rb||	k rb| j
d
|d� P qbW dS )a	  Check if there is any chained comparison in the expression.

        Add a refactoring message if a boolOp contains comparison like a < b and b < c,
        which can be chained as a < b < c.

        Care is taken to avoid simplifying a < b < c and b < d.
        r  r®   Nc             S   sæ   | j }xÚ| jD ]Ð\}}xÂ||fD ]¶}d }t|tjƒr<|j}nt|tjƒrN|j}|d krXq |dkr˜||kr||| d j| ƒ qÖ||krÖ|| d j| ƒ q |dkr ||kr¼|| d j| ƒ q ||kr || d j| ƒ q W |}qW d S )	Nú<ú<=Úlower_boundÚupper_boundú>ú>=)r'  r(  )r+  r,  )	rÖ   rÓ   r
   r   r   r   r   r   r  )Úcomparison_nodeÚusesZleft_operandÚoperatorZright_operandÚoperandr   r   r   r   Ú_find_lower_upper_boundso  s*    zNRefactoringChecker._check_chained_comparison.<locals>._find_lower_upper_boundsc               S   s   t ƒ t ƒ dœS )N)r)  r*  )r•   r   r   r   r   r#   ‰  s    z>RefactoringChecker._check_chained_comparison.<locals>.<lambda>r)  r*  zchained-comparison)r   )r  r   r  r  r  r
   r   rÑ   rÀ   r  rª   )
r‘   r   r1  r.  r-  r²   ZboundsZ
num_sharedZnum_lower_boundsZnum_upper_boundsr   r   r   Ú_check_chained_comparisond  s    
z,RefactoringChecker._check_chained_comparisonc             C   s"   | j |ƒ | j|ƒ | j|ƒ d S )N)r  r&  r2  )r‘   r   r   r   r   Úvisit_boolop—  s    

zRefactoringChecker.visit_boolopc             C   s>   t | tjƒo<t| jƒdko<t | jd tjjƒo<t | jtjjƒS )Nr   r   )	r
   r   r§   r   r¨   Znode_classesr¢   r   r   )r   r   r   r   Ú_is_simple_assignmentŸ  s    z(RefactoringChecker._is_simple_assignmentc                sÌ   |j ƒ  s|j ƒ j ƒ  rd S ||j ƒ |j ƒ j ƒ g}t‡ fdd„|D ƒƒsLd S t‡ fdd„|D ƒƒrfd S dd„ |D ƒ}dd„ |D ƒ}|d |d krÈ|dd … |d d… krÈˆ jj|ƒ d	}ˆ j||d
� d S )Nc             3   s   | ]}ˆ j |ƒV  qd S )N)r4  )r(   r   )r‘   r   r   r)   ¬  s    z;RefactoringChecker._check_swap_variables.<locals>.<genexpr>c             3   s   | ]}|ˆ j kV  qd S )N)r–   )r(   r   )r‘   r   r   r)   ®  s    c             S   s   g | ]}|j d  j‘qS )r   )r¨   r   )r(   r   r   r   r   r¤   °  s    z<RefactoringChecker._check_swap_variables.<locals>.<listcomp>c             S   s   g | ]}|j j‘qS r   )r   r   )r(   r   r   r   r   r¤   ±  s    r   r   zconsider-swap-variables)r   éÿÿÿÿr5  )Znext_siblingr:   rî   r–   r  rª   )r‘   r   ZassignmentsrÖ   ÚrightÚmessager   )r‘   r   Ú_check_swap_variables¨  s    (z(RefactoringChecker._check_swap_variablesc       	      C   sº   | j |ƒ | j|jƒr*| j|jƒ\}}}nd S tdd„ ||fD ƒƒrHd S tj|ƒ}|d tjfkrfd}n|j	ƒ }|dkr„d}|j
ƒ }n d}dj|j
ƒ |j
ƒ |j
ƒ d�}| j|||fd	� d S )
Nc             s   s   | ]}t |tjƒV  qd S )N)r
   r   rÑ   )r(   r   r   r   r   r)   Ä  s    z2RefactoringChecker.visit_assign.<locals>.<genexpr>TFzsimplify-boolean-expressionzconsider-using-ternaryz{truth} if {cond} else {false})ÚtruthÚcondÚfalse)r   r¥   )r8  Ú_is_and_or_ternaryr   Ú_and_or_ternary_argumentsr:   r   r×   r   ré   Z
bool_valuer  rÝ   rª   )	r‘   r   r:  Ztruth_valueÚfalse_valueZinferred_truth_valueZtruth_boolean_valuer7  r%  r   r   r   Úvisit_assign·  s(    


zRefactoringChecker.visit_assignc             C   sÐ   |j }t|tjƒ s"t|jƒdkr&dS |jƒ }t|tjƒs>dS dd„ |jD ƒ}|j	dko¸t|j
tjƒo¸t|jƒdko¸|j
j|ko¸t|jtjƒo¸t|jjtƒo¸t|jtjƒo¸|jj|j
jk}|rÌ| jd|d� dS )a  
        We start with the augmented assignment and work our way upwards.
        Names of variables for nodes if match successful:
        result = ''  # assign
        for number in ['1', '2', '3']  # for_loop
            result += number  # aug_assign
        r   Nc             S   s   h | ]}t |tjƒr|j’qS r   )r
   r   r¢   r   )r(   r£   r   r   r   ú	<setcomp>ê  s   z@RefactoringChecker._check_consider_using_join.<locals>.<setcomp>z+=zconsider-using-join)r   )r�   r
   r   ÚForr   r	   Zprevious_siblingr§   r¨   r  r£   r¢   r   r   r   Ústrr   rª   )r‘   Z
aug_assignZfor_loopZassignZresult_assign_namesZis_concat_loopr   r   r   Ú_check_consider_using_joinÜ  s$    

z-RefactoringChecker._check_consider_using_joinc             C   s   | j |ƒ d S )N)rC  )r‘   r   r   r   r   Úvisit_augassigný  s    z"RefactoringChecker.visit_augassignc             C   s   | j |ƒ d S )N)Ú _check_unnecessary_comprehension)r‘   r   r   r   r   Úvisit_comprehension  s    z&RefactoringChecker.visit_comprehensionc             C   sx  t |jtjƒs2t|jƒdks2t|jjƒdks2|jr6d S t |jtjƒr°t |jj	tj
ƒr°t |jjtj
ƒr°t |jtjƒr°tdd„ |jjD ƒƒr°|jj	j|jjjg}dd„ |jjD ƒ}nšt |jtjtjfƒ�rF|jj}t |tj
ƒrà|jnt |tjƒrüdd„ |jD ƒng }|jjd j}t |tjƒ�r"|jn t |tjƒ�r@dd„ |jD ƒng }nd S ||  k�o^g kn  �rt| jd	|d
� d S )Nr   r   c             s   s   | ]}t |tjƒV  qd S )N)r
   r   r¢   )r(   rò   r   r   r   r)     s    zFRefactoringChecker._check_unnecessary_comprehension.<locals>.<genexpr>c             S   s   g | ]
}|j ‘qS r   )r   )r(   rò   r   r   r   r¤     s    zGRefactoringChecker._check_unnecessary_comprehension.<locals>.<listcomp>c             S   s   g | ]}t |tjƒr|j‘qS r   )r
   r   r   r   )r(   rò   r   r   r   r¤     s    c             S   s   g | ]}t |tjƒr|j‘qS r   )r
   r   r¢   r   )r(   rò   r   r   r   r¤   )  s   zunnecessary-comprehension)r   )r
   r�   r   ÚGeneratorExpr   ZifsÚ
generatorsZis_asyncÚDictCompr	  r   r   r£   r  r:   Úeltsr   rñ   ÚSetComprò   r¢   rª   )r‘   r   Z	expr_listZtarget_listÚexprr£   r   r   r   rE    s0    z3RefactoringChecker._check_unnecessary_comprehensionc             C   sˆ   t | tjƒo†| jdko†t| jƒdko†t | jd tjƒo†t | jd tjƒ o†| jd jdko†t | jd jd tjƒ o†t| jd jƒdkS )z»
        Returns true if node is 'condition and true_value or false_value' form.

        All of: condition, true_value and false_value should not be a complex boolean expression
        r  r®   r   r   r  )r
   r   ÚBoolOpr  r   r  )r   r   r   r   r<  6  s    
z%RefactoringChecker._is_and_or_ternaryc             C   s$   | j d }| j d j \}}|||fS )Nr   r   )r  )r   r>  Ú	conditionZ
true_valuer   r   r   r=  H  s    
z,RefactoringChecker._and_or_ternary_argumentsc             C   s"   t |jtjtjd�ƒ| j|j< d S )N)r¶   )r8   rº   r   r¦   r¹   rŽ   r   )r‘   r   r   r   r   Úvisit_functiondefN  s    z$RefactoringChecker.visit_functiondefc             C   sV   dd„ | j |j D ƒ}|sdS t|ƒt| j |j ƒkrD| j|ƒrDdS | jd|d� dS )a{  Check that all return statements inside a function are consistent.

        Return statements are consistent if:
            - all returns are explicit and if there is no implicit return;
            - all returns are empty and if there is, possibly, an implicit return.

        Args:
            node (astroid.FunctionDef): the function holding the return statements.

        c             S   s   g | ]}|j d k	r|‘qS )N)r   )r(   rÍ   r   r   r   r¤   `  s    z@RefactoringChecker._check_consistent_returns.<locals>.<listcomp>Nzinconsistent-return-statements)r   )rŽ   r   r   Ú_is_node_return_endedrª   )r‘   r   Zexplicit_returnsr   r   r   rß   S  s    
z,RefactoringChecker._check_consistent_returnsc                s^  t |tjƒrdS t |tjƒrTy |jjƒ d }ˆ j|ƒr:dS W n tjk
rR   Y nX t |tjƒrddS t |tj	ƒ� rü|j
s|dS tj|ƒsŠdS tj|j
ƒ}|dks¨|tjkr¬dS |jƒ jdƒd }tj||ƒ}|dk	rÚt|ƒng }|røt‡ fdd„|D ƒƒS dS t |tjƒ�rDt‡ fd	d„|jD ƒƒ}t‡ fd
d„|jD ƒƒ}|�oB|S t‡ fdd„|jƒ D ƒƒS )zðCheck if the node ends with an explicit return statement.

        Args:
            node (astroid.NodeNG): node to be checked.

        Returns:
            bool: True if the node ends with an explicit statement, False otherwise.

        Tr   NFÚ.r   c             3   s   | ]}ˆ j |ƒV  qd S )N)rP  )r(   Z_handler)r‘   r   r   r)   ˜  s    z;RefactoringChecker._is_node_return_ended.<locals>.<genexpr>c             3   s$   | ]}t |tjƒsˆ j|ƒV  qd S )N)r
   r   r¹   rP  )r(   Z_ore)r‘   r   r   r)   ¡  s   c             3   s$   | ]}t |tjƒsˆ j|ƒV  qd S )N)r
   r   r¹   rP  )r(   Z_ifn)r‘   r   r   r)   ¦  s   c             3   s$   | ]}t |tjƒsˆ j|ƒV  qd S )N)r
   r   ÚExceptHandlerrP  )r(   Z_child)r‘   r   r   r)   ®  s   r5  )r
   r   r¦   r   r   rÿ   Ú _is_function_def_never_returningZInferenceErrorr   rÇ   rè   r   Zis_node_inside_try_exceptr×   ré   ZpytypeÚsplitZget_exception_handlersr8   rî   r   rž   r	   Zget_children)r‘   r   Zfuncdef_noderè   Zexc_nameÚhandlersZis_orelse_returningZis_if_returningr   )r‘   r   rP  j  sJ    





z(RefactoringChecker._is_node_return_endedc             C   s(   y|j ƒ | jkS  tk
r"   dS X dS )zýReturn True if the function never returns. False otherwise.

        Args:
            node (astroid.FunctionDef): function definition node to be analyzed.

        Returns:
            bool: True if the function never returns, False otherwise.
        FN)rë   r�   Ú	TypeError)r‘   r   r   r   r   rS  ³  s    	z3RefactoringChecker._is_function_def_never_returningc             C   s†   t | j|j ƒdkrdS t |jƒdkr*dS |jd }t|tjƒr‚|jdkrZ| jd|d� n(t|jtj	ƒr‚|jjdkr‚| jd|d� dS )a  Check for presence of a *single* return statement at the end of a
        function. "return" or "return None" are useless because None is the
        default return type if they are missing.

        NOTE: produces a message only if there is a single return statement
        in the function body. Otherwise _check_consistent_returns() is called!
        Per its implementation and PEP8 we can have a "return None" at the end
        of the function body if there are other return statements before that!
        r   Nzuseless-return)r   r5  )
r   rŽ   r   r	   r
   r   r¦   r   rª   r   )r‘   r   Úlastr   r   r   rà   Á  s    


z+RefactoringChecker._check_return_at_the_end)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   rr   )rs   rt   ru   )rv   rw   rx   )ry   rz   r{   )r|   r}   r~   )r   r€   r�   )r‚   rƒ   r„   )rŠ   )N)HÚ__name__Ú
__module__Ú__qualname__Ú__doc__r   ZITokenCheckerÚIAstroidCheckerÚ__implements__r   ÚmsgsÚoptionsÚpriorityr�   r�   r˜   r   Zcachedpropertyrš   Ústaticmethodrœ   r¡   r¬   r±   r³   r   Úcheck_messagesrµ   Zvisit_tryfinallyZvisit_whiler½   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&  r2  r3  r4  r8  r?  Zvisit_returnrC  rD  rF  rE  r<  r=  rO  rß   rP  rS  rà   r   r   r   r   r=   o   s<        

                                      	
G#"*+3	 !1Ir=   c               @   sR   e Zd ZejfZdZdddœZed	d
„ ƒZ	e
jdƒdd„ ƒZe
jdƒdd„ ƒZdS )ÚRecommandationCheckerr>   ú@Consider using enumerate instead of iterating with range and lenúconsider-using-enumerateú~Emitted when code that iterates with range and len is encountered. Such code can be simplified by using the enumerate builtin.úEConsider iterating the dictionary directly instead of calling .keys()úconsider-iterating-dictionaryú©Emitted when the keys of a dictionary are iterated through the .keys() method. It is enough to just iterate through the dictionary itself, as in "for key in dictionary".)ZC0200ZC0201c             C   s&   t j| ƒ}|sdS t j|ƒo$|j|kS )NF)r   r×   r  r   )r   Úfunctionrÿ   r   r   r   Ú_is_builtinî  s    
z!RecommandationChecker._is_builtinc             C   sŽ   t |jtjƒsd S |jjdkr"d S t |jtjtjfƒs:d S tj	|jƒ}t |tj
ƒ sdt |jtjƒ rhd S t |jtjtjfƒrŠ| jd|d� d S )NÚkeyszconsider-iterating-dictionary)r   )r
   r   r   r  Úattrnamer�   rA  ZComprehensionr   r×   ZBoundMethodZboundrØ   rª   )r‘   r   rÿ   r   r   r   rö   õ  s    z RecommandationChecker.visit_callc             C   s²  t |jtjƒsdS | j|jjdƒs&dS t|jjƒdkrLt|jjd ƒ rLdS t|jjƒdkr`dS t |jjd tjƒsxdS |jjd j}| j|dƒs–dS |jjd j}| s¶t|ƒdkrºdS |d }t |tj	ƒsÒdS |j
ƒ }|jdkoì|jdk� rôdS x¸|jD ]®}x¦|jtjƒD ]–}t |jtj	ƒ�s&�qt |jtjƒ�s:�qt |jjtj	ƒ�sP�q|jjj|jjk�rh�q|j|jjk�r|�q|jj
ƒ |j
ƒ k�r”�q| jd	|d
� dS W � qüW dS )z?Emit a convention whenever range and len are used for indexing.NÚranger®   r   r   r   r‘   Ú__iter__zconsider-using-enumerate)r   r5  r5  r5  )r
   Úiterr   r   rk  r   r   r¥   r   r   r¸   r   r	   rº   rÒ   r   rÔ   rÕ   r£   rª   )r‘   r   Zsecond_funcZlen_argsZiterating_objectr¸   ÚchildZ	subscriptr   r   r   r¾     sL    "zRecommandationChecker.visit_forN)rd  re  rf  )rg  rh  ri  )rX  rY  rZ  r   r\  r]  r   r^  ra  rk  r   rb  rö   r¾   r   r   r   r   rc  Ú  s       rc  c            	   @   sb   e Zd ZdZejfZddiZdZddd	d
dddddœZ	e
jfZdd„ dD ƒZejdƒdd„ ƒZdS )Ú
NotCheckerzžchecks for too many not in comparison expressions

    - "not not" should trigger a warning
    - "not" followed by a comparison should trigger a warning
    ZC0113úConsider changing "%s" to "%s"úunneeded-notú=Used when a boolean expression contains an unneeded negation.r>   z>=r+  z<=r'  z!=z==znot inzis not)r'  z<=r+  z>=z==z!=r  Úisc             C   s   g | ]}d t j|f ‘qS )z%s.%s)ÚbuiltinsrX  )r(   rë   r   r   r   r¤   b  s    zNotChecker.<listcomp>r•   Ú	frozensetc       	      C   s6  |j dkrd S |j}t|tjƒrJ|j dkrJ| jd||jƒ |jjƒ fd� nèt|tjƒ�r2|j}t	|j
ƒdkrpd S |j
d \}}|| jkrŒd S |jƒ }|jdkrª|dkrªd S xRtj|ƒtj|ƒfD ]:}|sÌd S t|| jƒrÜd S t|tjƒrÀ|jƒ | jkrÀd S qÀW d|jƒ | j| |jƒ f }| jd||jƒ |fd� d S )	NÚnotzunneeded-not)r   r¥   r   r   Ú__ne__z==z%s %s %s)r  r0  r
   r   ÚUnaryOprª   r  rÑ   rÖ   r   rÓ   Ú
reverse_oprä   r   r   Z	node_typeÚskipped_nodesZInstancerë   Úskipped_classnames)	r‘   r   r0  rÖ   r/  r6  rä   rÎ   r%  r   r   r   Úvisit_unaryope  s@    

zNotChecker.visit_unaryopN)rs  rt  ru  )r•   rx  )rX  rY  rZ  r[  r   r\  r]  r^  r   r|  r   ÚSetr}  r~  r   rb  r  r   r   r   r   rr  C  s$     rr  c               @   sP   e Zd ZdZejfZdZddiZdZ	f Z
ejdƒdd	„ ƒZejdƒd
d„ ƒZdS )Ú
LenCheckera  Checks for incorrect usage of len() inside conditions.
    Pep8 states:
    For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

        Yes: if not seq:
             if seq:

        No: if len(seq):
            if not len(seq):

    Problems detected:
    * if len(sequence):
    * if not len(sequence):
    * elif len(sequence):
    * elif not len(sequence):
    * while len(sequence):
    * while not len(sequence):
    * assert len(sequence):
    * assert not len(sequence):
    r>   ZC1801úQDo not use `len(SEQUENCE)` without comparison to determine if a sequence is emptyúlen-as-conditioná  Used when Pylint detects that len(sequence) is being used without explicit comparison inside a condition to determine if a sequence is empty. Instead of coercing the length to a boolean, either rely on the fact that empty sequences are false or compare the length against a scalar.r®   c             C   s^   t |ƒrZ|j}xt|tjƒr$|j}qW t|ƒs2d S ||jkpF|jj|ƒsLd S | jd|d� d S )Nzlen-as-condition)r   )	r   r�   r
   r   rM  r   rÐ   Z	parent_ofrª   )r‘   r   r�   r   r   r   rö   º  s    
zLenChecker.visit_callc             C   s2   t |tjƒr.|jdkr.t|jƒr.| jd|d� dS )z `not len(S)` must become `not S` regardless if the parent block
        is a test condition or something else (boolean expression)
        e.g. `if not len(S):`ry  zlen-as-condition)r   N)r
   r   r{  r  r   r0  rª   )r‘   r   r   r   r   r  Î  s    

zLenChecker.visit_unaryopN)r‚  rƒ  r„  éþÿÿÿ)rX  rY  rZ  r[  r   r\  r]  r   r^  r`  r_  r   rb  rö   r  r   r   r   r   r�  ‘  s     r�  c             C   s<   | j t| ƒƒ | j t| ƒƒ | j t| ƒƒ | j t| ƒƒ dS )z.Required method to auto register this checker.N)Zregister_checkerr=   rr  rc  r�  )r’   r   r   r   ÚregisterÛ  s    r†  )r   r   )r[  rw  r  r6   r%   Ú	functoolsr   r   r   Zpylintr   r   r   r™   Zpylint.checkersrý   rx  r÷   r   r   r   r   r<   rŒ   r=   ZBaseCheckerrc  rr  r�  r†  r   r   r   r   Ú<module>   s<   	-        siNJ