
    e                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ erddlmZ  G d	 d
ej                        ZddZy)zELooks for try/except statements with too much code in the try clause.    )annotations)TYPE_CHECKING)nodes)checkers)utils)HIGH)PyLinterc                  V    e Zd ZdZdZddiZ ej                  d      d	d       Zd	dZ	y)
)ConsiderRefactorIntoWhileConditionCheckera1  Checks for instances where while loops are implemented with a constant condition
    which.

    always evaluates to truthy and the first statement(s) is/are if statements which, when
    evaluated.

    to True, breaks out of the loop.

    The if statement(s) can be refactored into the while loop.
    consider_refactoring_into_whileR3501)zGConsider using 'while %s' instead of 'while %s:' an 'if', and a 'break')consider-refactoring-into-while-conditionzEmitted when `while True:` loop is used and the first statement is a break condition. The ``if / break`` construct can be removed if the check is inverted and moved to the ``while`` statement.r   c                &    | j                  |       y N) _check_breaking_after_while_true)selfnodes     ]/usr/lib/python3/dist-packages/pylint/extensions/consider_refactoring_into_while_condition.pyvisit_whilez5ConsiderRefactorIntoWhileConditionChecker.visit_while,   s    --d3    c           	     L   t        |j                  t        j                        r|j                  j	                         syg }|j
                  D ]/  }t        |t        j                        s n|j                  |       1 g }d}|D ]  }|s't        |j
                  d   t        j                        s n|j                  |       |j                  }|sN|d   }t        |t        j                        sd}n|j                  |       t        |t        j                        s|j                  }|r] |D cg c],  }t        |j
                  d   t        j                        s+|. }}dj                  |D cg c]%  }dt        j                  |j                         d' c}      }	t        |      dk(  r"t        j                  |d   j                        }	|	sy| j                  d	||j                  |	|j                  j!                         ft"        
       yc c}w c c}w )z@Check that any loop with an ``if`` clause has a break statement.NFr   Tz and ()   r   )r   lineargs
confidence)
isinstancetestr   Const
bool_valuebodyIfappendBreakorelsejoinr   not_condition_as_stringlenadd_messagelineno	as_stringr   )
r   r   pri_candidatesn
candidatestaintedcr&   orelse_nodemsgs
             r   r   zJConsiderRefactorIntoWhileConditionChecker._check_breaking_after_while_true0   s   $))U[[19M9M9O)+ 	%Aa*!!!$	% 
 	,AjEKK@a XXF$Qi!+uxx8"G%%k2!+uxx8$++ 	, ",RAz!&&)U[[/QaR
RllCMNaq..qvv67q9N
 z?a//
10B0BCC7tyy**,- 	 	
 SNs   7,H$H9*H!N)r   znodes.WhilereturnNone)
__name__
__module____qualname____doc__namemsgsr   only_required_for_messagesr   r    r   r   r   r      sE    	 -D 
D &U%%&QR4 S4)
r   r   c                8    | j                  t        |              y r   )register_checkerr   )linters    r   registerrA   \   s    
EfMNr   N)r@   r	   r4   r5   )r9   
__future__r   typingr   astroidr   pylintr   pylint.checkersr   pylint.interfacesr   pylint.lintr	   BaseCheckerr   rA   r=   r   r   <module>rJ      s>   
 L "     ! "$D
0D0D D
NOr   