o
    g\                     @   s   d Z ddlZddlZddlmZmZmZ ddlZddlmZm	Z	 ddl
mZmZ ddlmZ dd Zd*d	d
ZdedefddZdZddddddddddddddddZG d d! d!ZG d"d# d#eZG d$d% d%eZG d&d' d'ejZd(d) ZdS )+z,Checks for various exception related errors.    N)AnyListOptional)nodesobjects)checkers
interfaces)utilsc                  C   s"   dd } t t| }dd |D S )Nc                 S   s   t | to	t| tS N)
isinstancetype
issubclassBaseException)obj r   X/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pylint/checkers/exceptions.py	predicate0   s   z&_builtin_exceptions.<locals>.predicatec                 S   s   h | ]\}}|j qS r   )__name__.0_excr   r   r   	<setcomp>4       z&_builtin_exceptions.<locals>.<setcomp>)inspect
getmembersbuiltins)r   membersr   r   r   _builtin_exceptions/   s   r   c                 c   sp    t | tjtjfr#| jD ]}t|}|r |tjur ||fV  qdS | 	|D ]}|tju r0q(| |fV  q(dS )z
    Recursively generate nodes inferred by the given statement.
    If the inferred value is a list or a tuple, recurse on the elements.
    Returns an iterator which yields tuples in the format
    ('original node', 'inferred node').
    N)
r   r   r   Tupleeltsr	   
safe_inferastroidUninferableinfer)stmtcontexteltinferredr   r   r   _annotated_unpack_infer7   s   



r)   bodyreturnc                 C   s    | D ]}t |tjr dS qdS )z:Return true if the given statement node raise an exceptionTF)r   r   Raise)r*   noder   r   r   _is_raisingJ   s
   r.   )r   	Exception)zBad except clauses order (%s)bad-except-orderzUsed when except clauses are not in the correct order (from the more specific to the more generic). If you don't fix the order, some exceptions may not be caught by the most specific handler.)z6Raising %s while only classes or instances are allowedraising-bad-typeztUsed when something which is neither a class, an instance or a string is raised (i.e. a `TypeError` will be raised).)zFException context set to something which is not an exception, nor Nonebad-exception-contextzkUsed when using the syntax "raise ... from ...", where the exception context is not an exception, nor None.)z2The raise statement is not inside an except clausemisplaced-bare-raiseag  Used when a bare raise is not used inside an except clause. This generates an error, since there are no active exceptions to be reraised. An exception to this rule is represented by a bare raise inside a finally clause, which might work, as long as an exception is raised inside the try block, but it is nevertheless a code smell that must not be relied upon.)zBRaising a new style class which doesn't inherit from BaseExceptionraising-non-exceptionzOUsed when a new style class which doesn't inherit from BaseException is raised.)z8NotImplemented raised - should raise NotImplementedErrornotimplemented-raisedzAUsed when NotImplemented is raised instead of NotImplementedError)z>Catching an exception which doesn't inherit from Exception: %scatching-non-exceptionzcUsed when a class which doesn't inherit from Exception is used as an exception in an except clause.)zNo exception type(s) specifiedbare-exceptzDUsed when an except clause doesn't specify exceptions type to catch.)z!Catching too general exception %sbroad-exceptzWUsed when an except catches a too general exception, possibly burying unrelated errors.)z,Catching previously caught exception type %sduplicate-exceptzQUsed when an except catches a type that was already caught by a previous handler.)z%The except handler raises immediatelytry-except-raisezUsed when an except handler uses raise as its first or only operator. This is useless because it raises back the exception immediately. Remove the raise operator or the entire try-except-raise block!)z7Consider explicitly re-raising using the 'from' keywordraise-missing-froma8  Python 3's exception chaining means it shows the traceback of the current exception, but also the original exception. Not using `raise from` makes the traceback inaccurate, because the message implies there is a bug in the exception-handling code itself, which is a separate situation than wrapping an exception.)z;Exception to catch is the result of a binary "%s" operationbinary-op-exceptionz~Used when the exception to catch is of the form "except A or B:".  If intending to catch multiple, rewrite as "except (A, B):")z?Exception arguments suggest string formatting might be intendedraising-format-tuplezUsed when passing multiple arguments to an exception constructor, the first of them a string literal containing what appears to be placeholders intended for formatting)zInvalid exception operation. %swrong-exception-operationzUsed when an operation is done against an exception, but the operation is not valid for the exception in question. Usually emitted when having binary operations between exceptions in except handlers.)E0701E0702E0703E0704E0710E0711E0712W0702W0703W0705W0706W0707W0711W0715W0716c                   @   s4   e Zd ZdZdd Zdd Zdejddfd	d
ZdS )BaseVisitorz/Base class for visitors defined in this module.c                 C   s   || _ || _d S r
   )_checker_node)selfcheckerr-   r   r   r   __init__   s   
zBaseVisitor.__init__c                 C   s:   |j j }t| d| d }|r|| d S | | d S )Nvisit_)	__class__r   lowergetattrvisit_default)rQ   r-   namedispatch_methr   r   r   visit   s
   zBaseVisitor.visitr   r+   Nc                 C   s   dS )z)Default implementation for all the nodes.Nr   rQ   r   r   r   r   rX      s    zBaseVisitor.visit_default)	r   
__module____qualname____doc__rS   r[   r   NodeNGrX   r   r   r   r   rN      s
    rN   c                   @   s8   e Zd ZdZdejddfddZdejddfddZdS )	ExceptionRaiseRefVisitorz4Visit references (anything that is not an AST leaf).r-   r+   Nc                 C   s$   |j dkr| jjd| jd d S d S )NNotImplementedr5   r-   )rY   rO   add_messagerP   rQ   r-   r   r   r   
visit_name   s   
z#ExceptionRaiseRefVisitor.visit_namec                 C   s   t |jtjr| |j t|jdkrCt |jd tjrEt |jd jt	rG|jd j}d|v s8d|v rId|v rK| j
jd| jd d S d S d S d S d S d S )N   r   %{}r=   rc   )r   funcr   Namerf   lenargsConstvaluestrrO   rd   rP   )rQ   r-   msgr   r   r   
visit_call   s   z#ExceptionRaiseRefVisitor.visit_call)	r   r]   r^   r_   r   rl   rf   Callrs   r   r   r   r   ra      s    ra   c                   @   sx   e Zd ZdZdejddfddZdejddfdd	Z	e	Z
dejddfd
dZdejddfddZdejddfddZdS )ExceptionRaiseLeafVisitorz1Visitor for handling leaf kinds of a raise value.r-   r+   Nc                 C   s   | j jd| j|jjjd d S )Nr1   r-   rn   )rO   rd   rP   rp   rU   r   re   r   r   r   visit_const   s   
z%ExceptionRaiseLeafVisitor.visit_constinstancec                 C   s   |j }| | d S r
   )_proxiedvisit_classdef)rQ   rx   clsr   r   r   visit_instance   s   z(ExceptionRaiseLeafVisitor.visit_instancec                 C   s<   t |st |r|jr| jjd| jd d S d S d S d S )Nr4   rc   )r	   inherit_from_std_exhas_known_basesnewstylerO   rd   rP   re   r   r   r   rz      s
   z(ExceptionRaiseLeafVisitor.visit_classdefr   c                 C   s   | j jd| jdd d S )Nr1   tuplerv   )rO   rd   rP   r\   r   r   r   visit_tuple   s   z%ExceptionRaiseLeafVisitor.visit_tuplec                 C   s(   t |d|jj}| jjd| j|d d S )NrY   r1   rv   )rW   rU   r   rO   rd   rP   )rQ   r-   rY   r   r   r   rX      s   z'ExceptionRaiseLeafVisitor.visit_default)r   r]   r^   r_   r   ro   rw   r   ExceptionInstancer|   visit_exceptioninstanceClassDefrz   r   r   r`   rX   r   r   r   r   ru      s    ru   c                	       s"  e Zd ZdZejZdZeZ	dZ
deddddef d	ffZ fd
dZeddddddddejddfddZdd ZdejddfddZdejddfddZdd Zd d! Zed"dejddfd#d$Zed"dejddfd%d&Zed'd(d)d*d+d,d-dejddfd.d/Z  ZS )0ExceptionsCheckerzException related checks.
exceptionszovergeneral-exceptionscsvz<comma-separated class names>zHExceptions that will emit a warning when being caught. Defaults to "%s"., )defaultr   metavarhelpc                    s   t  | _ t   d S r
   )r   superopen)rQ   rU   r   r   r     s   zExceptionsChecker.openr3   r1   r4   r5   r2   r=   r;   r-   r+   Nc                 C   s   |j d u r| | d S |jd u r| | n| | |j }t| || z| d }W n tj	y:   Y d S w |rGt
| || d S d S )N)r   _check_misplaced_bare_raisecause_check_raise_missing_from_check_bad_exception_contextra   r[   r(   r"   InferenceErrorru   )rQ   r-   exprinferred_valuer   r   r   visit_raise  s    




zExceptionsChecker.visit_raisec                 C   s   |  }t|tjr| r|jdkrd S |}tjtjf}|r0t|j|s0|j}|r0t|j|r%tjf}|r<t|j|sE| jd|d d S d S )N__exit__r3   rc   )	scoper   r   FunctionDef	is_methodrY   ExceptHandlerparentrd   )rQ   r-   r   currentignoresexpectedr   r   r   r   :  s    

z-ExceptionsChecker._check_misplaced_bare_raisec                 C   sz   t |j}|tjdfv rdS t|tjr%|jdur#| j	d|d dS dS t|tj
s9t |s;| j	d|d dS dS dS )z}Verify that the exception context is properly set.

        An exception context can be only `None` or an exception.
        Nr2   rc   )r	   r!   r   r"   r#   r   r   ro   rp   rd   r   r}   )rQ   r-   r   r   r   r   r   O  s   
z.ExceptionsChecker._check_bad_exception_contextc                 C   s   |j d u rd S t|}|sd S |jd u r| jd|d d S t|j tjr6t|j jtj	r6| jd|d d S t|j tj	rN|j j|jjkrP| jd|d d S d S d S )Nr;   rc   )
r   r	   !find_except_wrapper_node_in_scoperY   rd   r   r   rt   rk   rl   )rQ   r-   containing_except_noder   r   r   r   `  s   


z+ExceptionsChecker._check_raise_missing_fromc                 C   s  t |tjr$dd |jD }tdd |D rd S tdd |D r$d S t |tjsdt |tjrV|jd u rVt |j	tjrB|j	jd u sH|j	
|rT| jd|j	| fd d S | jd|j	| fd d S t|s|j| jvrt|r| jd|j	|jfd d S d S d S d S )Nc                 S   s   g | ]}t |qS r   )r	   r!   )r   r'   r   r   r   
<listcomp>  r   zCExceptionsChecker._check_catching_non_exception.<locals>.<listcomp>c                 s   s    | ]}|t ju V  qd S r
   )r"   r#   r   r-   r   r   r   	<genexpr>  s    zBExceptionsChecker._check_catching_non_exception.<locals>.<genexpr>c                 s   s*    | ]}|ot |pt | V  qd S r
   )r	   r}   r~   r   r   r   r   r     s    
r6   rv   )r   r   r   r    anyallr   ro   rp   r   	parent_ofrd   	as_stringr	   r}   rY   r   r~   )rQ   handlerr   partr(   r   r   r   _check_catching_non_exception}  sJ   


z/ExceptionsChecker._check_catching_non_exceptionc           	         s   dt ttj  fdd}d}d }g }|jD ]E}|rB||}|s" d S |d u r) d S |D ]}t| t fdd|D rAd} nq+t|j	d grZ|j	d j
d u rZd}|}||}q|rf| jd	|d
 d S d S )Nr+   c                 S   sP   g }| j r&t| j }t|tjrtdd |jD }|S |r$|g}|S d S |S )Nc                 S   s   h | ]
}t |tjr|qS r   )r   r   rl   )r   	exceptionr   r   r   r         
zdExceptionsChecker._check_try_except_raise.<locals>.gather_exceptions_from_handler.<locals>.<setcomp>)r   r	   r!   r   r   r   listr    )r   r   exceptions_in_handlerr   r   r   gather_exceptions_from_handler  s   zQExceptionsChecker._check_try_except_raise.<locals>.gather_exceptions_from_handlerFc                 3   s"    | ]}t t | V  qd S r
   )r	   is_subclass_ofr!   )r   einferred_currentr   r   r     s
    
z<ExceptionsChecker._check_try_except_raise.<locals>.<genexpr>r   Tr:   rc   )r   r   r   r`   handlersr	   r!   r   r.   r*   r   rd   )	rQ   r-   r   
bare_raisehandler_having_bare_raiseexcs_in_bare_handlerr   excs_in_current_handlerexc_in_current_handlerr   r   r   _check_try_except_raise  s<   


z)ExceptionsChecker._check_try_except_raiser>   c                 C   sF   t |jtjr!d|j  d|j  d}| jd||fd d S d S )NDid you mean '(r   )' instead?r>   rv   )r   r   r   r   leftr   rightrd   rQ   r-   
suggestionr   r   r   visit_binop  s   zExceptionsChecker.visit_binopc                 C   sR   t |jtjr'd|j  dddd |jD  d}| jd||fd d S d S )Nr   r   c                 s   s    | ]	\}}|  V  qd S r
   )r   )r   r   operandr   r   r   r     s    z2ExceptionsChecker.visit_compare.<locals>.<genexpr>r   r>   rv   )	r   r   r   r   r   r   joinopsrd   r   r   r   r   visit_compare  s   *zExceptionsChecker.visit_comparer7   r8   r:   r<   r0   r6   r9   c              	   C   s  |  | g }t|j}t|jD ]\}}|jdu r7t|js&| jd|d ||d k r6d}| jd||d qt|jt	j
rI| jd||jjd qz	tt|j}W n
 tjy\   Y qw |D ]v\}}	|	tju riq_t|	tjrwt|	rw|	j}	| ||	| t|	t	jsq_d	d
 |	 D }
|D ]}||
v r|j d|	j }| jd|j|d q|	j| jjv r|	 jtjkrt|js| jd|	j|jd |	|v r| jd|	j|jd q_|dd
 |D 7 }qdS )zcheck for empty exceptNr7   rc   rg   z-empty except clause should always appear lastr0   rv   r<   c                 S   s   g | ]
}t |tjr|qS r   )r   r   r   )r   ancr   r   r   r   %  r   z5ExceptionsChecker.visit_tryexcept.<locals>.<listcomp>z is an ancestor class of r8   )rn   r-   r9   c                 S   s   g | ]\}}|qS r   r   r   r   r   r   r   ?  s    )r   rm   r   	enumerater   r.   r*   rd   r   r   BoolOpopr   r)   r"   r   r#   Instancer	   r}   ry   r   r   	ancestorsrY   configovergeneral_exceptionsrootEXCEPTIONS_MODULE)rQ   r-   exceptions_classesnb_handlersindexr   rr   excsr   r   exc_ancestorsprevious_excr   r   r   visit_tryexcept  sl   







z!ExceptionsChecker.visit_tryexcept) r   r]   r^   r_   r   IAstroidChecker__implements__rY   MSGSmsgspriorityOVERGENERAL_EXCEPTIONSr   optionsr   r	   check_messagesr   r,   r   r   r   r   r   r   BinOpr   Comparer   	TryExceptr   __classcell__r   r   r   r   r     s\    
	09	r   c                 C   s   |  t|  dS )z-required method to auto register this checkerN)register_checkerr   )linterr   r   r   registerB  s   r   r
   )r_   r   r   typingr   r   r   r"   r   r   pylintr   r   pylint.checkersr	   r   r)   boolr.   r   r   rN   ra   ru   BaseCheckerr   r   r   r   r   r   <module>   sF   "

	j  B