
'![c           @   s9  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	 d d l m
 Z d d l m
 Z
 d   Z d   Z d	 e	 j f d
     YZ d e	 j f d     YZ d e	 j f d     YZ d   Z d   Z d   Z d e	 j f d     YZ d   Z d   Z d S(   s'   Looks for code which can be refactored.iN(   t
   decorators(   t
   interfaces(   t   checkers(   t   utilsc         C   s   t  |   } | o t |  S(   N(   t   listt   all(   t   gent   values(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _all_elements_are_true!   s    c         C   sg   d   } |  j    } t | |  j d |  } |  j rW t | |  j d |  } n t } | of | S(   Nc         s   s[   xT |  D]L } t  | t j  r- t |  Vq t  | t j  r | j   | k Vq q Wd  S(   N(   t
   isinstancet   astroidt   Ift!   _if_statement_is_always_returningt   Returnt   scope(   t   elemsR   t   node(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _has_return_node'   s
    R   (   R   R   t   bodyt   orelset   False(   t   if_nodeR   R   t   body_returnst   orelse_returns(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR   &   s    		t   RefactoringCheckerc           B   s;  e  Z d  Z e j e j f Z d Z i
 da d 6db d	 6dc d 6d d d i dd g d 6f d 6d d d i de g d 6f d 6df d 6dg d  6d! d" d# i dh d& 6f d' 6d( d) d* i di d& 6f d+ 6dj d/ 6Z d0 i d1 d2 6d3 d4 6d5 d6 6d7 d8 6f d9 i dk d2 6d< d4 6d= d8 6f f Z	 d% Z
 dl d>  Z d?   Z d@   Z e j dA    Z e dB    Z dC   Z dD   Z dE   Z dF   Z e j d  dG    Z e Z e Z dH   Z e j d d  dI    Z e j d  dJ    Z e j d  dK    Z  dL   Z! e j d d d  dM    Z" e j d d-  dN    Z# e j d)  dO    Z$ dP   Z% e dQ    Z& e j d)  dR    Z' dS   Z( dT   Z) dU   Z* e dV    Z+ e j d  dW    Z, e j d d  dX    Z- e- Z. e dY    Z/ e dZ    Z0 e d[    Z1 e d\    Z2 d]   Z3 d^   Z4 d_   Z5 d`   Z6 RS(m   s   Looks for code which can be refactored

    This checker also mixes the astroid and the token approaches
    in order to create knowledge about whether a "else if" node
    is a true "else if" node, or a "elif" node.
    t   refactorings?   Consider merging these isinstance calls to isinstance(%s, (%s))s   consider-merging-isinstancesG   Used when multiple consecutive isinstance calls can be merged into one.t   R1701s   Consider using ternary (%s)s   consider-using-ternarys=   Used when one of known pre-python 2.5 ternary syntax is used.t   R1706s*   Boolean expression may be simplified to %ss   simplify-boolean-expressions=   Emitted when redundant pre-python 2.5 ternary syntax is used.t   R1709s   Too many nested blocks (%s/%s)s   too-many-nested-blockssv   Used when a function or a method has too many nested blocks. This makes the code less understandable and maintainable.t   R0101t	   old_namest   R1702s(   The if statement can be replaced with %ss   simplifiable-if-statements=   Used when an if statement can be replaced with 'bool(test)'. t   R0102t   R1703s*   Redefining argument with the local name %rs   redefined-argument-from-locals   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.t   R1704s!   Unnecessary "else" after "return"s   no-else-returns   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.t   R1705s   Disallow trailing comma tuples   trailing-comma-tuples  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.i   i    t
   minversiont   R1707sE   Do not raise StopIteration in generator, use return statement insteads   stop-iteration-returns   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 statementt   R1708s_   Either all return statements in a function should return an expression, or none of them should.s   inconsistent-return-statementss   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)t   R1710s   max-nested-blocksi   t   defaultt   intt   types   <int>t   metavars:   Maximum number of nested blocks for function / method bodyt   helps   never-returning-functionss   optparse.Valuess   sys.exitt   csvs   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.c         C   s3   t  j j |  |  i  |  _ |  j   d  |  _ d  S(   N(   R   t   BaseTokenCheckert   __init__t   _return_nodest   _initt   Nonet   _never_returning_functions(   t   selft   linter(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR/      s    	
c         C   s   g  |  _  g  |  _ d  |  _ d  S(   N(   t   _nested_blockst   _elifsR2   t   _nested_blocks_msg(   R4   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR1      s    		c         C   s   t  |  j j  |  _ d  S(   N(   t   sett   configt   never_returning_functionsR3   (   R4   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   open   s    c         C   s   t  j |  d d d  S(   Ns   dummy-variables-rgxR(   (   t
   lint_utilst   get_global_optionR2   (   R4   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt
   _dummy_rgx   s    c         C   s(   t  |  j t j  o' t  |  j j t  S(   N(   R	   t   valueR
   t   Constt   bool(   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _is_bool_const   s    c         C   s_   t  | j t j  r[ | j j } | r[ | | g k r[ | j | j f |  j k rX t Sq[ n  t	 S(   s	  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.
        (
   R	   t   parentR
   R   R   t   linenot
   col_offsetR7   t   TrueR   (   R4   R   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _is_actual_elif   s    	
c         C   s?  |  j  |  r d St | j  d k s= t | j  d k rA d S| j d } | j d } t | t j  r t | t j  s d S|  j |  } |  j |  } d } nS t | t j  r t | t j  s d S|  j |  } |  j |  } d } n d S| s| rd S| j	 j	 sd S|  j
 d d | d | f d S(	   su  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.
        Ni   i    s   'return bool(test)'s   'var = bool(test)'s   simplifiable-if-statementR   t   args(   RH   t   lenR   R   R	   R
   R   RC   t   AssignR@   t   add_message(   R4   R   t   first_brancht   else_brancht   first_branch_is_boolt   else_branch_is_boolt
   reduced_to(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _check_simplifiable_if   s2    
*		c         C   s   x t  |  D] \ } } | d } | d k r\ |  j j | | d | | d d g  q t j r t | |  r |  j j d  r |  j d d | j	 d q q q Wd  S(   Ni   t   elifi   s   trailing-comma-tuplet   linei    (
   t	   enumerateR7   t   extendt   sixt   PY3t   is_trailing_commaR5   t   is_message_enabledRL   t   start(   R4   t   tokenst   indext   tokent   token_string(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   process_tokens   s    
-c         C   s   |  j    d  S(   N(   R1   (   R4   t   _(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   leave_module  s    c         C   s   |  j  |  d  S(   N(   t   _check_nested_blocks(   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_tryexcept  s    c         C   s   |  j  r" |  j  j | j  r" d  S| j s/ d  S| j   } t | t j  sQ d  SxQ | j j	 t j
  D]: } | j | j k rg |  j d d | d | j f qg qg Wd  S(   Ns   redefined-argument-from-localR   RI   (   R?   t   matcht   nameRE   R   R	   R
   t   FunctionDefRI   t   nodes_of_classt
   AssignNameRL   (   R4   t	   name_nodeR   t   defined_argument(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt$   _check_redefined_argument_from_local  s    	c         C   s>   |  j  |  x* | j j t j  D] } |  j |  q# Wd  S(   N(   Rc   t   targetRh   R
   Ri   Rl   (   R4   R   Rf   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt	   visit_for   s    c         C   s5   | j  r1 t | j  t j  r1 |  j | j   n  d  S(   N(   Rf   R	   R
   Ri   Rl   (   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_excepthandler(  s    c         C   sT   xM | j  D]B \ } } | s" q
 n  x' | j t j  D] } |  j |  q5 Wq
 Wd  S(   N(   t   itemsRh   R
   Ri   Rl   (   R4   R   Ra   t   namesRf   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt
   visit_with-  s
    c         C   sC   | j  s d  St |  r? |  j |  r? |  j d d | n  d  S(   Ns   no-else-returnR   (   R   R   RH   RL   (   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _check_superfluous_else_return5  s    	c         C   s+   |  j  |  |  j |  |  j |  d  S(   N(   RR   Rc   Rs   (   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_if=  s    c         C   s:   |  j  |  j  g  |  _ |  j |  g  |  j | j <d  S(   N(   t%   _emit_nested_blocks_message_if_neededR6   t   _check_consistent_returnsR0   Rf   (   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   leave_functiondefD  s    	c         C   s   |  j  |  d  S(   N(   t&   _check_stop_iteration_inside_generator(   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_raiseN  s    c         C   s   | j    } t | t j  s, | j   r0 d St j | t  rF d S| j sS d St j	 | j  } | d k s | t j k r d S|  j |  r |  j d d | n  d S(   sH   Check if an exception of type StopIteration is raised inside a generatorNs   stop-iteration-returnR   (   t   frameR	   R
   Rg   t   is_generatorR   t   node_ignores_exceptiont   StopIterationt   exct
   safe_inferR2   t   Uninferablet+   _check_exception_inherit_from_stopiterationRL   (   R4   R   Rz   R~   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyRx   R  s     	c            s2   d j  t j    t   f d   |  j   D  S(   sH   Return True if the exception node in argument inherit from StopIterations   {}.StopIterationc         3   s!   |  ] } | j      k Vq d  S(   N(   t   qname(   t   .0t   _class(   t   stopiteration_qname(    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pys	   <genexpr>e  s    (   t   formatR   t   EXCEPTIONS_MODULEt   anyt   mro(   R~   (    (   R   s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR   a  s    c         C   s   |  j  |  d  S(   N(   t3   _check_raising_stopiteration_in_generator_next_call(   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt
   visit_callg  s    c         C   s   t  j | j  } t | d d  d k r | j   } t | t j  r | j   r t  j	 | t
  r |  j d d | q n  d S(   sI   Check if a StopIteration exception is raised by the call to next functionRf   t    t   nexts   stop-iteration-returnR   N(   R   R   t   funct   getattrRz   R	   R
   Rg   R{   R|   R}   RL   (   R4   R   t   inferredRz   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR   k  s    c         C   s   t  | j   t j  s d S|  j } | j | j   k rJ | g |  _ n x7 t |  j  D]& } | | j k rs Pn  |  j j   qZ Wt  | t j  r |  j	 |  r |  j r |  j j   q n  |  j j
 |  t |  t |  j  k r |  j |  n  d S(   s5   Update and check the number of nested blocks
        N(   R	   R   R
   Rg   R6   RD   t   reversedt   popR   RH   t   appendRJ   Ru   (   R4   R   t   nested_blockst   ancestor_node(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyRc   t  s    
!	c         C   sN   t  |  |  j j k rJ |  j d d | d d t  |  |  j j f n  d  S(   Ns   too-many-nested-blocksR   i    RI   (   RJ   R:   t   max_nested_blocksRL   (   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyRu     s    c            sN  t      t j t   } x|  j D]} t | t j  s" t | j  d k rV q" n  t	 j
 | j  } | s" t	 j |  r q" n  | j d k r q" n  | j d j   } | j d } | | k r   j |  n  t | t j  rg  | j   D] } | j   ^ q } n | j   g } | | j |  q" W  f d   | j   D S(   sH  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
        i   R	   i    i   c            s+   i  |  ]! \ } } |   k r | |  q S(    (    (   R   t   keyR@   (   t   duplicated_objects(    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pys
   <dictcomp>  s   	 	(   R9   t   collectionst   defaultdictR   R	   R
   t   CallRJ   RI   R   R   R   t   is_builtin_objectRf   t	   as_stringt   addt   Tuplet   iteredt   updateRp   (   R   t	   all_typest   callR   t   isinstance_objectt   isinstance_typest
   class_typeR   (    (   R   s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _duplicated_isinstance_types  s&    		((c      	   C   s   | j  d k r d S|  j |  } xX | j   D]J \ } } t d   | D  } |  j d d | d | d j |  f q/ Wd S(   s4   Check isinstance calls which can be merged together.t   orNc         s   s   |  ] } | Vq d  S(   N(    (   R   Rf   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pys	   <genexpr>  s    s   consider-merging-isinstanceR   RI   s   , (   t   opR   Rp   t   sortedRL   t   join(   R4   R   t
   first_argst   duplicated_namet   class_namesRq   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_boolop  s    c         C   s   |  j  | j  r0 |  j | j  \ } } } n4 |  j | j  r` |  j | j  \ } } } n d  S| j   t k r d } | j   } n6 d } d j d | j   d | j   d | j    } |  j	 | d | d | f d  S(	   Ns   simplify-boolean-expressions   consider-using-ternarys   {truth} if {cond} else {false}t   trutht   condt   falseR   RI   (
   t   _is_and_or_ternaryR@   t   _and_or_ternary_argumentst   _is_seq_based_ternaryt   _seq_based_ternary_paramst
   bool_valueR   R   R   RL   (   R4   R   R   t   truth_valuet   false_valuet   messaget
   suggestion(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_assign  s    	c         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(   s   
        Returns true if node is 'condition and true_value else false_value' form.

        All of: condition, true_value and false_value should not be a complex boolean expression
        R   i   i    i   t   and(   R	   R
   t   BoolOpR   RJ   R   (   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR     s    $!c         C   s0   |  j  d } |  j  d j  \ } } | | | f S(   Ni   i    (   R   (   R   R   t	   conditiont
   true_value(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR     s    c         C   s[   t  |  t j  oZ t  |  j t j t j f  oZ t |  j j  d k oZ t  |  j t j	  S(   sB   Returns true if node is '[false_value,true_value][condition]' formi   (
   R	   R
   t	   SubscriptR@   R   t   ListRJ   t   eltst   slicet   Index(   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR     s    c         C   s+   |  j  j \ } } |  j j  } | | | f S(   N(   R@   R   R   (   R   R   R   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR     s    c         C   sa   g  |  j  | j <| j t j  } g  | D]$ } | j   | j   k r) | ^ q) |  j  | j <d  S(   N(   R0   Rf   Rh   R
   R   Rz   (   R4   R   t   return_nodest   _rnode(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_functiondef  s    c         C   s   g  |  j  | j D] } | j d k	 r | ^ q } | s< d St |  t |  j  | j  k rq |  j |  rq d S|  j d d | d S(   s{  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.

        Ns   inconsistent-return-statementsR   (   R0   Rf   R@   R2   RJ   t   _is_node_return_endedRL   (   R4   R   t   _nodet   explicit_returns(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyRv     s    "c            s  t  | t j  r t St  | t j  rl y* | j j   d }   j |  rQ t SWql t j k
 rh ql Xn  t  | t j	  r t St  | t j
  rR| j s t St j |  s t St j | j  } | d k s | t j k r t S| j   j d  d } t j | |  } | d k	 r(t |  n g  } | rNt   f d   | D  St St  | t j  rg  | j   D]' } t  | t j  sq  j |  ^ qq} t |  d k St   f d   | j   D  S(   s   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.

        i    t   .ic         3   s   |  ] }   j  |  Vq d  S(   N(   R   (   R   t   _handler(   R4   (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pys	   <genexpr>I  s    i   c         3   s0   |  ]& } t  | t j  s   j |  Vq d  S(   N(   R	   R
   t   ExceptHandlerR   (   R   t   _child(   R4   (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pys	   <genexpr>U  s    N(   R	   R
   R   RG   R   R   t   inferedt    _is_function_def_never_returningt   InferenceErrort   Whilet   RaiseR~   R   t   is_node_inside_try_exceptR   R2   R   R   t   pytypet   splitt   get_exception_handlersR   R   R   t   get_childrenRg   R   t   sum(   R4   R   t   funcdef_nodeR~   t   exc_namet   handlersR   t   return_stmts(    (   R4   s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR     s<    	'c         C   s0   y | j    |  j k SWn t k
 r+ t SXd S(   s   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.
        N(   R   R3   t	   TypeErrorR   (   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR   X  s    	(   s?   Consider merging these isinstance calls to isinstance(%s, (%s))s   consider-merging-isinstancesG   Used when multiple consecutive isinstance calls can be merged into one.(   s   Consider using ternary (%s)s   consider-using-ternarys=   Used when one of known pre-python 2.5 ternary syntax is used.(   s*   Boolean expression may be simplified to %ss   simplify-boolean-expressions=   Emitted when redundant pre-python 2.5 ternary syntax is used.(   R   s   too-many-nested-blocks(   R    s   simplifiable-if-statement(   s*   Redefining argument with the local name %rs   redefined-argument-from-locals   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.(   s!   Unnecessary "else" after "return"s   no-else-returns   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.(   i   i    (   i   i    (   s_   Either all return statements in a function should return an expression, or none of them should.s   inconsistent-return-statementss   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)(   s   optparse.Valuess   sys.exitN(7   t   __name__t
   __module__t   __doc__R   t   ITokenCheckert   IAstroidCheckert   __implements__Rf   t   msgst   optionst   priorityR2   R/   R1   R<   R    t   cachedpropertyR?   t   staticmethodRC   RH   RR   R`   Rb   R   t   check_messagesRd   t   visit_tryfinallyt   visit_whileRl   Rn   Ro   Rr   Rs   Rt   Rw   Ry   Rx   R   R   R   Rc   Ru   R   R   R   t   visit_returnR   R   R   R   R   Rv   R   R   (    (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR   <   s               


				7					
					'			<t   RecommandationCheckerc           B   sm   e  Z e j f Z d  Z i d d 6d d 6Z e d	    Z e	 j
 d  d
    Z e	 j
 d  d    Z RS(   R   s@   Consider using enumerate instead of iterating with range and lens   consider-using-enumerates~   Emitted when code that iterates with range and len is encountered. Such code can be simplified by using the enumerate builtin.t   C0200sE   Consider iterating the dictionary directly instead of calling .keys()s   consider-iterating-dictionarys   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".t   C0201c         C   s5   t  j |   } | s t St  j |  o4 | j | k S(   N(   R   R   R   R   Rf   (   R   t   functionR   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _is_builtinv  s    c         C   s   t  j | j  } | s d  St | t j  s2 d  St | j t j  sW | j d k r[ d  St | j	 t j
 t j f  r |  j d d | n  d  S(   Nt   keyss   consider-iterating-dictionaryR   (   R   R   R   R	   R
   t   BoundMethodt   boundt   DictRf   RD   t   Fort   ComprehensionRL   (   R4   R   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR   }  s    %c         C   s  t  | j t j  s d S|  j | j j d  s5 d St | j j  d k rh t | j j d  rh d St | j j  d k r d St  | j j d t j  s d 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 Sx | j
 D] } x | j t j  D] } t  | j t j	  saq@n  t  | j t j  s|q@n  t  | j j t j	  sq@n  | j j j | j j k rq@n  | j | j j k rq@n  | j j   | j   k rq@n  |  j d d	 | d SWq'Wd S(
   s?   Emit a convention whenever range and len are used for indexing.Nt   rangei   i    iRJ   i   s   consider-using-enumerateR   (   R	   t   iterR
   R   R   R   RJ   RI   t   _is_constant_zerot   NameR   Rh   R   R@   R   R   Rf   Rm   R   RL   (   R4   R   t   second_funct   len_argst   iterating_objectt   childt	   subscript(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyRn     sF    /
(   s@   Consider using enumerate instead of iterating with range and lens   consider-using-enumerates~   Emitted when code that iterates with range and len is encountered. Such code can be simplified by using the enumerate builtin.(   sE   Consider iterating the dictionary directly instead of calling .keys()s   consider-iterating-dictionarys   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".(   R   R   R   R   R   Rf   R   R   R   R   R   R   Rn   (    (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR   g  s      
t
   NotCheckerc           B   s   e  Z d  Z e j f Z i d d 6Z d Z i d d 6d d	 6d	 d 6d d 6d
 d 6d d
 6d d 6d d 6Z e	 j
 f Z g  d D] Z d e j j j  e f ^ q| Z e j d  d    Z RS(   s   checks for too many not in comparison expressions

    - "not not" should trigger a warning
    - "not" followed by a comparison should trigger a warning
    s   Consider changing "%s" to "%s"s   unneeded-nots=   Used when a boolean expression contains an unneeded negation.t   C0113t   basics   >=t   <t   >s   <=s   !=s   ==s   not int   ins   is nott   isR9   t	   frozensets   %s.%sc   	      C   s  | j  d k r d  S| j } t | t j  rn | j  d k rn |  j d d | d | j   | j j   f nGt | t j  r| j } t	 | j
  d k r d  S| j
 d \ } } | |  j k r d  S| j   } | j d k r | d k r d  Sxq t j |  t j |  f D]Q } | s"d  St | |  j  r8d  St | t j  r| j   |  j k rd  SqWd	 | j   |  j | | j   f } |  j d d | d | j   | f n  d  S(
   Nt   nots   unneeded-notR   RI   i   i    t   __ne__s   ==s   %s %s %s(   R   t   operandR	   R
   t   UnaryOpRL   R   t   Comparet   leftRJ   t   opst
   reverse_opRz   Rf   R   t	   node_typet   skipped_nodest   InstanceR   t   skipped_classnames(	   R4   R   R  R  t   operatort   rightRz   t   _typeR   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_unaryop  s<    	!		%
(   s   Consider changing "%s" to "%s"s   unneeded-nots=   Used when a boolean expression contains an unneeded negation.(   R9   R
  (   R   R   R   R   R   R   R   Rf   R  R
   t   SetR  R   RW   t   movest   builtinsR  R   R   R  (    (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR    s    
&)c         C   s7   t  |  t j  o6 t  |  j t j  o6 |  j j d k S(   s!   Checks if node is len(SOMETHING).RJ   (   R	   R
   R   R   R   Rf   (   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _is_len_call  s    'c         C   s   t  |  t j  o |  j d k S(   Ni    (   R	   R
   RA   R@   (   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR     s    c         C   s%   t  |  t j t j t j t j f  S(   sC    Checks if node is an if, while, assert or if expression statement.(   R	   R
   R   R   t   Assertt   IfExp(   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   _node_is_test_condition  s    t
   LenCheckerc           B   s   e  Z d  Z e j f Z d Z i d
 d 6Z d Z d Z	 e
 j d  d    Z e
 j d  d    Z e
 j d  d	    Z RS(   s  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):
    * if len(sequence) == 0:
    * if len(sequence) != 0:
    * if len(sequence) > 0:
    RJ   s>   Do not use `len(SEQUENCE)` to determine if a sequence is emptys   len-as-conditions   Used when Pylint detects that len(sequence) is being used inside a condition to determine if a sequence is empty. Instead of comparing the length to 0, rely on the fact that empty sequences are false.t   C1801ic         C   s   t  |  r | j } x t | t j  r6 | j } q Wt |  sG d  S| | j k pe | j j |  sl d  S|  j d d | n  d  S(   Ns   len-as-conditionR   (	   R  RD   R	   R
   R   R!  t   testt	   parent_ofRL   (   R4   R   RD   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR   ,  s    	!c         C   sJ   t  | t j  rF | j d k rF t | j  rF |  j d d | n  d S(   s   `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):`R  s   len-as-conditionR   N(   R	   R
   R  R   R  R  RL   (   R4   R   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR  @  s    0c   	      C   s9  d | j  f g } | j | j  t t j |    } x t t |  d  D] } | | } | | d } | | d } t } t	 |  r | d
 k r t
 |  r t } n- t
 |  r | d k r t	 |  r t } n  | rN | j } x  | rt |  r| j } q Wt |  r1|  j d d	 | q1qN qN Wd  S(   NR   i   i   s   ==s   !=R  R  s   len-as-conditionR   (   s   ==s   !=R  (   s   ==s   !=R  (   R  RV   R  R   t	   itertoolst   chainR   RJ   R   R   R  RG   RD   R!  RL   (	   R4   R   R  t   ops_idxt   op_1t   op_2t   op_3t   error_detectedRD   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   visit_compareH  s$    	
$	$		(   s>   Do not use `len(SEQUENCE)` to determine if a sequence is emptys   len-as-conditions   Used when Pylint detects that len(sequence) is being used inside a condition to determine if a sequence is empty. Instead of comparing the length to 0, rely on the fact that empty sequences are false.(    (   R   R   R   R   R   R   Rf   R   R   R   R   R   R   R  R-  (    (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyR"  
  s    
c   	         s      } | j  t j k r  t St j    d d  } t t j | d  |   } t	 d   | D  } | s{ | r t S   f d   } |   } x(  |   !D] } d | j
 k r t Sq Wt S(   s  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
    i   c         S   s   |  j  d | j  d k S(   Ni    (   R[   (   t   other_tokent   _token(    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   <lambda>~  s    c         s   s*   |  ]  } | j  t j t j f k Vq d  S(   N(   R*   t   tokenizet   NEWLINEt   COMMENT(   R   R.  (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pys	   <genexpr>  s   c             sN   xG t  t       D]/ \ }  } | j t j t j f k r   |  Sq Wd S(   s4   Get the index denoting the start of the current linei    (   RU   R   R*   R1  R2  t   NL(   t   subindexR^   (   R]   R\   (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   get_curline_index_start  s    #t   =N(   t
   exact_typeR1  t   COMMAR   R&  t   isliceR2   R   t	   takewhileR   t   stringRG   (	   R\   R]   R^   t   left_tokenst   same_line_remaining_tokenst   is_last_elementR6  t   curline_startt	   prevtoken(    (   R]   R\   s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyRY   o  s$    	
			c         C   sP   |  j  t |    |  j  t |    |  j  t |    |  j  t |    d S(   s.   Required method to auto register this checker.N(   t   register_checkerR   R  R   R"  (   R5   (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   register  s    (   R   R   R&  R1  R
   R    RW   t   pylintR   R   R   R=   t   pylint.checkersR   R   R.   R   t   BaseCheckerR   R  R  R   R!  R"  RY   RC  (    (    (    s:   lib/python2.7/site-packages/pylint/checkers/refactoring.pyt   <module>   s.   		  -[;			e	(