
    eW                        d Z ddlmZ ddlZddlmZ ddlm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 dd
lmZ ddlmZ ddhZ ed      ZdZddZddZddZddZ G d de      Zy)z+Basic Error checker from the basic checker.    )annotationsN)Iterator)Any)nodes)InferenceResult)utils)_BasicChecker)	infer_all)HIGHz_py_abc.ABCMetazabc.ABCMeta)
__module__ztyping.ForwardRefc                    t         j                  t         j                  f}| j                  }t	        ||      r| t        |dg       v r0|} |j                  }|	 |S t	        ||      s | t        |dg       v r0|S )zReturns the loop node that holds the break node in arguments.

    Args:
        break_node (astroid.Break): the break node of interest.

    Returns:
        astroid.For or astroid.While: the loop node holding the break node.
    orelse)r   ForWhileparent
isinstancegetattr)
break_node
loop_nodesr   s      J/usr/lib/python3/dist-packages/pylint/checkers/base/basic_error_checker.py_get_break_loop_noder      s     ))U[[)JF,
g"? 1 
>M ,
g"? 1 M    c                @   t         j                  t         j                  f}t         j                  t         j                  f}| j                  ||      D cg c]	  }|| k7  r| c}t        fd| j                  t         j                  |      D              S c c}w )zReturns true if a loop may end with a break statement.

    Args:
        loop (astroid.For, astroid.While): the loop node inspected.

    Returns:
        bool: True if the loop may end with a break statement, False otherwise.
    
skip_klassc              3  <   K   | ]  }t        |      vr|  y wN)r   ).0_nodeinner_loop_nodess     r   	<genexpr>z$_loop_exits_early.<locals>.<genexpr>A   s'      &.>> 	s   )r   r   r   FunctionDefClassDefnodes_of_classanyBreak)loopr   definition_nodesr   r    s       @r   _loop_exits_earlyr)   1   s     ))U[[)J))5>>: ((@P(Q7D= 	7
  ((AQ(R  7s   Bc                D    t        t        j                  |             dkD  S )zDetermine if the given `node` has abstract methods.

    The methods should be made abstract by decorating them
    with `abc` decorators.
    r   )lenr   unimplemented_abstract_methodsnodes    r   _has_abstract_methodsr/   H   s     u33D9:Q>>r   c                    | j                   r\| j                   j                  D ]C  }t        |t        j                        st	        |j
                  dd      | j                  k(  sC y y)zReturn True if the object is a method redefined via decorator.

    For example:
        @property
        def x(self): return self._x
        @x.setter
        def x(self, value): self._x = value
    nameNTF)
decoratorsr   r   	Attributer   exprr1   )r.   	decorators     r   redefined_by_decoratorr6   Q   sS     .. 	I9eoo6INNFD9TYYF	 r   c            	         e Zd Zi ddddddddd	d
ddddddddifdddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,ifiZdVd-Z ej                  d.      dWd/       ZdXd0Z ej                  d1d2      dYd3       Z	 ej                  d4      dZd5       Z
 ej                  d6d7d.dd8d9d)      d[d:       ZeZd[d;Zd[d<Z ej                  d=      d\d>       Z ej                  d?      d]d@       Z ej                  d?      d^dA       Z ej                  dBdC      d_dD       Z ej                  dB      d`dE       Z ej                  dF      dadG       Z ej                  dF      dbdH       Z ej                  dI      dcdJ       ZdddKZ ej                  dL      dedM       Z ej                  dN      dfdO       Z	 	 	 	 	 	 dgdPZd]dQZdhdRZ	 	 	 	 	 	 didSZ	 	 	 	 	 	 djdTZyU)kBasicErrorCheckerE0100)z__init__ method is a generatorinit-is-generatorz^Used when the special class method __init__ is turned into a generator by a yield in its body.E0101)zExplicit return in __init__return-in-initzIUsed when the special class method __init__ has an explicit return value.E0102)z%s already defined line %sfunction-redefinedz3Used when a function / class / method is redefined.E0103)z%r not properly in loopnot-in-loopz=Used when break or continue keywords are used outside a loop.E0104)zReturn outside functionreturn-outside-functionzEUsed when a "return" statement is found outside a function or method.E0105)zYield outside functionyield-outside-functionzDUsed when a "yield" statement is found outside a function or method.E0106z%Return with argument inside generatorzreturn-arg-in-generatorzUsed when a "return" statement with an argument is found in a generator function or method (e.g. with some "yield" statements).
maxversion)   rG   E0107)z#Use of the non-existent %s operatornonexistent-operatorzzUsed when you attempt to use the C-style pre-increment or pre-decrement operator -- and ++, which doesn't exist in Python.E0108)z1Duplicate argument name %s in function definitionduplicate-argument-namezCDuplicate argument names in function definitions are syntax errors.E0110)z4Abstract class %r with abstract methods instantiatedabstract-class-instantiatedzeUsed when an abstract class with `abc.ABCMeta` as metaclass has abstract methods and is instantiated.W0120)zcElse clause on loop without a break statement, remove the else and de-indent all the code inside ituseless-else-on-loopzLoops should only have an else clause if they can exit early with a break statement, otherwise the statements under else should be on the same scope as the loop itself.E0112)z.More than one starred expression in assignmenttoo-many-star-expressionszhEmitted when there are more than one starred expressions (`*x`) in an assignment. This is a SyntaxError.E0113)z4Starred assignment target must be in a list or tupleinvalid-star-assignment-targetzFEmitted when a star expression is used as a starred assignment target.E0114)z4Can use starred expression only in assignment targetstar-needs-assignment-targetzCEmitted when a star expression is not used in an assignment target.E0115)zName %r is nonlocal and globalnonlocal-and-globalz0Emitted when a name is both nonlocal and global.E0116)z0'continue' not supported inside 'finally' clausecontinue-in-finallyz]Emitted when the `continue` keyword is found inside a finally clause, which is a SyntaxError.E0117)z&nonlocal name %s found without bindingnonlocal-without-bindingz^Emitted when a nonlocal variable does not have an attached name somewhere in the parent scopesE0118z+Name %r is used prior to global declarationused-prior-global-declarationzcEmitted when a name is used prior a global declaration, which results in an error since Python 3.6.
minversion)rG      c                X    | j                   j                  j                  }|dk\  | _        y )N)rG      )linterconfig
py_version
_py38_plus)selfrd   s     r   openzBasicErrorChecker.open   s#    [[''22
$.r   r>   c                (    | j                  d|       y )Nclass)_check_redefinitionrf   r.   s     r   visit_classdefz BasicErrorChecker.visit_classdef   s      $/r   c                    d}|j                         D ]O  }t        |t        j                        r| j	                  |      c S t        |t        j
                        sK|dz  }Q |dkD  S )Nr      )iteredr   r   Tuple_too_many_starred_for_tupleStarred)rf   assign_tuplestarred_countelems       r   rq   z-BasicErrorChecker._too_many_starred_for_tuple   sc     '') 	#D$,77==$."		#
 q  r   rQ   rS   c                   |j                   d   }t        |j                   d   t        j                        r| j	                  d|       t        |t        j
                        sy | j                  |      r| j	                  d|       y y )Nr   rS   r-   rQ   )targetsr   r   rr   add_messagerp   rq   )rf   r.   assign_targets      r   visit_assignzBasicErrorChecker.visit_assign   ss    
 Qdll1ou}}5=DI-5++M:8tD ;r   rU   c                   t        |j                  t        j                        ryt        |j                  t        j                  t        j
                  t        j                  t        j                  f      ry|j                         }t        |t        j                        sy|j                  |u s|j                  j                  |      r| j                  d|       yy)z@Check that a Starred expression is used in an assignment target.NrU   r-   )r   r   r   CallListrp   SetDict	statementAssignvalue	parent_ofrx   )rf   r.   stmts      r   visit_starredzBasicErrorChecker.visit_starred   s     dkk5::. dkkEJJUYY

#ST~~$-::!5!5d!;;$G "<r   r:   r<   rK   rW   c                   | j                  |       | j                  |       t        |      s=t        j                  |      s(| j                  |j                         xr dxs d|       |j                  t        j                  t        j                  t        j                  f      }|j                         rq|j                  dk(  rb|j                         r| j                  d|       n>|D cg c]  }|j                   }}t!        d |D              r| j                  d|       i }t#        d |j$                  j$                  |j$                  j&                  g      }t(        j*                  j-                  |      D ]D  }|j                  |v r%| j                  d	||j                  ft.        
       6|||j                  <   F y c c}w )Nmethodfunctionr   __init__r:   r-   c              3  L   K   | ]  }t        j                  |      r|  y wr   )r   is_none)r   vs     r   r!   z6BasicErrorChecker.visit_functiondef.<locals>.<genexpr>  s     AQa0@qA   $$r<   rK   )r.   args
confidence)_check_nonlocal_and_global_check_name_used_prior_globalr6   r   (is_registered_in_singledispatch_functionrj   	is_methodr$   r   Returnr"   r#   r1   is_generatorrx   r   r%   filterr   
kwonlyargs	itertoolschainfrom_iterabler   )rf   r.   returnsrvaluesarg_clusters	argumentsargs           r   visit_functiondefz#BasicErrorChecker.visit_functiondef  s    	''-**40%
@@F$$T^^%5%B(%PjRVW%%LLe&7&7%H & 
 >>		Z 7  "  !44 @+23a!''33A&AA$$%5D$A#)$AUAU0V#W	??00; 		-Cxx<'  -((#	 !  *-SXX&		- 4s   1G
c                   |j                  t        j                        D ci c](  }|j                  D ]  }|j	                         |u r|| * }}}|sy |j                  t        j
                        D ]i  }|j	                         |ur|j                  }|j                  |      }|s6|j                  }|sE||j                  kD  sU| j                  d||f       k y c c}}w )Nr]   r.   r   )
r$   r   GlobalnamesscopeNamer1   get
fromlinenorx   )rf   r.   childr1   scope_globals	node_namecorresponding_globalglobal_linenos           r   r   z/BasicErrorChecker._check_name_used_prior_global.  s     ,,U\\:

 {{}$ %K

 
 ,,UZZ8 	I ,>>D#0#4#4T#: '0;;M1E1E!E  3)4' ! 	
s   -C"c                   dfdt         j                  j                  }t         |fdj	                  t
        j                        D                    }|syt         |fdj	                  t
        j                        D                    }|j                  |      D ]  }| j                  d|f        y)z.Check that a name is both nonlocal and global.c                (    | j                         u S r   )r   )currentr.   s    r   
same_scopez@BasicErrorChecker._check_nonlocal_and_global.<locals>.same_scopeK  s    ==?d**r   c              3  F   K   | ]  } |      r|j                     y wr   r   r   r   r   s     r   r!   z?BasicErrorChecker._check_nonlocal_and_global.<locals>.<genexpr>P  %      e$    !Nc              3  F   K   | ]  } |      r|j                     y wr   r   r   s     r   r!   z?BasicErrorChecker._check_nonlocal_and_global.<locals>.<genexpr>[  r   r   rW   r   r.   )r   znodes.Global | nodes.Nonlocalreturnbool)
r   r   r   setr$   r   Nonlocalr   intersectionrx   )rf   r.   	from_iter	nonlocalsglobal_varsr1   r   s    `    @r   r   z,BasicErrorChecker._check_nonlocal_and_globalH  s    	+ OO11	 !00@ 
	  !00> 
 **;7 	MD2$tL	Mr   rB   c                |    t        |j                         t        j                        s| j	                  d|       y y )NrB   r-   )r   framer   r"   rx   rk   s     r   visit_returnzBasicErrorChecker.visit_returnd  s1    $**,(9(9:6TB ;r   rD   c                &    | j                  |       y r   _check_yield_outside_funcrk   s     r   visit_yieldzBasicErrorChecker.visit_yieldi      &&t,r   c                &    | j                  |       y r   r   rk   s     r   visit_yieldfromz!BasicErrorChecker.visit_yieldfromm  r   r   r@   rY   c                (    | j                  |d       y )Ncontinue_check_in_looprk   s     r   visit_continuez BasicErrorChecker.visit_continueq  s    D*-r   c                (    | j                  |d       y )Nbreakr   rk   s     r   visit_breakzBasicErrorChecker.visit_breaku  s    D'*r   rO   c                &    | j                  |       y r   _check_else_on_looprk   s     r   	visit_forzBasicErrorChecker.visit_fory        &r   c                &    | j                  |       y r   r   rk   s     r   visit_whilezBasicErrorChecker.visit_while}  r   r   rI   c                D   |j                   dv rt        |j                  t        j                        rm|j                  j                   |j                   k(  rI|j
                  dz   |j                  j
                  k(  r"| j                  d||j                   dz         yyyyy)z2Check use of the non-existent ++ and -- operators.z+-rn   rI      r   N)opr   operandr   UnaryOp
col_offsetrx   rk   s     r   visit_unaryopzBasicErrorChecker.visit_unaryop  s     WW_4<<7DGG+1$(?(??3$TWWq[Q @ , 8 r   c                   |j                         }|j                  |t        |t        j                  t        j
                  f      s| j                  d|f|       y ||j                         u s||j                  vr|j                  j                         }y t        |t        j
                        s| j                  d|f|t               y y )Nr[   r   )r   r.   r   )	r   r   r   r   r#   r"   rx   localsr   )rf   r.   r1   current_scopes       r   _check_nonlocal_without_bindingz1BasicErrorChecker._check_nonlocal_without_binding  s    

"".menne>O>O-PQ  !;4'PT U
 

,M<P<P0P - 4 4 : : < -):):;*$tPT   <r   r[   c                J    |j                   D ]  }| j                  ||        y r   )r   r   )rf   r.   r1   s      r   visit_nonlocalz BasicErrorChecker.visit_nonlocal  s%    JJ 	=D00t<	=r   rM   c                \    t        |j                        D ]  }| j                  ||        y)zRCheck instantiating abstract class with
        abc.ABCMeta as metaclass.
        N)r
   func!_check_inferred_class_is_abstract)rf   r.   inferreds      r   
visit_callzBasicErrorChecker.visit_call  s.    
 "$)), 	CH228TB	Cr   c                   t        |t        j                        sy t        j                  |      }||u ry t        |      }|sy |j                         }|J|j                         D ]6  }|j                         dk(  s| j                  d|j                  f|        y  y |j                         t        v r | j                  d|j                  f|       y y )Nzabc.ABCrM   r   )r   r   r#   r   node_frame_classr/   	metaclass	ancestorsqnamerx   r1   ABC_METACLASSES)rf   r   r.   klassabstract_methodsr   ancestors          r   r   z3BasicErrorChecker._check_inferred_class_is_abstract  s     (ENN3&&t,H
  1:&&(	 %..0 >>#y0$$5X]]<LSW %   ??/-X]]4D4   0r   c                    t        |j                         t        j                  t        j                  f      s| j                  d|       y y )NrD   r-   )r   r   r   r"   Lambdarx   rk   s     r   r   z+BasicErrorChecker._check_yield_outside_func  s;    $**,):):ELL(IJ5DA Kr   c                    |j                   r;t        |      s/| j                  d||j                   d   j                  dz
         yyy)z>Check that any loop with an else clause has a break statement.rO   r   rn   )r.   lineN)r   r)   rx   linenork   s     r   r   z%BasicErrorChecker._check_else_on_loop  sI    ;;06& [[^**Q.    7;r   c                   |j                         D ]  }t        |t        j                  t        j                  f      r||j
                  vr yt        |t        j                  t        j                  f      r ngt        |t        j                        s||j                  v st        |t        j                        s| j                  r| j                  d|        | j                  d||       y)z0Check that a node is inside a for or while loop.NrY   r-   r@   r   )node_ancestorsr   r   r   r   r   r#   r"   Try	finalbodyContinuere   rx   )rf   r.   r   r   s       r   r   z BasicErrorChecker._check_in_loop  s     ))+ 	CF&599ekk":;v}},&5>>53D3D"EF6599-F,,,tU^^4  !6T B	C 	T	Br   c                   |j                   j                         }|j                  |j                     D cg c]>  }t	        |j                   t
        j                        r|j                   j                  s|@ }}t        d |D        |      }||ur?t        j                  ||      s't	        |t
        j                        r|j                  t        v ryt        j                  |      ryt	        |j                   t
        j                        rt	        |j                   j                   t
        j"                        r|j                   j                   j$                  dk(  rpt	        |j                   j                   j&                  t
        j(                        r8|j                   j                   j&                  j                  |j                  k(  ryt	        |j                   j                   t
        j*                        rt	        |j                   j                   j,                  t
        j(                        r|j                   j                   j,                  j                  |j                  k(  r|j                   j                   j.                  d   d   dk(  rot	        |j                   j                   j.                  d   d   t
        j0                        r1|j                   j                   j.                  d   d   j2                  y	 |j5                  |      }|d| D ]N  }t        j6                  |      }	|	st	        |	t        j8                        s6|	j;                         t<        k(  sN y 	 | j@                  jB                  jD                  }
|
r|
jG                  |j                        ry| jI                  d|||jJ                  f       yyyc c}w # t>        $ r Y qw xY w)	z;Check for redefinition of a function / method / class name.c              3  L   K   | ]  }t        j                  |      r|  y wr   )r   is_overload_stub)r   locals     r   r!   z8BasicErrorChecker._check_redefinition.<locals>.<genexpr>  s     SuU5K5KE5RUSr   Nnotr   isrn   r>   r   )&r   r   r   r1   r   r   	AnnAssignsimplenextastroidare_exclusiver#   REDEFINABLE_METHODSr   r  Iftestr   r   r   r   CompareleftopsConstr   index
safe_inferInstancer   TYPING_FORWARD_REF_QNAME
ValueErrorrb   rc   dummy_variables_rgxmatchrx   r   )rf   	redeftyper.   parent_frameiredefinitionsdefined_selfredefinition_indexredefinitionr   r  s              r   rj   z%BasicErrorChecker._check_redefinition  s    {{((*
 "((3
qxx9ahhoo 
 

 SS
 t#G,A,A$,U <8II!44 %%d+ $++uxx0 t{{//?((++u4"4;;#3#3#;#;UZZH((0055B
 t{{//?"4;;#3#3#8#8%**E((--22dii?((,,Q/2d:"4;;#3#3#7#7#:1#=u{{K((,,Q/288@%2%8%8%>" %22E3E$F L$//=H &x1A1AB$NN,0HH #'++"4"4"H"H"':'@'@'K$!8!89  m -V#
`  s   AO
O 	O$#O$N)r   None)r.   nodes.ClassDefr   r   )rs   znodes.Tupler   r   )r.   znodes.Assignr   r   )r.   znodes.Starredr   r   )r.   nodes.FunctionDefr   r   )r.   znodes.Returnr   r   )r.   znodes.Yieldr   r   )r.   znodes.YieldFromr   r   )r.   znodes.Continuer   r   )r.   nodes.Breakr   r   )r.   z	nodes.Forr   r   )r.   znodes.Whiler   r   )r.   znodes.UnaryOpr   r   )r.   nodes.Nonlocalr1   strr   r   )r.   r$  r   r   )r.   
nodes.Callr   r   )r   r   r.   r&  r   r   )r.   nodes.For | nodes.Whiler   r   )r.   znodes.Continue | nodes.Breakr   r%  r   r   )r  r%  r.   znodes.Call | nodes.FunctionDefr   r   )__name__r   __qualname__msgsrg   r   only_required_for_messagesrl   rq   rz   r   r   visit_asyncfunctiondefr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rj    r   r   r8   r8   d   s   i 
i 	 
i 	 
i$ 	 
%i. 	 
/i8 	 
9iB 	3%U6"
CiP 	 
Qi\ 	 
]if 	 
gir 	 
siB 	 
CiN 	 
OiX 	 
Yib 	 
cil 	 
mix 	 
yiD 	9+:6"
EiDV/ &U%%&:;0 <0! &U%%#%E
E
E &U%%&DEH FH" &U%%!!'--B /4M8 &U%%&?@C AC &U%%&>?- @- &U%%&>?- @- &U%%m5JK. L. &U%%m4+ 5+ &U%%&<=' >' &U%%&<=' >' &U%%&<=R >R, &U%%&@A= B= &U%%&CDC EC%'%/9%	%NB
C0C=@C	C*JJ$BJ	Jr   r8   )r   r#  r   znodes.For | nodes.While | None)r'   r'  r   r   )r.   r!  r   r   )r.   r"  r   r   )__doc__
__future__r   r   collections.abcr   typingr   r	  r   astroid.typingr   pylint.checkersr   "pylint.checkers.base.basic_checkerr	   pylint.checkers.utilsr
   pylint.interfacesr   r   	frozensetr  r  r   r)   r/   r6   r8   r-  r   r   <module>r8     sh   
 2 "  $    * ! < + "$m40 . *.?&a ar   