
    e26                         d 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 ej                  ej                  ej                  ej                  fZ ed      Z ed      Zd	Zd
ej$                  defdZ G d de      Zy)z*Comparison checker from the basic checker.    N)nodes)utils)_BasicChecker)HIGH)==!=<>z<=z>=)isis notr   r   zbuiltins.typecallreturnc                     t        | t        j                        xr' t        | j                        dk(  xr | j
                   S )z3Is this a call with exactly 1 positional argument ?   )
isinstancer   Calllenargskeywords)r   s    I/usr/lib/python3/dist-packages/pylint/checkers/base/comparison_checker.py_is_one_arg_pos_callr      s1    dEJJ'UC		Na,?UDUU    c                   h   e Zd ZdZddddddgifdd	d
dddZ	 d(dej                  dej                  dej                  deddf
dZ		 d(dej                  dej                  dej                  deddf
dZ
dej                  dej                  ddfdZdej                  ddfdZdej                  ddfdZdej                  ddfdZ ej                   dddddd d!      dej                  ddfd"       Zdej                  ddfd#Zdej                  d$ej                  d%ed&ej                  ddf
d'Zy))ComparisonCheckera  Checks for comparisons.

    - singleton comparison: 'expr == True', 'expr == False' and 'expr == None'
    - yoda condition: 'const "comp" right' where comp can be '==', '!=', '<',
      '<=', '>' or '>=', and right can be a variable, an attribute, a method or
      a function
    )Comparison %s should be %ssingleton-comparisonzQUsed when an expression is compared to singleton values like True, False or None.z4Use isinstance() rather than type() for a typecheck.unidiomatic-typecheckzThe idiomatic way to perform an explicit typecheck in Python is to use isinstance(x, Y) rather than type(x) == Y, type(x) is Y. Though there are unusual situations where these give different results.	old_names)W0154zold-unidiomatic-typecheck)zBIn '%s', use '%s' when comparing constant literals not '%s' ('%s')literal-comparisonzUsed when comparing an object to a literal, which is usually what you do not want to do, since you can compare to a different literal than what was expected altogether.)zRedundant comparison - %scomparison-with-itselfz/Used when something is compared against itself.)z=Comparison between constants: '%s %s %s' has a constant valuecomparison-of-constantszWhen two literals are compared with each other the result is a constant. Using the constant directly is both easier to read and more performant. Initializing 'True' and 'False' this way is not required since Python 2.3.)z;Comparing against a callable, did you omit the parenthesis?comparison-with-callablezThis message is emitted when pylint detects that a comparison with a callable was made, which might suggest that some parenthesis were omitted, resulting in potential unwanted behaviour.)r   nan-comparisonzRUsed when an expression is compared to NaN values like numpy.NaN and float('nan').)C0121C0123R0123R0124R0133W0143W0177
left_valueright_value	root_nodechecking_for_absencer   Nc           
         t        j                  |      r|j                  |}}n%t        j                  |      r|j                  |}}nyddd}|dv rd}ddd}	d	d
d}
||u}|j                  ||   j                  |j	                         |j	                               |t        j
                  |      s|rdndj                  |	|   j                  |j	                                     |
|         }n1||   j                  |j	                         |j	                               }| j                  d|d|j	                          d|f       y)z=Check if == or != is being used to compare a singleton value.Nz
'{} is {}'z'{} is not {}')FT>   FTzB{} if checking for the singleton value {}, or {} if testing for {}znot {}z{}
truthiness	falsiness)TFz
'bool({})'z'{}'r   'noder   )r   is_singleton_constvalueformat	as_stringis_test_conditionadd_message)selfr,   r-   r.   r/   	singletonother_valuesingleton_comparison_examplesuggestion_templatetruthiness_exampletruthiness_phrasechecking_truthiness
suggestions                r   _check_singleton_comparisonz-ComparisonChecker._check_singleton_comparisonT   sz    ##J/%/%5%5{{I%%k2%0%6%6
{I/;CS'T$ %T   *2!>'3K H #,3G"G,33,-ABII((*K,A,A,C  !229=BU !&&':;BB#--/
 ""56J" 66JKRR$$&(=(=(?J 	"i))+,A.
; 	 	
r   c                   	
 dt         j                  dt        fd	dt         j                  dt        fd
dt         j                  dt        f	
fd} ||      }|s	 ||      sy d}|rd}|rd| d	|j                          d
}nd| d	|j                          d
}| j	                  d|d|j                          d|f       y )Nr5   r   c                 &   	 t        | t        j                        rgt        | j                        dk(  rO| j                  d   j
                  j                         dk(  r%| j                         d   j                         dk(  ryy# t        $ r Y yw xY w)Nr   r   nanzbuiltins.floatTF)
r   r   r   r   r   r7   lowerinferredpytypeAttributeErrorr5   s    r   _is_float_nanz>ComparisonChecker._check_nan_comparison.<locals>._is_float_nan   sx    	dEJJ/C		Na4G		!**002e; MMOA.557;KK#! s   B B 	BBc                     t        | t        j                        rK| j                  dk(  r<t        | j                  t        j
                        r| j                  j                  dv S y)NNaN>   npnmpnumpyF)r   r   	AttributeattrnameexprNamenamerM   s    r   _is_numpy_nanz>ComparisonChecker._check_nan_comparison.<locals>._is_numpy_nan   sD    $0T]]e5Kdii499>>-CCCr   c                 (     |       xs  |       S N )r5   rN   rY   s    r   _is_nanz8ComparisonChecker._check_nan_comparison.<locals>._is_nan   s     &=-*==r    znot r3   zmath.isnan(z)'r$   r4   )r   NodeNGboolr9   r;   )r<   r,   r-   r.   r/   r]   nan_leftabsence_textrD   rN   rY   s            @@r   _check_nan_comparisonz'ComparisonChecker._check_nan_comparison   s    
	 
	 
		 	 		>%,, 	>4 	> :& 4!L\N+k6K6K6M5NbQJ\N+j6J6J6L5MRPJi))+,A.
; 	 	
r   literalr5   c                    t        |t        j                  t        j                  t        j                  f      }d}t        |t        j
                        rQt        |j                  t              s|j                  yt        |j                  t        t        t        t        f      }|s|rM|j                         }d|v rd}d}nd}d}|j                  ||      }| j                  d||||f|t               yy)	zMCheck if we compare to a literal, which is usually what we do not want to do.FNr   r   r   r   r    )r   r5   
confidence)r   r   ListDictSetConstr7   r`   bytesstrintfloatr9   replacer;   r   )	r<   rd   r5   is_other_literalis_constincorrect_node_strequal_or_not_equalis_or_is_notfixed_node_strs	            r   _check_literal_comparisonz+ComparisonChecker._check_literal_comparison   s     &g

EJJ		/RSgu{{+'--.'--2G!'--%c51IJH'!%!1--%)"'%)"#/770N $&& "	   
 (r   c                    |j                   }|j                  d   d   }|j                  d   d   }t        |t        j                        r3t        |t        j                        r|j
                  }|j
                  }nLt        |t        j                        r2t        |t        j                        r|j                  }|j                  }||k(  r | d| d| }| j                  d||f       yy)zCheck if identifier is compared against itself.

        :param node: Compare node
        :Example:
        val = 786
        if val == val:  # [comparison-with-itself]
            pass
        r   r    r!   r4   N)	leftopsr   r   rj   r7   rW   rX   r;   )r<   r5   left_operandright_operandoperatorrD   s         r   _check_logical_tautologyz*ComparisonChecker._check_logical_tautology   s     yyA88A;q>lEKK0Z5;;6
 (--L)//Mejj1j5::7
 (,,L)..M=((>8*Am_EJ5D
}U )r   c                 0   |j                   }t        |t        j                        sy|j                  d   d   }t        |t        j                        sy|j                  d   d   }| j                  d||j                  ||j                  ft               y)zGWhen two constants are being compared it is always a logical tautology.Nr   r   r"   )r5   r   rf   )ry   r   r   rj   rz   r;   r7   r   )r<   r5   r{   r|   r}   s        r   _check_constants_comparisonz-ComparisonChecker._check_constants_comparison   s    yy,4A-588A;q>%$$h0C0CD	 	 	
r   c                    |j                   d   d   }|t        vry t        j                  t        j
                  f}|j                  |j                   d   d   }}d}||fD ]Y  }t        j                  |      }t        ||      s%d|j                         vs8t        d |j                  D              rU|dz  }[ |dk(  r| j                  d|       y y )Nr   r   ztyping._SpecialFormc              3   P   K   | ]  }t        |t        j                           y wr[   )r   r   Raise).0xs     r   	<genexpr>z?ComparisonChecker._check_callable_comparison.<locals>.<genexpr>  s     N1Jq%++6Ns   $&r#   rM   )rz   COMPARISON_OPERATORSr   FunctionDefastroidBoundMethodry   r   
safe_inferr   decoratornamesanybodyr;   )	r<   r5   r}   bare_callablesr{   r|   number_of_bare_callablesoperandrJ   s	            r   _check_callable_comparisonz,ComparisonChecker._check_callable_comparison	  s    88A;q>//++W-@-@A&*ii!Qm $% #]2 		.G''0H 8^4)1H1H1JJNNN(A-(		. $q(7dC )r   r   r    r!   r"   r#   r$   c                    | j                  |       | j                  |       | j                  |       | j                  |       t	        |j
                        dk7  ry |j                  }|j
                  d   \  }}|dv r| j                  ||||dk(         |dv r| j                  ||||dv        |dv r| j                  ||       y y )	Nr   r   >   r   r   r   )r/   >   r   r   r   r   >   r   r   >   r   r   )
r   r~   _check_unidiomatic_typecheckr   r   rz   ry   rE   rc   rv   )r<   r5   ry   r}   rights        r   visit_comparezComparisonChecker.visit_compare   s     	''-%%d+))$/((. txx=Ayy((1+%|#,,eTD8H -  33&&eTDT8T '  ''**5$7 (r   c                     |j                   d   \  }}|t        v r-|j                  }t        |      r| j	                  ||||       y y y )Nr   )rz   TYPECHECK_COMPARISON_OPERATORSry   r   _check_type_x_is_y)r<   r5   r}   r   ry   s        r   r   z.ComparisonChecker._check_unidiomatic_typecheckB  sK    ((1+%5599D#D)''dHeD * 6r   ry   r}   r   c                    t        j                  |j                        }t        |t        j
                        r|j                         t        k(  sy|dv rt        |      rt        j                  |j                        }t        |t        j
                        rJ|j                         t        k(  r3t        j                  |j                  d         }t        |t              sy| j                  d|       y)z(Check for expressions like type(x) == Y.N>   r   r   r   r   rM   )r   r   funcr   r   ClassDefqname
TYPE_QNAMEr   r   LITERAL_NODE_TYPESr;   )r<   r5   ry   r}   r   	left_func
right_func	right_args           r   r   z$ComparisonChecker._check_type_x_is_yI  s     $$TYY/	y%..1ioo6G:6U'',@,G))%**5J:u~~6$$&*4 ",,UZZ];	!)-?@0t<r   )F)__name__
__module____qualname____doc__msgsr   r_   Comparer`   rE   rc   rv   r~   r   r   r   only_required_for_messagesr   r   rl   r   r\   r   r   r   r      s   
 C#= ABC






U0Dn &+6
LL6
 \\6
 ==	6

 #6
 
6
z &++
LL+
 \\+
 ==	+

 #+
 
+
Z!||!+0==!	!FVU]] Vt V6
 
$ 
$Du}} D D. &U%% !"8%-- 8D 882E E4 E=mm= ll= 	=
 ||= 
=r   r   )r   r   r   pylint.checkersr   "pylint.checkers.base.basic_checkerr   pylint.interfacesr   rj   rh   rg   ri   r   	frozensetr   r   r   r_   r`   r   r   r\   r   r   <module>r      s}   
 1   ! < "kk5::uzz599E  !CD !*+G!H 
Vu|| V V
I= I=r   