
    yIf'8                        d dl mZ d dlZd dlZd dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZ ddZ G d dej                  ZdS )    )annotationsN)basesnodesutil)checkers)utils)HIGH	INFERENCEnodestr | nodes.NodeNGreturnboolc                ^    t          | t          j                  o| j        dk    o| j        duS )Nr   F)
isinstanceastroidConstvaluer   s    k/var/www/piapp/venv/lib/python3.11/site-packages/pylint/checkers/refactoring/implicit_booleaness_checker.py_is_constant_zeror      s0     	4''WDJ!OW
RW@W    c            	      N   e Zd ZdZdZdddddgifdd	d
dddgdfdddddgdfdZdZh dZ ej	        d          d2d            Z
ed3d            Z ej	        d          d4d             Z ej	        d!d
d          d5d#            Zd5d$Zd5d%Zd6d(Zd7d-Zed8d0            Zd1S )9ImplicitBooleanessCheckera  Checks for incorrect usage of comparisons or len() inside conditions.

    Incorrect usage of len()
    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):
    * bool(len(sequence))

    Incorrect usage of empty literal sequences; (), [], {},

    For empty sequences, (dicts, lists, tuples), use the fact that empty sequences are false.

        Yes: if variable:
             if not variable

        No: if variable == empty_literal:
            if variable != empty_literal:

    Problems detected:
    * comparison such as variable == empty_literal:
    * comparison such as variable != empty_literal:
    refactoringzQDo not use `len(SEQUENCE)` without comparison to determine if a sequence is emptyuse-implicit-booleaness-not-lenzEmpty sequences are considered false in a boolean context. You can either remove the call to 'len' (``if not x``) or compare the length against a scalar (``if len(x) > 1``).	old_names)C1801zlen-as-condition)zV"%s" can be simplified to "%s", if it is strictly a sequence, as an empty %s is falsey&use-implicit-booleaness-not-comparisona*  Empty sequences are considered false in a boolean context. Following this check blindly in weakly typed code base can create hard to debug issues. If the value can be something else that is falsey but not a sequence (for example ``None``, an empty string, or ``0``) the code will not be equivalent.zX"%s" can be simplified to "%s", if it is striclty a string, as an empty string is falsey0use-implicit-booleaness-not-comparison-to-stringa'  Empty string are considered false in a boolean context. Following this check blindly in weakly typed code base can create hard to debug issues. If the value can be something else that is falsey but not a string (for example ``None``, an empty sequence, or ``0``) the code will not be equivalent.F)C1901zcompare-to-empty-string)default_enabledr   zH"%s" can be simplified to "%s", if it is strictly an int, as 0 is falsey.use-implicit-booleaness-not-comparison-to-zeroa#  0 is considered false in a boolean context. Following this check blindly in weakly typed code base can create hard to debug issues. If the value can be something else that is falsey but not an int (for example ``None``, an empty string, or an empty sequence) the code will not be equivalent.)C2001zcompare-to-zero)C1802C1803C1804C1805 >   is!===is notr   
nodes.Callr   Nonec                   t          j        |d          sd S |j        }t          |t          j                  r!|j        }t          |t          j                  !t          j        ||          sd S |j        d         }t          j        t          j	        t          j
        t          j        f}t          ||          r|                     d|t                     d S 	 t          |                                          }n# t           j        $ r Y d S w xY w|                     |          t'          fddD                       }dv s|r4|                     |          s!|                     d|t*                     d S d S d S )Nlenr   r   r   
confidencec              3      K   | ]}|v V  	d S Nr(   .0tmother_classess     r   	<genexpr>z7ImplicitBooleanessChecker.visit_call.<locals>.<genexpr>   s9       
 
$%A
 
 
 
 
 
r   )strtuplelistsetrange)r   is_call_of_nameparentr   r   BoolOpis_test_conditionargsListCompSetCompDictCompGeneratorExpadd_messager	   nextinferr   InferenceErrorbase_names_of_instanceanyinstance_has_boolr
   )selfr   r@   len_arggenerator_or_comprehensioninstanceaffected_by_pep8r8   s          @r   
visit_callz$ImplicitBooleanessChecker.visit_callo   s   
 $T511 	F .. 	#]F .. 	# &tV44 	F)A,NMN	&
" g9:: 	1    
 F	GMMOO,,HH% 	 	 	FF	 44X>> 
 
 
 
)H
 
 
 
 
 n$$ %%)%;%;H%E%E % 1$       %$$$s   !C? ?DD	class_defnodes.ClassDefr   c                `    	 |                      d           dS # t          j        $ r Y nw xY wdS )N__bool__TF)getattrr   AttributeInferenceError)rU   s    r   rN   z+ImplicitBooleanessChecker.instance_has_bool   sH    	j)))4. 	 	 	C	us    ++nodes.UnaryOpc                    t          |t          j                  rD|j        dk    r;t	          j        |j        d          r#|                     d|t                     dS dS dS 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):`.
        notr0   r   r1   N)	r   r   UnaryOpopr   r?   operandrH   r	   rO   r   s     r   visit_unaryopz'ImplicitBooleanessChecker.visit_unaryop   s     tU]++	5  %dlE:: ! 1      	 	    r   r   nodes.Comparec                    | j                             d          r|                     |           | j                             d          s| j                             d          r|                     |           d S d S )Nr   r"   -use-implicit-booleaness-not-comparison-to-str)linteris_message_enabled-_check_use_implicit_booleaness_not_comparison_check_compare_to_str_or_zerora   s     r   visit_comparez'ImplicitBooleanessChecker.visit_compare   s     ;))*RSS 	E>>tDDD;))<
 
 	5[++;
 
	5
 ..t44444	5 	5r   c                   d|j         fg|j        }t          |          }t          t	          j        |           }t          t          |          dz
            D ]}||dz            }|| j        vr||         }||dz            }d}	| j	        
                    d          rt          |          rd}	|}
nt          |          rd}	|}
|	r|                                 d| d|                                 }|dv r|
                                nd	|
                                 }|                     d||f|t          
           d}	| j	        
                    d          rd}t          j        |          rd}	|                                }n*t          j        |          rd}	|                                }|	r=|dv rd	| n|}|                     d|                                |f|t          
           d S )N       Fr"   T >   r*   r,   not rC   r   r2   re   >   r)   r+   r   )leftopsiterr<   	itertoolschainr>   r0   
_operatorsrf   rg   r   	as_stringrH   r	   r   is_empty_str_literal)rO   r   rs   iter_opsall_opsops_idxop_2op_1op_3error_detectedr_   original
suggestion	node_names                 r   ri   z7ImplicitBooleanessChecker._check_compare_to_str_or_zero   so   
 13DI.J.J99y122S\\A-.. 5	 5	G7Q;'D4?**7#D7Q;'D"N{--@  + %T** %)NBB&t,, %)NB! +"&.."2"2NNTNNDNN<L<LNNH  #333 4BLLNN44 
 $$H&
3!#'	 %    &+N{--?   	-d33 1%)N $ 0 0II/55 1%)N $ 0 0I! 	.2l.B.B*y***	  $$J"nn..
;!#'	 %   a5	 5	r   c           	     8  
 t          j        |j                  pt          j        |j                  }|j        D ]\  }}t          j        |          pt          j        |          }||z  r|r|j        n|}|r|n|j        }t          j        |          }|_|                     |          
t          
fddD                       }	|	s|                     |          r|dv r3| 	                    d| 
                    |||          |t                     dS )zBCheck for left side and right side of the node for empty literals.Nc              3      K   | ]}|v V  	d S r4   r(   r5   s     r   r9   zZImplicitBooleanessChecker._check_use_implicit_booleaness_not_comparison.<locals>.<genexpr>  s9       1 1,-A'1 1 1 1 1 1r   )r;   r<   dictr=   >   <=>=<>r*   r+   r   rq   )r   is_base_containerrr   is_empty_dict_literalrs   
safe_inferrL   rM   rN   rH   !_implicit_booleaness_message_argsr	   )rO   r   is_left_empty_literaloperator
comparatoris_right_empty_literaltarget_nodeliteral_nodetarget_instanceis_base_comprehension_typer8   s             @r   rh   zGImplicitBooleanessChecker._check_use_implicit_booleaness_not_comparison  s    !& 7I!
 !
 !4(33 	
 %)H #	 #	 Hj%*%<& & &9,Z88 #
 &(== +AQdiiz-CRzz"'"2;"?"?"*!%!<!<_!M!M-0 1 1 1 11Q1 1 1 . .* 2 d6L6L#7 7   AAA$$@!CC((K  "#' %   9#	 #	r   nodes.NodeNGr:   c                    t           j        dt           j        dt           j        dt           j        di                    t          |          d          S )Nr<   r;   r   r:   iterable)r   ListTupleDictr   gettypera   s     r   _get_node_descriptionz/ImplicitBooleanessChecker._get_node_description3  s?    JKJK	

 #d4jj*
%
%	&r   r   r   r   tuple[str, str, str]c                   |                      |          }dddd                    |d          }d}t          |t          j                  r$|j        r|j                                         d}n:t          |t          j        t          j        f          r|                                }| d| d| }|d	k    r| nd
| }|||fS )zMHelper to get the right message for "use-implicit-booleaness-not-comparison".z[]z()z{})r<   r;   r   r   xz(...)ro   r*   rp   )	r   r   r   r   Callfuncrx   	AttributeName)	rO   r   r   r   descriptioncollection_literalinstance_nameoriginal_comparisonr   s	            r   r   z;ImplicitBooleanessChecker._implicit_booleaness_message_args;  s     00>>
 
 #k:
&
&	 	
 k5:.. 	4;3C 	4*/99;;BBBMMeouz%BCC 	4'1133M!.PPPP<NPP+3t+;+;''AWAWAW
"J;;r   %util.UninferableBase | bases.Instance	list[str]c                    t          | t          j                  r'| j        gd |                                 D             z   S g S )zReturn all names inherited by a class instance or those returned by a
        function.

        The inherited names include 'object'.
        c                    g | ]	}|j         
S r(   )name)r6   r   s     r   
<listcomp>zDImplicitBooleanessChecker.base_names_of_instance.<locals>.<listcomp>X  s    !C!C!CQ!&!C!C!Cr   )r   r   Instancer   	ancestorsr   s    r   rL   z0ImplicitBooleanessChecker.base_names_of_instanceN  sF     dEN++ 	DI;!C!C$..2B2B!C!C!CCC	r   N)r   r-   r   r.   )rU   rV   r   r   )r   r[   r   r.   )r   rc   r   r.   )r   r   r   r:   )r   r   r   r:   r   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   msgsoptionsrw   r   only_required_for_messagesrT   staticmethodrN   rb   rj   ri   rh   r   r   rL   r(   r   r   r   r      s       $ $L D `-+ 89:

 g> $)BC 
 W<
 !&5Q4RSS	
?) )DV G---J%U%&GHH. . . IH.`    \ &U%&GHH   IH &U%0:8 
5 5 5 
5= = = =~, , , ,\& & & &< < < <& 
 
 
 \
 
 
r   r   )r   r   r   r   )
__future__r   ru   r   r   r   r   pylintr   pylint.checkersr   pylint.interfacesr	   r
   r   BaseCheckerr   r(   r   r   <module>r      s   
 # " " " " "      & & & & & & & & & &       ! ! ! ! ! ! - - - - - - - -   @ @ @ @ @ 4 @ @ @ @ @r   