bISc@sdZddlZddlZddlmZddlZddlmZmZmZddlm Z ddl m Z ddl m Z ddlmZmZmZmZmZmZmZmZmZmZmZmZd Zd Zd Zid d dfd6dddfd6dddfd6dddfd6dddfd6d d!d"fd#6d$d%d&fd'6d(d)d*fd+6d,d-d.fd/6d0d1d2fd36d4d5d6fd76d8d9d:fd;6d<d=d>fd?6d@dAdBfdC6dDdEdFfdG6dHdIdJfdK6dLdMdNfdO6dPdQdRfdS6dTdUdVfdW6dXdYdZfd[6Zd\e fd]YZd^efd_YZ ej!d`dafkre ZndbZ"dS(cs#variables checkers for Python code iN(tcopy(t are_exclusivetbuiltin_lookuptAstroidBuildingException(tfile_from_modpath(tIAstroidChecker(t BaseChecker( t PYMETHODStis_ancestor_namet is_builtintis_defined_beforetis_errortis_func_defaulttis_func_decoratort assign_parenttcheck_messagestis_inside_excepttclobber_in_excepttget_all_elementscs/t|tjo.tfd|jDS(sEReturns True if stmt in inside the else branch for a parent For stmt.c3s|]}|jVqdS(N(t parent_of(t.0t else_stmt(tstmt(sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pys &s(t isinstancetastroidtFortanytorelse(tparentR((RsS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytin_for_else_branch#scCsqy|j|j}Wnttfk r3dSXy||}Wntk rVdSXt|tjrm|SdS(sget overridden method if anyN(tlocal_attr_ancestorstnextt StopIterationtKeyErrortNoneRRtFunction(tklasstnameRt meth_node((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytoverridden_method(s cCsd}|jj}|jj|krk|j|jkrOd|j}q|jrd|j}qn|jrd|j|f}n|S(sxreturn extra information to add to the message for unpacking-non-sequence and unbalanced-tuple-unpacking errors ts %ss defined at line %ss defined at line %s of %s(trootR%tlinenot as_string(tnodetinferedtmoretinfered_module((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt_get_unpacking_extra_info8s  s#Using variable %r before assignmentsused-before-assignmentsLUsed when a local variable is accessed before it's assignment.tE0601sUndefined variable %rsundefined-variables,Used when an undefined variable is accessed.tE0602s%Undefined variable name %r in __all__sundefined-all-variables>Used when an undefined variable name is referenced in __all__.tE0603s7Invalid object %r in __all__, must contain only stringssinvalid-all-objects;Used when an invalid (non-string) object occurs in __all__.tE0604sNo name %r in module %rsno-name-in-modules-Used when a name cannot be found in a module.tE0611s0Global variable %r undefined at the module levelsglobal-variable-undefinedsUsed when a variable is defined through the "global" statement but the variable is not defined in the module scope.tW0601s-Using global for %r but no assignment is donesglobal-variable-not-assignedsxUsed when a variable is defined through the "global" statement but no assignment to this variable is done.tW0602sUsing the global statementsglobal-statementsUsed when you use the "global" statement to update a global variable. PyLint just try to discourage this usage. That doesn't mean you can not use it !tW0603s.Using the global statement at the module levelsglobal-at-module-levelsaUsed when you use the "global" statement at the module level since it has no effecttW0604sUnused import %ss unused-imports5Used when an imported module or variable is not used.tW0611sUnused variable %rsunused-variables-Used when a variable is defined but not used.tW0612sUnused argument %rsunused-arguments4Used when a function or method argument is not used.tW0613s%Unused import %s from wildcard importsunused-wildcard-importsiUsed when an imported module or variable is not used from a 'from X import *' style import.tW0614s-Redefining name %r from outer scope (line %s)sredefined-outer-namesQUsed when a variable's name hide a name defined in the outer scope.tW0621sRedefining built-in %rsredefined-builtins5Used when a variable or function override a built-in.tW0622s/Redefining name %r from %s in exception handlersredefine-in-handlersWUsed when an exception handler assigns the exception to an existing nametW0623s)Using possibly undefined loop variable %rsundefined-loop-variablesUsed when an loop variable (i.e. defined by a for loop or a list comprehension or a generator expression) is used outside the loop.tW0631sjPossible unbalanced tuple unpacking with sequence%s: left side has %d label(s), right side has %d value(s)sunbalanced-tuple-unpackings>Used when there is an unbalanced tuple unpacking in assignmenttW0632s%Attempting to unpack a non-sequence%ssunpacking-non-sequencesKUsed when something which is not a sequence is used in an unpack assignmenttW0633s Cell variable %s defined in loopscell-var-from-loopsA variable used in a closure is defined in a loop. This will result in all closures using the same value for the closed-over variable.tW0640tVariablesCheckercBseZdZeZdZeZdZdidd6dd6dd 6d d 6fd id d6dd6dd 6dd 6fdid=d6dd6dd 6dd 6ffZ d>dZ dZ e ddddddZdZdZdZd Zd!Zd"Zd#Zd$Zd%Zd&Zd'Zd(Ze d)d*d+d,dd-Zd.Zd/Ze d0d1Zd2Zd3Z e ej!d4Z"e d5d6Z#e d5d7Z$e d8d9d:Z%d;Z&d<Z'RS(?schecks for * unused variables / imports * undefined variables * redefinition of variable from builtins or from an outer scope * use of variable before assignment * __all__ consistency t variablesis init-importitdefaulttynttypestmetavarsBTells whether we should check for unused import in __init__ files.thelpsdummy-variables-rgxs_$|dummytregexpssUA regular expression matching the name of dummy variables (i.e. expectedly not used).sadditional-builtinstcsvssList of additional names supposed to be defined in builtins. Remember that you should avoid to define new builtins when possible.cCs&tj||d|_d|_dS(N(Rt__init__R"t _to_consumet_checking_mod_attr(tselftlinter((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyRNs c Cst|jidfg|_xZ|jjD]I\}}t|r.t|d r.|jdd|d|dq.q.WdS(snvisit module : update consumption analysis variable checks globals doesn't overrides builtins tmoduleisredefined-builtintargsR,N(RtlocalsROt iteritemsR Rt add_message(RQR,R%tstmts((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_moduless unused-importsunused-wildcard-importsredefined-builtinsundefined-all-variablesinvalid-all-objectc Cst|jdkst|jjd}d|jkr|jdj}|tjk rxt |ddD]u}y|j j}Wntj k rqtnXt |tj  st |jt r|jdd|jd|qtn|j}||kr||=qtn||jkrt|jsK|jdd|d|qtjj|jd}tjj|d kr|jd |}yt|jd Wqtk r|jdd|d|qtk r}qXqqtqtWqn|jj r |jr d Sx|jD]\}} t d | DrBqn| d} t | tj!rz|jd d|d| qt | tj"r| j#dkr| j$dddkr|jdd|d| q|jd d|d| qqW|`d S(s$leave module: check globals iit__all__teltssinvalid-all-objectRTR,sundefined-all-variableRNt.Ncss9|]/}t|tjo0t|jtjVqdS(N(RRtAssNametass_typet AugAssign(RR((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pys ss unused-importt __future__t*sunused-wildcard-import((%tlenROtAssertionErrortpopRUtigetattrRRtYEStgetattrtinfertInferenceErrorRtConsttvaluet basestringRWR+tpackagetostpathtsplitexttfiletbasenameR%Rtsplitt ImportErrort SyntaxErrortconfigt init_importRVRtImporttFromtmodnametnames( RQR,t not_consumedtassignedtelttelt_nameRrR%texcRXR((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt leave_modules`           ! cCs&|jjt|jidfdS(s:visit class: update consumption analysis variable tclassN(ROtappendRRU(RQR,((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_class scCs|jjdS(s:leave class: update consumption analysis variable N(RORd(RQt_((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt leave_classscCs&|jjt|jidfdS(s;visit lambda: update consumption analysis variable tlambdaN(RORRRU(RQR,((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_lambdascCs|jjdS(s;leave lambda: update consumption analysis variable N(RORd(RQR((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt leave_lambdascCs&|jjt|jidfdS(s<visit genexpr: update consumption analysis variable t comprehensionN(RORRRU(RQR,((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_genexpr scCs|jjdS(s<leave genexpr: update consumption analysis variable N(RORd(RQR((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt leave_genexpr%scCs&|jjt|jidfdS(s=visit dictcomp: update consumption analysis variable RN(RORRRU(RQR,((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytvisit_dictcomp+scCs|jjdS(s=leave dictcomp: update consumption analysis variable N(RORd(RQR((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytleave_dictcomp0scCs&|jjt|jidfdS(s<visit setcomp: update consumption analysis variable RN(RORRRU(RQR,((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_setcomp6scCs|jjdS(s<leave setcomp: update consumption analysis variable N(RORd(RQR((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt leave_setcomp;sc Cs$|jjt|jidf|jjdpC|jjdsJdS|jj}x|jD]\}}t |rqfn||krt |t j  r||dj }|jj}|j|s|jdd||fd|qqft|rf|jdd|d|qfqfWdS(sNvisit function: update consumption analysis variable and check locals tfunctionsredefined-outer-namesredefined-builtinNiRTR,(RORRRURRtis_message_enabledR)tglobalstitemsRRRtGlobalt fromlinenoRvtdummy_variables_rgxtmatchRWR (RQR,tglobsR%Rtlinet dummy_rgx((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytvisit_functionAs"  % c Cs|jjd}|jjdp4|jjds;dSt|rKdS|j}|jj}|r|jdks|j rdS|j j }t }|j }xS|jD]E\}} |j|rqn| d} t| tjrqn||kr|r|jdkr0||dkr0qn|sQt||j} t}n| dk ru|| j kruqn|jtkr|jd krqqn|jjd s|jjd rqn|jdd |d | q|jdd |d | qWdS(s4leave function: check function's locals are consumedisunused-variablesunused-argumentNt interfacet staticmethodRNt__new__tcb_t_cbRTR,(s__init__s__new__(RORdRRRR t is_methodRtframeRIt is_abstractRvRtFalsetargnamesRVRRRRR'R%tTrueR"Rt startswithtendswithRW( RQR,R|RR$tauthorized_rgxtcalled_overriddenRR%RXRt overridden((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytleave_functionUsD  !      $sglobal-variable-undefinedsglobal-variable-not-assignedsglobal-statementsglobal-at-module-levelcCs|j}t|tjr5|jdd|dS|j}t}x|jD]}y|j|}Wntj k rg}nXxX|D]1}|j dkrqn|j|krPqqW|jdd|d|t }|sqQnxo|D]H}|j dkr'|jdd|d|Pn|j|krPqqW|jdd|d|t }qQW|r|jdd|ndS( s/check names imported exists in the global scopesglobal-at-module-levelR,Nsglobal-variable-not-assignedRTsredefined-builtinsglobal-variable-undefinedsglobal-statement( RRRtModuleRWR)RR{Rgt NotFoundErrorRR"R(RQR,RRStdefault_messageR%t assign_nodestanode((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_globals<      cCs|j}t|tjtjfs+dSt|tjrw|jj|jr|jdd|d|j qn|j}|}xvt|tj s||krPn|j}qW|j|rt|j tj  r|jdd|d|j ndS(Nscell-var-from-loopR,RT( tscopeRRtLambdaR#t ComprehensionRRRWR%Rt statementtReturn(RQR,tassignment_nodet scope_typet node_scopet assign_scopet maybe_for((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt_check_late_binding_closures "   (c Cs|jjdsdSg|j|dD]}t|dr*|^q*}| s|djsl|djr|djj|rg}n |d }xdt|dD]R\}}||jj|rt ||j| rqn|j |qW|}t |dkr|dj }t |tjtjtjfr|j|jk r|jdd|d|qndS(Nsundefined-loop-variableiR^iRTR,(RRRtlookupthasattrt is_statementRRRt enumerateRRRbR^RRRRtGenExprRW(RQR,R%Rtastmtst_astmtstitass((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt _loopvar_names&!  !sredefine-in-handlercCsRxKt|jD]:}t|\}}|r|jdd|d|qqWdS(Nsredefine-in-handlerRTR,(RR%RRW(RQR,R%t clobberingRT((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytvisit_excepthandlerscCs,t|jtjr(|j|ndS(N(RR^RR_t visit_name(RQR,((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_assnamescCs|j|dS(N(R(RQR,((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_delnamescs|j}|jdkr>|jjjd s:tdS|j|j}t |szt |szt ||rt |j d}nt |j d}|j |d}xt|ddD]}|j |\}}} | dkr||kr|dko||dk rqn|kret|d} |j|| | |j|Pny||9st NameErrort Exceptiont BaseExceptionsundefined-variableRTR,Rsused-before-assignment(s NameErrors Exceptions BaseException((RRR"R)RqRRcR%RR R RRbROtrangeRRRR!RRRRt scope_attrsRRRURt get_childrenR RRRtDelNameR]RWRR Rvtadditional_builtins(RQR,RRt start_indextbase_scope_typeRt to_consumetconsumedRtdefnodetdefstmttdefframet maybee0601((R%sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyRst             " sno-name-in-modulecCs{xt|jD]i\}}|jd}y|j|dj}Wntjk r[q nX|j|||dq WdS(s check modules attribute accessesR\iiN(R{Rstinfer_name_moduleRRt ResolveErrort_check_module_attrs(RQR,R%RtpartsRS((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_importTscCs|jjd}t|dd}y#|jj|dd|}Wn.tk r[dStk rw}dG|GHdSX|j|||d}|sdSxE|j D]:\}}|dkrqn|j|||jdqWdS(s check modules attribute accessesR\tleveliNs(Unhandled exception in VariablesChecker:iRa( RzRsRgR"R)t import_moduleRRRR{(RQR,t name_partsRRSRR%R((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_from_s #   sunbalanced-tuple-unpackingsunpacking-non-sequencecCst|jdtjtjfs&dS|jdj}y1x*|jjD]}|j|||qLWWntj k rdSXdS(s^Check unbalanced tuple unpacking for assignments and unpacking non-sequences. iN( RttargetsRtTupletListtiteredRkRht_check_unpackingRi(RQR,RR-((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyt visit_assignss"c Cs/|tjkrdSt|tjtjfr|j}t|t|kr+|jdd|dt||t|t|fq+nt|tj rxdD]2}y|j |PWqtj k rqqXqW|jdd|dt||fn%|jdd|dt||fdS( sS Check for unbalanced tuple unpacking and unpacking non sequences. Nsunbalanced-tuple-unpackingR,RTt__iter__t __getitem__sunpacking-non-sequence(s__iter__s __getitem__( RRfRRRRRbRWR0tInstanceRgR(RQR-R,Rtvaluestmeth((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyRs(      c Cs2t|tjst|x|r|jd}|dkrId}Pny6|j|djj}|tj kr~dSWqtj k r|j dd||j fd|dStj k rdSXqW|r|r|j pd}|j dd|ddj||fdSt|tjr.|SdS(scheck that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it it__dict__sno-name-in-moduleRTR,R\N(RRRRcRdR"RgRhRRfRRWR%Ritjoin(RQR,RSt module_namesR%Rz((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyRs.  " (N((t__name__t __module__t__doc__Rt__implements__R%tMSGStmsgstprioritytoptionsR"RNRYRRRRRRRRRRRRRRRRRRRRtkeysRRRRRR(((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyREsX     !?            / +  $  _  tVariablesChecker3kcBs)eZdZdZdZdZRS(s!Modified variables checker for 3kcCs&|jjt|jidfdS(s=visit dictcomp: update consumption analysis variable RN(RORRRU(RQR,((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytvisit_listcompscCs|jjdS(s=leave dictcomp: update consumption analysis variable N(RORd(RQR((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytleave_listcompscCsx|jtjD]}|jr|j}|jdd}t|jtjrm|j|jj dn|r|j|j j dqqqWt t |j|dS(sG Update consumption analysis variable for metaclasses. iN(tnodes_of_classRtClasst _metaclasst metaclassRORtNameRdR%R"R)tsuperRR(RQR,R$Rt module_locals((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyRs  #(RRRRRR(((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyRs  iicCs|jt|dS(s-required method to auto register this checkerN(tregister_checkerRE(RR((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pytregisters(#RRntsysRRRRRtlogilab.common.modutilsRtpylint.interfacesRtpylint.checkersRtpylint.checkers.utilsRRR R R R R RRRRRRR'R0RRERt version_infoR (((sS/Users/jacob/code/ruby/exercism-analysis/vendor/python/pylint/checkers/variables.pyts   R                       %