bg$|ddlmZddlZddlmZmZmZddlmZddlm Z ddl m Z m Z Gddej ZdS) ) annotationsN)basesnodesutil)checkers)utils)HIGH INFERENCEceZdZdZdZdddddgifdd Zd Zejdd&dZ e d'dZ ejdd(dZ ejdd)dZ d)dZd*dZd+d!Ze d,d$Zd%S)-ImplicitBooleanessCheckeraChecks 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 emptyuse-implicit-booleaness-not-lenaUsed 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. old_names)C1801zlen-as-condition)z7'%s' can be simplified to '%s' as an empty %s is falsey&use-implicit-booleaness-not-comparisonzUsed when Pylint detects that collection literal comparison is being used to check for emptiness; Use implicit booleaness instead of a collection classes; empty collections are considered as false)C1802C1803node nodes.CallreturnNonectj|dsdS|j}t|tjr!|j}t|tj!tj||sdS|jd}tjtj tj tj f}t||r| d|tdS t|}n#t j$rYdSwxYw||t'fddD}dvs|r4||s!| d|t*dSdSdS)Nlenrrr confidencec3 K|]}|vV dSNr.0tmother_classess /builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib/python3.11/site-packages/pylint/checkers/refactoring/implicit_booleaness_checker.py z7ImplicitBooleanessChecker.visit_call..rs9  $%A       )strtuplelistsetrange)ris_call_of_nameparent isinstancerBoolOpis_test_conditionargsListCompSetCompDictComp GeneratorExp add_messager nextinferastroidInferenceErrorbase_names_of_instanceanyinstance_has_boolr )selfrr,len_arggenerator_or_comprehensioninstanceaffected_by_pep8r"s @r# visit_callz$ImplicitBooleanessChecker.visit_callMs $T511  F.. #]F.. #&tV44  F)A, N M N   & " g9 : :    1     F GMMOO,,HH%    FF 44X>>    )H      n $ $  %%)%;%;H%E%E %   1$       % $ $ $s!C??DD class_defnodes.ClassDefboolc` |ddS#tj$rYnwxYwdS)N__bool__TF)getattrr8AttributeInferenceError)rCs r#r<z+ImplicitBooleanessChecker.instance_has_bool~sH    j ) ) )4.    C us ++ nodes.UnaryOpct|tjrD|jdkr;t j|jdr#|d|tdSdSdSdS)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):`. notrrrN) r-rUnaryOpoprr+operandr5r r=rs r# visit_unaryopz'ImplicitBooleanessChecker.visit_unaryops tU] + + 5  %dlE::!   1             r%r nodes.Comparec0||dSr)-_check_use_implicit_booleaness_not_comparisonrPs r# visit_comparez'ImplicitBooleanessChecker.visit_compares ::4@@@@@r%c 8 tj|jptj|j}|jD]\}}tj|ptj|}||z r|r|jn|}|r|n|j}tj|}|_|| t fddD} | s||r|dvr3| d| ||||tdS)zBCheck for left side and right side of the node for empty literals.Nc3 K|]}|vV dSrrrs r#r$zZImplicitBooleanessChecker._check_use_implicit_booleaness_not_comparison..s911,-A'111111r%)r'r(dictr)><===>=<>!=r)r0rr) ris_base_containerleftis_empty_dict_literalops safe_inferr:r;r<r5!_implicit_booleaness_message_argsr ) r=ris_left_empty_literaloperator comparatoris_right_empty_literal target_node literal_nodetarget_instanceis_base_comprehension_typer"s @r#rTzGImplicitBooleanessChecker._check_use_implicit_booleaness_not_comparisons!& 7 I! ! !4  ( 3 3  %)H# #  Hj%*%<&&&9,Z88 # &(== +AQdiiz -CRzz "'"2;"?"?"*!%!>    #k: & &   k5: . . 4;3C 4*/99;;BBBMM  eouz%B C C 4'1133M!.PPPPzDImplicitBooleanessChecker.base_names_of_instance..s!C!C!CQ!&!C!C!Cr%)r-rInstancer ancestors)rs r#r:z0ImplicitBooleanessChecker.base_names_of_instancesF dEN + + DI;!C!C$..2B2B!C!C!CC C r%N)rrrr)rCrDrrE)rrJrr)rrRrr)rrmrr&)rjrmrfr&rirmrrw)rrrr)__name__ __module__ __qualname____doc__rmsgsoptionsronly_required_for_messagesrB staticmethodr<rQrUrTrvrdr:rr%r#r r so$$L D ` - 3 89 :     D(G%U%&GHH...IH.`\&U%&GHH   IH &U%&NOOAAAPOA,,,,\&&&&<<<<&   \   r%r ) __future__rr8rrrpylintrpylint.checkersrpylint.interfacesr r BaseCheckerr rr%r#rs #"""""&&&&&&&&&&!!!!!!--------^^^^^ 4^^^^^r%