o
    g)Q                    @   s  d Z ddlZddlZddlZddlZddlZddlmZ ddlZddlm	Z	 ddl
mZmZ ddlmZ ddlmZ ddlmZmZmZmZ dd	lmZ ed
ZdZedZddiZeddhZdZdZ eh dZ!dd Z"dd Z#edddd Z$dd Z%dd Z&d d! Z'd"d# Z(d$d% Z)d&d' Z*d(d) Z+d*d+ Z,d,d- Z-d.d/ Z.i d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVgifdWdXdYdUdZgifd[d\d]d^Z/e0d_d`Z1G dadb dbZ2G dcdd ddeZ3dedf Z4dS )gz#variables checkers for Python code
    N)	lru_cache)nodes)BaseCheckerutils)is_postponed_evaluation_enabled)	PY39_PLUS)HIGH	INFERENCEINFERENCE_FAILUREIAstroidCheckerget_global_optionz^_{2}[a-z]+_{2}$
__future__z_.*|^ignored_|^unused__py_abcabcztyping.TYPE_CHECKINGTYPE_CHECKINGzbuiltins.rangetyping>5   AnySetDictListTextTypeDequeSizedTupleUnionAnyStrCounterGenericMappingPatternTypeVarBinaryIOCallableClassVarHashableIterableIteratorKeysViewOptionalSequence	Awaitable	Container	Coroutine	FrozenSet	Generator	ItemsView
ByteString
Collection
MutableSet
NamedTuple
Reversible
ValuesViewAbstractSetDefaultDictMappingViewSupportsAbsSupportsIntAsyncIterableAsyncIteratorSupportsBytesSupportsFloatSupportsRoundAsyncGeneratorContextManagerMutableMappingMutableSequenceSupportsComplexAsyncContextManagerc                 C   s\   z|  | j}W n tjy   Y dS w |j|g D ]}t|tjr+|jt	kr+ dS qdS )z9Check if the name is a future import from another module.NT)
do_import_modulemodnameastroidAstroidBuildingExceptionlocalsget
isinstancer   
ImportFromFUTURE)stmtnamemodule
local_node rU   W/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pylint/checkers/variables.py_is_from_future_import   s   rW   c                    s$   t | tjot fdd| jD S )zEReturns True if stmt in inside the else branch for a parent For stmt.c                 3   s"    | ]}|  p| kV  qd S N)	parent_of).0	else_stmtrQ   rU   rV   	<genexpr>   s    
z%in_for_else_branch.<locals>.<genexpr>)rN   r   Foranyorelse)parentrQ   rU   r\   rV   in_for_else_branch   s   rb   i  )maxsizec              	   C   sb   z	t | |}W n ttfy   Y dS w z|| }W n
 ty&   Y dS w t|tjr/|S dS )zget overridden method if anyN)nextlocal_attr_ancestorsStopIterationKeyErrorrN   r   FunctionDef)klassrR   ra   	meth_noderU   rU   rV   overridden_method   s   rk   c                 C   sl   d}|  j}|   j|kr(| j|jkrd|  }|S |jr&d|j }|S |jr4d|j d| }|S )zxreturn extra information to add to the message for unpacking-non-sequence
    and unbalanced-tuple-unpacking errors
      z defined at line z of )rootrR   lineno	as_string)nodeinferredmoreinferred_modulerU   rU   rV   _get_unpacking_extra_info   s   
ru   c                 C   s   d }}|r|j r|j  }|r|j r|j  }t|tjr+t| j tjtjfs*dS ntdd ||fD r8dS g }||fD ]!}|}|r_t|tjtjfsS|	| n|j r\|j  }nn|sDq>|rlt
t|dkrldS |j|jk S )a  Detect that the given frames shares a global
    scope.

    Two frames shares a global scope when neither
    of them are hidden under a function scope, as well
    as any of parent scope of them, until the root scope.
    In this case, depending from something defined later on
    will not work, because it is still undefined.

    Example:
        class A:
            # B has the same global scope as `C`, leading to a NameError.
            class B(C): ...
        class C: ...

    NFc                 s   s$    | ]}t |tjtjf V  qd S rX   )rN   r   ClassDefModule)rZ   frU   rU   rV   r]      s    
z'_detect_global_scope.<locals>.<genexpr>   )ra   scoperN   r   rh   	Argumentsr_   rv   rw   appendlensetro   )rq   framedefframe	def_scoperz   break_scopescurrent_scopeparent_scoperU   rU   rV   _detect_global_scope   s8   




r   c                 C   s   t j }||_| j|ddS )NF)asname)rJ   contextInferenceContext
lookupnameinfer)rq   rR   r   rU   rU   rV   _infer_name_module  s   
r   c           	      C   s   i }|   D ]R\}}tdd |D rq|D ]A}t|tjtjfs"q|jD ]1}d}|d }|dkr4|}nd}||rD|ddkrDd	}|sJ||v rL|}|rV||vrV|||< q%qqt	|  d
d dS )zTry to fix imports with multiple dots, by returning a dictionary
    with the import names expanded. The function unflattens root imports,
    like 'xml' (when we have both 'xml.etree' and 'xml.sax'), to 'xml.etree'
    and 'xml.sax' respectively.
    c                 s   s,    | ]}t |tjot | tjV  qd S rX   )rN   r   
AssignNameassign_type	AugAssignrZ   rQ   rU   rU   rV   r]     s    
z#_fix_dot_imports.<locals>.<genexpr>Nr   *F.Tc                 S   s
   | d j S )Nry   )
fromlineno)arU   rU   rV   <lambda>3  s   
 z"_fix_dot_imports.<locals>.<lambda>)key)
itemsr_   rN   r   rO   Importnames
startswithfindsorted)	not_consumedr   rR   stmtsrQ   importssecond_nameimport_module_namename_matches_dotted_importrU   rU   rV   _fix_dot_imports  s6   
r   c                 C   sZ   | tjtjf}|D ]}|jD ]\}}|r|| kr  dS q|r)|| kr)  dS qqdS )z
    Detect imports in the frame, with the required
    *name*. Such imports can be considered assignments.
    Returns True if an import for the given name was found.
    TN)nodes_of_classr   r   rO   r   )rR   r   r   import_nodeimport_nameimport_aliasrU   rU   rV   _find_frame_imports6  s   r   c                 C   s6   | j D ]\}}|r||v r dS q||v r dS qdS )NTF)r   )rQ   global_namesr   r   rU   rU   rV   _import_name_is_globalI  s   r   c                 C   s   dd | D }t tj|S )Nc                 s   s    | ]}t |jV  qd S rX   )r~   r   r   rU   rU   rV   r]   V  s    z)_flattened_scope_names.<locals>.<genexpr>)r~   	itertoolschainfrom_iterable)iteratorvaluesrU   rU   rV   _flattened_scope_namesU  s   r   c                    s&      tj}t fdd|D S )zN
    Checks if name_node has corresponding assign statement in same scope
    c                 3   s    | ]	}|j  j kV  qd S rX   rR   rZ   r   	name_noderU   rV   r]   _      z$_assigned_locally.<locals>.<genexpr>)rz   r   r   r   r_   )r   assign_stmtsrU   r   rV   _assigned_locallyZ  s   r   c                 C   s(   | j }t|tjsdS |j}| tv S NF)ra   rN   r   Iftestrp   TYPING_TYPE_CHECKS_GUARDS)rq   ra   r   rU   rU   rV   _is_type_checking_importb  s
   r   c                 C   sf   t jt jt jt jf}|jt j|dD ]}t|j	}t
|r0t|dd dkr0| j|jk r0 dS qdS )N)
skip_klassrR   rL   TF)r   rh   rv   r   rO   r   Callr   
safe_inferfuncis_builtin_objectgetattrro   )rQ   rz   
skip_nodescallrr   rU   rU   rV   _has_locals_call_after_nodej  s   r   E0601)z#Using variable %r before assignmentused-before-assignmentz=Used when a local variable is accessed before its assignment.E0602)zUndefined variable %rundefined-variablez,Used when an undefined variable is accessed.E0603)z%Undefined variable name %r in __all__undefined-all-variablez>Used when an undefined variable name is referenced in __all__.E0604)z7Invalid object %r in __all__, must contain only stringsinvalid-all-objectz;Used when an invalid (non-string) object occurs in __all__.E0605)z1Invalid format for __all__, must be tuple or listinvalid-all-formatz(Used when __all__ has an invalid format.E0611)zNo name %r in module %rno-name-in-modulez-Used when a name cannot be found in a module.W0601)z0Global variable %r undefined at the module levelglobal-variable-undefinedzsUsed when a variable is defined through the "global" statement but the variable is not defined in the module scope.W0602)z-Using global for %r but no assignment is doneglobal-variable-not-assignedzjUsed when a variable is defined through the "global" statement but no assignment to this variable is done.W0603)zUsing the global statementglobal-statementzUsed when you use the "global" statement to update a global variable. Pylint just try to discourage this usage. That doesn't mean you cannot use it !W0604)z.Using the global statement at the module levelglobal-at-module-levelzSUsed when you use the "global" statement at the module level since it has no effectW0611)z	Unused %sunused-importz5Used when an imported module or variable is not used.W0612)zUnused variable %runused-variablez-Used when a variable is defined but not used.W0613)zUnused argument %runused-argumentz4Used when a function or method argument is not used.W0614)z%Unused import %s from wildcard importunused-wildcard-importz]Used when an imported module or variable is not used from a `'from X import *'` style import.W0621)z-Redefining name %r from outer scope (line %s)redefined-outer-namezDUsed when a variable's name hides a name defined in the outer scope.W0622)zRedefining built-in %rredefined-builtinz5Used when a variable or function override a built-in.W0631)z)Using possibly undefined loop variable %rundefined-loop-variablezUsed when a loop variable (i.e. defined by a for loop or a list comprehension or a generator expression) is used outside the loop.zjPossible unbalanced tuple unpacking with sequence%s: left side has %d label(s), right side has %d value(s)unbalanced-tuple-unpackingz>Used when there is an unbalanced tuple unpacking in assignment	old_names)E0632zold-unbalanced-tuple-unpackingz%Attempting to unpack a non-sequence%sunpacking-non-sequencezKUsed when something which is not a sequence is used in an unpack assignment)W0633zold-unpacking-non-sequence)z Cell variable %s defined in loopcell-var-from-loopzA 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.)zPossibly unused variable %rpossibly-unused-variablezUsed when a variable is defined but might not be used. The possibility comes from the fact that locals() might be used, which could consume or not the said variable)z"Invalid assignment to %s in methodself-cls-assignmentzKInvalid assignment to self or cls in instance or class method respectively.)W0632E0633W0640W0641W0642ScopeConsumerzto_consume consumed scope_typec                   @   s\   e Zd ZdZdd Zdd Zdd Zedd	 Zed
d Z	edd Z
dd Zdd ZdS )NamesConsumerzZ
    A simple class to handle consumed, to consume and scope type info of node locals
    c                 C   s    t t|ji || _|| _d S rX   )r   copyrL   _atomicrq   )selfrq   
scope_typerU   rU   rV   __init__
  s   
zNamesConsumer.__init__c                 C   s\   dd | j j D }dd | j j D }d|}d|}d| d| d| j j dS )	Nc                 S      g | ]\}}| d | qS z->rU   rZ   kvrU   rU   rV   
<listcomp>      z*NamesConsumer.__repr__.<locals>.<listcomp>c                 S   r   r   rU   r   rU   rU   rV   r    r  z, z
to_consume : z
consumed : z
scope_type : 
)r   
to_consumer   consumedjoinr   )r   to_consumesr  rU   rU   rV   __repr__  s   

zNamesConsumer.__repr__c                 C   s
   t | jS rX   )iterr   r   rU   rU   rV   __iter__  s   
zNamesConsumer.__iter__c                 C      | j jS rX   )r   r  r
  rU   rU   rV   r       zNamesConsumer.to_consumec                 C   r  rX   )r   r  r
  rU   rU   rV   r     r  zNamesConsumer.consumedc                 C   r  rX   )r   r   r
  rU   rU   rV   r   $  r  zNamesConsumer.scope_typec                    s@    fdd| j | D } | j|< |r|| j |< dS | j |= dS )z
        Mark the given nodes as consumed for the name.
        If all of the nodes for the name were consumed, delete the name from
        the to_consume dictionary
        c                    s   g | ]
}|t  vr|qS rU   )r~   rZ   nconsumed_nodesrU   rV   r  .  s    z2NamesConsumer.mark_as_consumed.<locals>.<listcomp>N)r  r  )r   rR   r  
unconsumedrU   r  rV   mark_as_consumed(  s
   
zNamesConsumer.mark_as_consumedc                    s    j } j}| j|}|r*t|tjr*||d jkr*|d jjd }|j |kr*d}|r>t|tjr>|j	 kr>|j
|v r>d}|rI fdd|D }|S )z
        Return a list of the nodes that define `node` from this scope.
        Return None to indicate a special case that needs to be handled by the caller.
        r   Nc                    s.   g | ]}t | tjr|  r|qS rU   )rN   	statementr   ExceptHandlerrY   r  rq   rU   rV   r  Q  s    z5NamesConsumer.get_next_to_consume.<locals>.<listcomp>)rR   ra   r  rM   rN   r   Assigntargetsr^   r	  target)r   rq   rR   parent_nodefound_nodeslhsrU   r  rV   get_next_to_consume6  s.   





z!NamesConsumer.get_next_to_consumeN)__name__
__module____qualname____doc__r   r  r  propertyr  r  r   r  r  rU   rU   rU   rV   r     s    


r   c                   @   sV  e Zd ZdZeZdZeZdZ	dddddd	fd
ddddd	fdddddd	fdddddd	fdddddd	fde
dddd	fdddddd	fd dddd!d	ffZdd#d$Zed%d&ejd'd"fd(d)Zed%d&ejd'd"fd*d+Zd&ejd'd"fd,d-Zed.d/d0d1d2d3d4d&ejd'd"fd5d6Zd&ejd'd"fd7d8Zd9ejd'd"fd:d;Zd&ejd'd"fd<d=Zd9ejd'd"fd>d?Zd&ejd'd"fd@dAZd9ejd'd"fdBdCZd&ejd'd"fdDdEZ d9ejd'd"fdFdGZ!d&ej"d'd"fdHdIZ#d9ej"d'd"fdJdKZ$d&ej%d'd"fdLdMZ&d&ej%d'd"fdNdOZ'e&Z(e'Z)edPdQdRdSd0d&ej*d'd"fdTdUZ+d&ej,d'd"fdVdWZ-d&ej.d'd"fdXdYZ/d&ej0d'd"fdZd[Z1ed\d&ej2d'd"fd]d^Z3ed\d&ej4d'd"fd_d`Z5edadbdcd&ej6d'd"fdddeZ7d&ej8d'd"fdfdgZ9d9ej8d'd"fdhdiZ:d&ej6d'd"fdjdkZ;d&ej<d'd"fdldmZ=d&ej>d'd"fdndoZ?e@jAjBdpdq ZCe@jAjBdrds ZDe@jAjBdtdu ZEeFdvdw ZGeFd&ejHdxejHd'eIfdydzZJeFd{d| ZKd}d~ ZLdd ZMdd ZNdd ZOdd ZPd&ej0d'd"fddZQdd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYd&ejfddZZdd Z[dd Z\dd Z]dd Z^d"S )VariablesCheckerzchecks for
    * unused variables / imports
    * undefined variables
    * redefinition of variable from builtins or from an outer scope
    * use of variable before assignment
    * __all__ consistency
    * self/cls assignment
    	variablesr   zinit-importr   ynz<y_or_n>zBTells whether we should check for unused import in __init__ files.)defaulttypemetavarhelpzdummy-variables-rgxz;_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_regexpz<regexp>zYA regular expression matching the name of dummy variables (i.e. expected to not be used).zadditional-builtinsrU   csvz<comma separated list>zList of additional names supposed to be defined in builtins. Remember that you should avoid defining new builtins when possible.	callbacks)cb__cbz<callbacks>z|List of strings which can identify a callback function by name. A callback name must start or end with one of those strings.zredefining-builtins-modules)z	six.moveszpast.builtinszfuture.builtinsbuiltinsiozQList of qualified module names which can have objects that can redefine builtins.zignored-argument-nameszcArgument names that match this expression will be ignored. Default to name with leading underscore.allow-global-unused-variablesTzGTells whether unused global variables should be treated as a violation.zallowed-redefined-builtinsz(List of names allowed to shadow builtinsNc                 C   s.   t | | d | _d | _g | _g | _d| _d S r   )r   r   _to_consume_checking_mod_attr_loop_variables_type_annotation_names_postponed_evaluation_enabled)r   linterrU   rU   rV   r     s   
zVariablesChecker.__init__r   rq   returnc                    s   dd |j tjD }| jj  fdd|D }|D ] }| jD ]\}}||v r:t||s:| jd||j	f|d  nq q| j
||f d S )Nc                 S      g | ]}|j qS rU   r   r   rU   rU   rV   r        z.VariablesChecker.visit_for.<locals>.<listcomp>c                    s   g | ]	}  |s|qS rU   )match)rZ   var	dummy_rgxrU   rV   r        r   argsrq   )r  r   r   r   configdummy_variables_rgxr4  rb   add_messager   r|   )r   rq   assigned_tovariable	outer_forouter_variablesrU   r=  rV   	visit_for  s    
zVariablesChecker.visit_forc                 C   s   | j   | | d S rX   )r4  pop_store_type_annotation_namesr   rq   rU   rU   rV   	leave_for  s   
zVariablesChecker.leave_forc                 C   sf   t |dg| _t|| _|j D ]\}}t|r0| |d s%|dkr&q| j	d||d d qdS )znvisit module : update consumption analysis variable
        checks globals doesn't overrides builtins
        rS   r   r!  r   r@  N)
r   r2  r   r6  rL   r   r   
is_builtin _should_ignore_redefined_builtinrD  )r   rq   rR   r   rU   rU   rV   visit_module  s   

zVariablesChecker.visit_moduler   r   r   r   r   r   r   c                 C   sh   t | jdks	J | | | j j}d|jv r| || | | | jj	s-|j
r-dS | | dS )zleave module: check globalsry   __all__N)r}   r2  _check_metaclassesrJ  r  rL   
_check_all_check_globalsrB  init_importpackage_check_imports)r   rq   r   rU   rU   rV   leave_module  s   


zVariablesChecker.leave_modulec                 C      | j t|d dS )z1visit class: update consumption analysis variableclassNr2  r|   r   rL  rU   rU   rV   visit_classdef     zVariablesChecker.visit_classdef_c                 C      | j   dS )z1leave class: update consumption analysis variableNr2  rJ  r   r^  rU   rU   rV   leave_classdef     zVariablesChecker.leave_classdefc                 C   rY  )z2visit lambda: update consumption analysis variablelambdaNr[  rL  rU   rU   rV   visit_lambda  r]  zVariablesChecker.visit_lambdac                 C   r_  )z2leave lambda: update consumption analysis variableNr`  ra  rU   rU   rV   leave_lambda  rc  zVariablesChecker.leave_lambdac                 C   rY  )z3visit genexpr: update consumption analysis variablecomprehensionNr[  rL  rU   rU   rV   visit_generatorexp$  r]  z#VariablesChecker.visit_generatorexpc                 C   r_  )z3leave genexpr: update consumption analysis variableNr`  ra  rU   rU   rV   leave_generatorexp(  rc  z#VariablesChecker.leave_generatorexpc                 C   rY  z4visit dictcomp: update consumption analysis variablerg  Nr[  rL  rU   rU   rV   visit_dictcomp-  r]  zVariablesChecker.visit_dictcompc                 C   r_  z4leave dictcomp: update consumption analysis variableNr`  ra  rU   rU   rV   leave_dictcomp1  rc  zVariablesChecker.leave_dictcompc                 C   rY  )z3visit setcomp: update consumption analysis variablerg  Nr[  rL  rU   rU   rV   visit_setcomp6  r]  zVariablesChecker.visit_setcompc                 C   r_  )z3leave setcomp: update consumption analysis variableNr`  ra  rU   rU   rV   leave_setcomp:  rc  zVariablesChecker.leave_setcompc                 C   s   | j t|d | jds| jdsdS | j}| D ]W\}}||v r`t|t	j
s`|| d }t|t	jr@|jtkr@q tdd || D rLq |j}| ||s_| jd||f|d q t|rw| |sw| |sw| jd||d q dS )	zEvisit function: update consumption analysis variable and check localsfunctionr   r   Nr   c                 s   s.    | ]}t |jtjo|jj tv V  qd S rX   )rN   ra   r   r   r   rp   r   )rZ   
definitionrU   rU   rV   r]   T  s    
z5VariablesChecker.visit_functiondef.<locals>.<genexpr>r@  )r2  r|   r   r7  is_message_enabledrn   globalsr   rN   r   GlobalrO   rI   rP   r_   r   _is_name_ignoredrD  r   rN  _allowed_redefined_builtinrO  )r   rq   globsrR   rQ   rq  linerU   rU   rV   visit_functiondef?  sB   





z"VariablesChecker.visit_functiondefc           	      C   s   |  | |jr| |j |jr|jD ]}| | q| j j}| jds6| jds6| jds6dS t	
|r=dS | }|rI| rIdS t|tj}t|tj}| D ]\}}| |||d || q]dS )z4leave function: check function's locals are consumedr   r   r   Nr   )rR  type_comment_returns_store_type_annotation_nodetype_comment_argsr2  rJ  r  r7  rr  r   is_error	is_methodis_abstractr   r   r   rt  Nonlocalr   _check_is_unused)	r   rq   argument_annotationr   r~  r   nonlocal_namesrR   r   rU   rU   rV   leave_functiondefi  s0   





z"VariablesChecker.leave_functiondefr   r   r   r   c           
   	   C   sD  |  }t|tjr| jd|d dS | }d}| j}|jD ]s}z|	|}W n t
jy6   g }Y nw tdd ||dD  }t||sX|rX| jd||d	 d
}q!|D ]-}	t|	tjrr|	j|jv rr| jd||d	  n"|	  |u rz nt|	tjr|	j|u r nqZ|r| jd||d	 d
}q!|r| jd|d dS dS )z/check names imported exists in the global scoper   r  NTc                 s       | ]	}t |tjV  qd S rX   )rN   r   r   )rZ   localrU   rU   rV   r]     s    
z0VariablesChecker.visit_global.<locals>.<genexpr>rU   r   r@  Fr   r   r   )r   rN   r   rw   rD  rn   rz   rL   r   r   rJ   NotFoundErrorr_   rM   r   is_reassigned_after_currentr   rR   special_attributesrh   ra   )
r   rq   r   rS   default_messagelocals_rR   assign_nodesnot_defined_locally_by_importanoderU   rU   rV   visit_global  sR   	





zVariablesChecker.visit_globalc                 C   s"   t | tjr| | d S d S rX   )rN   r   r   r   
visit_namerL  rU   rU   rV   visit_assignname  s   z!VariablesChecker.visit_assignnamec                 C      |  | d S rX   )r  rL  rU   rU   rV   visit_delname     zVariablesChecker.visit_delnamec                 C   s`  |  }|jdu r| jdrJ dS |j}| }t| jd }| j	
d}| j	
d}| j| j}t|ddD ]}	| j|	 }
|
jdkrYt|
j|sX|	|krY| |rYq;|
jdkrnt|jtjrnt|jjtjrnq;|
jdkr{| ||
jr{q;|
jd	krt||
jrq;||
jv rt|
js|
jd
kr| ||	s| | | ||  dS |
|}|du rq;|rt|d }nd}|r| jd||d | | |s|r|dur|  }|  }||u o|!|ot|tjo|j|jk}|rt"|rt|r| j |ur dS | #|||||||||	\}}}|r(q;|rt$|st%&||ds||u oFt|tj'tj(f}|sW|sW|sWt|tj)rt*|t+s~| j,rvt|tj-tj.tj/frv|| j0v s~| jd||d n]|d	kr| j,rt|tj-tj.fs| jd||d nA|d	krt|tjr||j0v rt|jtj/r|j|jkr| jd||d n| jd||d n|
jd	kr| jd||d |
1|| | ||  dS |r$|tj2j3v s&t4|s(|| j5j6v s*|dkrt|tj.r|7 s.t*|t+s,| jd||d dS dS dS dS dS dS dS )z1Check that a name is defined in the current scopeNz.pyry   r   r   r   rZ  rp  rd  rg  r   r@  )	NameErrorrq   rA  	__class__)8r  r   rn   fileendswithrR   rz   r}   r2  r7  rr  r   ranger   is_ancestor_namerq   _ignore_class_scoperN   ra   r   Keywordrv   _defined_in_function_definitionis_default_argumentr  is_func_decorator$_has_homonym_in_upper_function_scope_check_late_binding_closure_loopvar_namer  assign_parentrD  r   rY   is_inside_lambda_is_variable_violationis_defined_beforerJ   are_exclusiveDelNamer   Deletenode_ignores_exceptionr  r6  	AnnAssignrh   r{   rL   r  rw   scope_attrsrN  rB  additional_builtinsr~  )r   rq   rQ   rR   r   start_indexundefined_variable_is_enabled!used_before_assignment_is_enabledbase_scope_typeicurrent_consumerr  defnodedefstmtr   recursive_klass
maybee0601annotation_returnuse_outer_definitiondefined_by_stmtrU   rU   rV   r    sN  




















zVariablesChecker.visit_namer   c              	   C   s   | j s
t|r
dS t|du rdS |jD ]1\}}|d}ztt||d }W n
 tj	y4   Y qw t
|tjs<q| |||dd  qdS ) check modules attribute accessesNTr   r   ry   )_analyse_fallback_blocksr   is_from_fallback_blockis_node_in_guarded_import_blockr   splitrd   r   rJ   ResolveErrorrN   r   rw   _check_module_attrs)r   rq   rR   r^  partsrS   rU   rU   rV   visit_import  s   
zVariablesChecker.visit_importc                 C   s   | j s
t|r
dS t|du rdS |jd}z	||d }W n tjy-   Y dS w | 	|||dd }|s=dS |j
D ]\}}|dkrIq@| 	|||d q@dS )r  NTr   r   ry   r   )r  r   r  r  rI   r  rH   rJ   rK   r  r   )r   rq   
name_partsrS   rR   r^  rU   rU   rV   visit_importfrom  s$   z!VariablesChecker.visit_importfromr   r   r   c                 C   sz   |  | t|jd tjtjfsdS |jd  }zt|j	}|dur/| 
||| W dS W dS  tjy<   Y dS w )zCheck unbalanced tuple unpacking for assignments
        and unpacking non-sequences as well as in case self/cls
        get assigned.
        r   N)_check_self_cls_assignrN   r  r   r   r   iteredr   r   value_check_unpackingrJ   InferenceError)r   rq   r  rr   rU   rU   rV   visit_assign  s   
zVariablesChecker.visit_assignc                 C   rY  rj  r[  rL  rU   rU   rV   visit_listcomp  r]  zVariablesChecker.visit_listcompc                 C   r_  rl  r`  ra  rU   rU   rV   leave_listcomp  rc  zVariablesChecker.leave_listcompc                 C   r  rX   rK  rL  rU   rU   rV   leave_assign  r  zVariablesChecker.leave_assignc                 C   r  rX   r  rL  rU   rU   rV   
leave_with	  r  zVariablesChecker.leave_withc                 C   s   |j D ]}| | qd S rX   )r|  r{  )r   rq   
annotationrU   rU   rV   visit_arguments  s   
z VariablesChecker.visit_argumentsc                 C      t | dddS )Nzanalyse-fallback-blocksFr&  r   r
  rU   rU   rV   r       z)VariablesChecker._analyse_fallback_blocksc                 C   s   t | dg dS )Nzignored-modulesr  r   r
  rU   rU   rV   _ignored_modules  r  z!VariablesChecker._ignored_modulesc                 C   r  )Nr1  Tr  r   r
  rU   rU   rV   _allow_global_unused_variables  r  z/VariablesChecker._allow_global_unused_variablesc                 C   s   d}t |tjrI|  |u rI| |jjv p+| |jjv p+| |jjv p+| |jju p+| |jj	u pH|j
| pH|jo:|j
| pH|joH| |ju pH|j
| }|S r   )rN   r   rh   r  rA  annotationsposonlyargs_annotationskwonlyargs_annotationsvarargannotationkwargannotationrY   
decoratorsreturns)rq   r   %in_annotation_or_default_or_decoratorrU   rU   rV   r    s&   





z0VariablesChecker._defined_in_function_definitionr   c                 C   s   | }| j }|durD||u rdS t|tjr||jurdS t|tjr)||jur)dS t|tjr;|jr9||jd u s;dS |}|j }|dus	dS )zreturn True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frameNFTr   )	ra   rN   r   LambdarA  Comprehensionr	  ComprehensionScope
generators)rq   r   childra   rU   rU   rV    _in_lambda_or_comprehension_body2  s$   z1VariablesChecker._in_lambda_or_comprehension_bodyc	                    s|  d}	d}
d}||urt | ||}	nR|jd u r% |jv s"t d r$d}	n>t|tjp2t|  tj	o6t
| }|sP|  d rPd}	||koNt|tj }n |jv rct fdd| D  }	|dkrt|tjr |jv rt|tjo| |jv o|j  d j|jk  }	n.t|tjrt|tjr| |ju r||jrd }	}
|	r|j|jv r|j  d j|jk rd}	t| jtjr|j|jk}	n|rd}	n|	o|j|jk}	|	ro|j|jkrot|tjr||u r|| r||urd}	nyt|tjtjtjtjtjfr!t|jtjr!||u r!|| r!||u r!d}	nNt|tj ro||u ro||ro||u ro|j| jkrF|j!| j!k sm|j| jk smt"so|j| jkrot|tjtjtjtjfrot|jtj#rod}	t|tj$tj%fr|j}t|tj&r|j'( t)v r|| }d}|j*D ]}t|tjrt fdd|j+D }|r nq|s|sd}	|	|
|fS )	NTFry   c                 3   s&    | ]}t |tjo |jv V  qd S rX   )rN   r   r  r   rZ   r  r   rU   rV   r]   x  s
    
z:VariablesChecker._is_variable_violation.<locals>.<genexpr>rd  r   c                 3       | ]}|j  kV  qd S rX   r   rZ   r  r   rU   rV   r]         

),r   ra   r  rJ   builtin_lookuprN   r   rh   r   r  r   rn   lookupr  rL   r_   get_childrenrv   r{   defaultsr   r  rY   rR   ro   r  r  r   ExprReturnr  IfExp	NamedExpr
col_offsetr   	JoinedStrr   rO   r   r   rp   r   r`   r  )rq   rR   r  rQ   r  r   r   r  r  r  r  r  forbid_lookupdefstmt_parentused_in_branchdefined_in_or_elserq  rU   r   rV   r  L  s   















z'VariablesChecker._is_variable_violationc                 C   sn   |j }|  }| ||}t||}|s|r |j j}n|j}t|t	j
s+|o5| || o5||v  S )a  
        Return True if the node is in a local class scope, as an assignment.

        :param node: Node considered
        :type node: astroid.Node
        :return: True if the node is in a local class scope, as an assignment. False otherwise.
        :rtype: bool
        )rR   r  rz   r  r   r  ra   rL   rN   r   rv   r  )r   rq   rR   r   r  in_ancestor_listframe_localsrU   rU   rV   r    s   z$VariablesChecker._ignore_class_scopec                    s  | j dsd S dd ||d D }|  t tjr+t fdd|D r+d S |r@|d js7|d j	rC|d 
 |rCg }n|d d }t|dd  D ]\}}|| 
 |rht|| 
 |shqQ|| qQ|}t|dkrxd S |d  }t|tjtjtjfr|
 |
 usd S t|tjs| jd||d d S z	t|j }W n tjy   | jd||d Y d S w t|tjr| tkrd S tjtjtjtj tj!j"f}	t||	s| jd||d d S t#|d	t#|d
g }
|
s| jd||d d S d S )Nr   c                 S   s   g | ]	}t |d r|qS )r   )hasattrr   rU   rU   rV   r  :  r?  z2VariablesChecker._loopvar_name.<locals>.<listcomp>ry   c                 3   s    | ]
}|   V  qd S rX   )r  rY   )rZ   asmtrz   rU   rV   r]   @  s    
z1VariablesChecker._loopvar_name.<locals>.<genexpr>r   r@  eltsr   )$r7  rr  r  rz   rN   r   rh   r_   is_statementra   r  rY   	enumeraterb   r|   r}   r   r^   r  GeneratorExprD  rd   r	  r   rJ   r  InstanceqnameBUILTIN_RANGEr   r   r   r   objectsr/   r   )r   rq   rR   astmts_astmtsr  rQ   assignrr   	sequenceselementsrU   r  rV   r  6  sr   


zVariablesChecker._loopvar_namec                    sX  |  | rd S t|tjr( dkr(t|jd dkr(t|jd d tjr(d S t|tjtjtj	fr<|r<t
||r<d S tt| dd |jjD } |v r[|  ||| d S |jrnt|jtjtjfrn |v rnd S d  }}t|tjtj	frt|jdkrt fdd|jD d }	n|jd }	|	r|	\}}|p| t|| rd}
nKt|tjr|d ur| d	| }nd
  }| jd||d d S t|tj	r|d ur| d|j d| }n  d|j }| jd||d d S d}
t|tjr|jrd S t|rd S t|jtjr"t fdd|j tj!D r"d S | j|
 |d d S )Nr  ry   r   c                 S   r9  rU   r   )rZ   argrU   rU   rV   r    r:  z5VariablesChecker._check_is_unused.<locals>.<listcomp>c                 3   s    | ]	} |v r|V  qd S rX   rU   )rZ   r   r   rU   rV   r]     r   z4VariablesChecker._check_is_unused.<locals>.<genexpr>r    imported as import r   r@   imported from  as r   c                 3   r  rX   r   r  r   rU   rV   r]     r  )"ru  rN   r   rh   r}   rL   rv   rt  r   rO   r   listr   r   argnamesrA  
kwonlyargs_check_unused_argumentsra   r  r  r   rd   r   rz   rD  rI   r  r   is_overload_stubr  r_   r   Name)r   rR   rq   rQ   r   r  r  r  r   import_namesmessage_namemsgrU   r   rV   r    sl   



z!VariablesChecker._check_is_unusedc                 C   sJ   | j j}t|tjrt|jtjst|tjr| j j}n|}|o$||S rX   )	rB  rC  rN   r   r   ra   r{   ignored_argument_namesr;  )r   rQ   rR   authorized_rgxregexrU   rU   rV   ru    s   


z!VariablesChecker._is_name_ignoredc           	         s      } j }|rt|tjrt|rtnt	}nt
}|rK jdkr,||d kr,d S t| j}|d ur>|| v r>d S  jtjv rK jdvrKd S t fdd| jjD rZd S t rad S t rhd S t|rod S | jd|||d d S )Nstaticmethodr   )r   __new__c                 3   s(    | ]} j |p j |V  qd S rX   )rR   r   r  )rZ   cbr  rU   rV   r]     s
    
z;VariablesChecker._check_unused_arguments.<locals>.<genexpr>r   )rA  rq   
confidence)r~  ra   r   rN   r   rv   r   has_known_basesr	   r
   r   r'  rk   rR   r  	PYMETHODSr_   rB  r,  (is_registered_in_singledispatch_functionr  is_protocol_classrD  )	r   rR   rq   rQ   r  r~  ri   r  
overriddenrU   r  rV   r    s2   



z(VariablesChecker._check_unused_argumentsc                 C   s  | j dsdS | }t||r|j }t|tjtj	fr&|j
|jv r(dS ||j
\}}|r7||s9dS t|rI| jd||j
d dS |d }|}|rht|tjsh||u r]dS |j}|rht|tjrW|r||rt|st| tjs| jd||j
d dS dS dS dS dS )a:  Check whether node is a cell var that is assigned within a containing loop.

        Special cases where we don't care about the error:
        1. When the node's function is immediately called, e.g. (lambda: i)()
        2. When the node's function is returned from within the loop, e.g. return lambda: i
        r   Nr  r   )r7  rr  r   r   r  ra   rN   r   r  rh   rR   rL   r  rY   is_comprehensionrD  r^   is_being_calledr  r  )r   rq   
node_scopeassign_scoper   assignment_node	maybe_forrU   rU   rV   r    s>   

z,VariablesChecker._check_late_binding_closurec                 C   s   t |tjsdS |j| jjv S r   )rN   r   rO   rI   rB  redefining_builtins_modules)r   rQ   rU   rU   rV   rO  <  s   z1VariablesChecker._should_ignore_redefined_builtinc                 C   s   || j jv S rX   )rB  allowed_redefined_builtins)r   rR   rU   rU   rV   rv  A  s   z+VariablesChecker._allowed_redefined_builtinc                 C   s:   | j |d dd D ]}|jdkr|j|jv r dS q
dS )a  
        Return True if there is a node with the same name in the to_consume dict of an upper scope
        and if that scope is a function

        :param node: node to check for
        :type node: astroid.Node
        :param index: index of the current consumer inside self._to_consume
        :type index: int
        :return: True if there is a node with the same name in the to_consume dict of an upper scope
                 and if that scope is a function
        :rtype: bool
        ry   Nr   rp  TF)r2  r   rR   r  )r   rq   index	_consumerrU   rU   rV   r  D  s
   z5VariablesChecker._has_homonym_in_upper_function_scopec                 C   s   t |tjr| j|j dS t |tjr| |j dS t |tj	s%dS t |j
tjrCt |j
jtjrC|j
jjtkrC| jt dS | jdd |tjD  dS )z>Given a type annotation, store all the name nodes it refers toNc                 s   s    | ]}|j V  qd S rX   r   )rZ   r  rU   rU   rV   r]   k  s    
z?VariablesChecker._store_type_annotation_node.<locals>.<genexpr>)rN   r   r  r5  r|   rR   	Attributer{  expr	Subscriptr  TYPING_MODULEextendr   )r   type_annotationrU   rU   rV   r{  V  s$   
z,VariablesChecker._store_type_annotation_nodec                 C   s   |j }|sd S | |j  d S rX   )r4  r{  )r   rq   r4  rU   rU   rV   rK  o  s   z-VariablesChecker._store_type_annotation_namesc                    s   dd |j D  | }t fdd|jD }|r!| j }t|tjr1| r1d|	 vs3dS |
 }|s;dS |d }dd |j D }||v rV| jd	||fd
 dS dS )z&Check that self/cls don't get assignedc                 S   s   h | ]}t |tjr|jqS rU   rN   r   r   rR   r  rU   rU   rV   	<setcomp>w  s    
z:VariablesChecker._check_self_cls_assign.<locals>.<setcomp>c                 3   s.    | ]}t |tjr t|j@ r|V  qd S rX   )rN   r   r  r~   r   r  assign_namesrU   rV   r]   }  s    

z:VariablesChecker._check_self_cls_assign.<locals>.<genexpr>zbuiltins.staticmethodNr   c                 s   s"    | ]}t |tjr|jV  qd S rX   r5  r  rU   rU   rV   r]     s    

r   r  )r  rz   r_   bodyra   rN   r   rh   r~  decoratornamesr  rD  )r   rq   rz   nonlocals_with_same_nameargument_namesself_cls_nametarget_assign_namesrU   r7  rV   r  u  s2   
z'VariablesChecker._check_self_cls_assignc                 C   s   t |rdS t |rdS |tju rdS t|jtjr-t|j	tj
r-|j	j|jjkr-dS t|tjtjfrc| }t|t|kratdd |D rMdS | jd|t||t|t|fd dS dS t |sv| jd|t||fd dS dS )zRCheck for unbalanced tuple unpacking
        and unpacking non sequences.
        Nc                 s   r  rX   )rN   r   Starredr  rU   rU   rV   r]     r   z4VariablesChecker._check_unpacking.<locals>.<genexpr>r   r  r   )r   is_inside_abstract_classr%  rJ   UninferablerN   ra   r   r{   r  r  rR   varargr   r   r  r}   r_   rD  ru   is_iterable)r   rr   rq   r  r   rU   rU   rV   r    sB   






z!VariablesChecker._check_unpackingc                 C   s   |rO| d}|dkrd}nAzt||d  }|tju r"W dS W n) tjyB   |j| jv r4Y dS | j	d||jf|d Y dS  tj
yL   Y dS w |s|rg|rV|jnd}| j	d|d||fd dS t|tjro|S dS )zcheck that module_names (list of string) are accessible through the
        given module
        if the latest access name corresponds to a module, return it
        r   __dict__Nr   r@  r   r  )rJ  rd   r   r   rJ   rA  r  rR   r  rD  r  r  rN   r   rw   )r   rq   rS   module_namesrR   rI   rU   rU   rV   r    s<   

z$VariablesChecker._check_module_attrsc           
   	   C   s  t |d}|tju rd S | dvr&|j|j}}| jd|||d d S t|ddD ]}zt |	 }W n
 tj
y@   Y q,w |tju rGq,|jsKq,t|tjrWt|jtsb| jd| |d q,|j}||v rm||= q,||jvr|js| jd	|f|d q,tj|jd
 }tj|dkr|jd | }	ztj|	d W q, ty   | jd	|f|d Y q, ty   Y q,w q,d S )NrQ  )zbuiltins.listzbuiltins.tupler   )rx  r  rq   r  rU   r   r@  r   r   r   r   )rd   igetattrrJ   rA  pytypetolinenor  rD  r   r   r  ra   rN   r   Constr  strrp   rL   rV  ospathsplitextr  basenamerR   modutilsfile_from_modpathr  ImportErrorSyntaxError)
r   rq   r   assignedrx  coleltelt_namerN  rR   rU   rU   rV   rS    sZ   



zVariablesChecker._check_allc                 C   s<   | j rd S | D ]\}}|D ]}| jd|f|d qq	d S )Nr   r@  )r  r   rD  )r   r   rR   node_lstrq   rU   rU   rV   rT    s   zVariablesChecker._check_globalsc                 C   s  t |}t }|D ]\}}|jD ]}|d  }}|dkr|}|d }	||v r'q|||	fvr.q|| || jv p<|	| jv }
t|tjsLt|tjr~|j	s~t|tjrXt
|rXq|
r[q|	dkr`q|	d u rjd| }n| d|	 }t|s}| jd||d qt|tjr|j	tkrt
|rqt||rq|
rq|dkr| jd	||d q|	d u r| d
|j	 }n| d
|j	 d|	 }t|s| jd||d qq	| `d S )Nr   r   ry   r^  r  r  r   r@  r   r  r  )r   r~   r   addr5  rN   r   r   rO   rI   SPECIAL_OBJsearchr   rD  rP   rW   r2  )r   r   local_namescheckedrR   rQ   r   	real_nameimported_nameas_nameis_type_annotation_importr  rU   rU   rV   rW    sh   





=zVariablesChecker._check_importsc                 C   sN   g }|  D ]}t|tjr|| || q|D ]
\}}||d qdS )z,Update consumption analysis for metaclasses.N)r  rN   r   rv   r3  _check_classdef_metaclassesrJ  )r   rq   r  
child_nodescope_localsrR   rU   rU   rV   rR  ^  s   z#VariablesChecker._check_metaclassesc           
      C   s"  |j sg S g }| }d }t|j tjr|j j}n)t|j tjr;|j jr;|j j}t|tjs7|j}t|tjr.|j}n|rB| j}d }t	
||}|rj| jd d d D ]\}}	}	|
|}|ri|||f  nqT|d u r|s|tjjv st|s|| jjv s||jv s| jd||fd |S )Nr   r   r  )
_metaclass	metaclassrN   r   r  rR   r/  r0  rn   METACLASS_NAME_TRANSFORMSrM   r2  r|   rw   r  r   rN  rB  r  rL   rD  )
r   ri   r  r  re  rR   attrfoundrc  r^  rU   rU   rV   ra  k  sD   



z,VariablesChecker._check_classdef_metaclassesrX   )_r  r  r   r!  r   __implements__rR   MSGSmsgspriorityIGNORED_ARGUMENT_NAMESoptionsr   r   check_messagesr   r^   rI  rM  rw   rP  rX  rv   r\  rb  r  re  rf  r  rh  ri  DictComprk  rm  SetComprn  ro  rh   ry  r  visit_asyncfunctiondefleave_asyncfunctiondefrt  r  r   r  r  r  r  r  r   r  rO   r  r  r  ListCompr  r  r  Withr  r{   r  rJ   r  cachedpropertyr  r  r  r  r  NodeNGboolr  r  r  r  r  ru  r  r  rO  rv  r  r{  rK  r  r  r  rS  rT  rW  rR  ra  rU   rU   rU   rV   r#  \  s8   	

Y
	* 2 m




 <.PN+1")!3Cr#  c                 C   s   |  t|  dS )z-required method to auto register this checkerN)register_checkerr#  )r7  rU   rU   rV   register  s   rz  )5r!  collectionsr   r   rK  re	functoolsr   rJ   r   pylint.checkersr   r   pylint.checkers.utilsr   pylint.constantsr   pylint.interfacesr   r	   r
   r   pylint.utilsr   compilerY  rP   rm  rf  	frozensetr   r  r2  TYPING_NAMESrW   rb   rk   ru   r   r   r   r   r   r   r   r   r   rj  
namedtupler   r   r#  rz  rU   rU   rU   rV   <module>   s   6

;
A*%+28=BGMRW_	 W            E