
    e                       d 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 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 erddlmZ e	ej2                  ej4                  ej6                  ej8                  ej:                  ej<                  ej>                  ej@                  ejB                  ejD                  ejF                  ejH                  ejJ                  ejL                  ejN                  ejP                  f   Z)e	ejT                  ejV                  ejX                  ejZ                  f   Z. ede	e)ejZ                  ej^                  f         Z0 G d de      Z G d de      Z G d dejb                        Z2ddZ3y)z.Module to add McCabe checker class for pylint.    )annotations)Sequence)TYPE_CHECKINGAnyTypeVarUnion)nodes)	PathGraph)PathGraphingAstVisitor)checkers)only_required_for_messages)HIGH)PyLinter_AppendableNodeT)boundc                        e Zd Zd fdZ xZS )r
   c                8    t         |   ddd       || _        y )N    )nameentitylineno)super__init__root)selfnode	__class__s     :/usr/lib/python3/dist-packages/pylint/extensions/mccabe.pyr   zPathGraph.__init__1   s    bA6	    )r   z"_SubGraphNodes | nodes.FunctionDef)__name__
__module____qualname__r   __classcell__r   s   @r   r
   r
   0   s     r    r
   c                       e Zd Zd
 fdZddZddZddZeZddZexZ	xZ
xZxZxZxZxZxZxZxZxZxZxZxZxZZddZeZddZ	 d	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 dd	Z xZS )r   c                >    t         |           d| _        d | _        y )Nr   )r   r   _bottom_countergraph)r   r   s    r   r   zPathGraphingAstVisitor.__init__7   s     '+
r    c                T    |j                         D ]  } | j                  |g|   y N)get_childrendispatch)r   r   argschilds       r   defaultzPathGraphingAstVisitor.default<   s-    &&( 	(EDMM%'$'	(r    c                    || _         |j                  }| j                  j                  |      }|?|j                  }t        | j                  d|z   | j                        }|| j                  |<    ||g| S )Nvisit)r   r   _cachegetr!   getattrvisitorr0   )r   r   r.   klassmeth
class_names         r   r-   zPathGraphingAstVisitor.dispatch@   sk    	{{u%<J4<<:)=t||LD!%DKKD 4  r    c                @   | j                   | j                  |      }|| _        | j                  |j                         | j
                   }| xj
                  dz  c_        | j                   j                  | j                  |       | j                   j                  ||       || _        y t        |      | _         || _        | j                  |j                         | j                   | j                  | j                   |j                   <   | j                          y )Nr   )r)   _append_nodetaildispatch_listbodyr(   connectr
   graphs	classnamer   reset)r   r   pathnodebottoms       r   visitFunctionDefz'PathGraphingAstVisitor.visitFunctionDefJ   s    ::!((.H DItyy),,-F  A% JJtyy&1JJtV,DI"4DJDItyy):>**DKK4>>*499+67JJLr    c                &    | j                  |       y r+   )r;   r   r   s     r   visitSimpleStatementz+PathGraphingAstVisitor.visitSimpleStatement^   s    $r    c                \    | j                  |       | j                  |j                         y r+   )r;   r=   r>   rG   s     r   	visitWithz PathGraphingAstVisitor.visitWithy   s"    $499%r    c                    | j                   r| j                  sy | j                  j                  | j                   |       || _         |S r+   )r<   r)   r?   rG   s     r   r;   z#PathGraphingAstVisitor._append_node   s6    yy



499d+	r    c                   | j                   [t        |      | _         | j                  |||       | j                   | j                  | j                   | <   | j                          y| j                  |       | j                  |||       y)z@Create the subgraphs representing any `if` and `for` statements.N)r)   r
   _subgraph_parser@   rA   rB   r;   )r   r   r   extra_blockss       r   	_subgraphz PathGraphingAstVisitor._subgraph   st     ::"4DJ  t\:59ZZDKK4>>*4&12JJLd#  t\:r    c                x   g }|| _         | j                  |j                         |j                  | j                          |D ]?  }|| _         | j                  |j                         |j                  | j                          A |j                  r>|| _         | j                  |j                         |j                  | j                          n|j                  |       |rZ| j
                  rM| j                   }| xj                  dz  c_        |D ]  }| j
                  j                  ||         || _         yyy)zAParse the body and any `else` block of `if` and `for` statements.r   N)r<   r=   r>   appendorelser)   r(   r?   )r   r   rC   rN   
loose_endsextrarD   ends           r   rM   z&PathGraphingAstVisitor._subgraph_parse   s	    
	499%$))$! 	)EDIuzz*dii(	) ;;DIt{{+dii(d#DJJ,,-F  A% ! 0

""3/0DI 4r    )returnNone)r   nodes.NodeNGr.   r   rV   rW   )r   rX   r.   r   rV   r   )r   znodes.FunctionDefrV   rW   )r   _StatementNodesrV   rW   )r   z
nodes.WithrV   rW   )r   r   rV   z_AppendableNodeT | None) )r   _SubGraphNodesr   strrN   Sequence[nodes.ExceptHandler]rV   rW   )r   r[   rC   r[   rN   r]   rV   rW   )r!   r"   r#   r   r0   r-   rE   visitAsyncFunctionDefrH   visitAssertvisitAssignvisitAugAssignvisitDelete
visitRaise
visitYieldvisitImport	visitCallvisitSubscript	visitPassvisitContinue
visitBreakvisitGlobalvisitReturn	visitExpr
visitAwaitrJ   visitAsyncWithr;   rO   rM   r$   r%   s   @r   r   r   6   s8   ,
(!$ - 2 >R-RK RR 	R
 	R 	R 	R 	R 	R 	R" 	#R& 	'R* 	+R, 	-R, "-R, %.
& N 79	;; ; 4	;
 
;" ! 4	
 
r    r   c                  N    e Zd ZdZdZddiZddddd	d
ffZ ed      dd       Zy)McCabeMethodCheckerzoChecks McCabe complexity cyclomatic threshold in methods and functions
    to validate a too complex code.
    designR1260)z*%s is too complex. The McCabe rating is %dtoo-complexzSUsed when a method or function is too complex based on McCabe Complexity Cyclomaticzmax-complexity
   intz<int>z&McCabe complexity cyclomatic threshold)r0   typemetavarhelprt   c                   t               }|j                  D ]  }|j                  ||        |j                  j	                         D ]  }|j                         }|j                  }t        |d      rd|j                   d}n(d|j                  j                  j                          d}|| j                  j                  j                  k  r| j                  d|t         ||f        y)zVisit an astroid.Module node to check too complex rating and
        add message if is greater than max_complexity stored from options.
        r   'zThis 'rt   )r   
confidencer.   N)r   r>   preorderr@   values
complexityr   hasattrr   r   r!   lowerlinterconfigmax_complexityadd_messager   )r   r   r6   r/   r)   r   	node_names          r   visit_modulez McCabeMethodChecker.visit_module   s    
 )*YY 	-EUG,	-^^**, 	E))+J::DtV$		{!,	$T^^%<%<%B%B%D$EQG	T[[//>>>DTJ@W  	r    N)r   znodes.ModulerV   rW   )	r!   r"   r#   __doc__r   msgsoptionsr   r   rZ   r    r   rq   rq      sZ     D 	 
D "@		

G  . /r    rq   c                8    | j                  t        |              y r+   )register_checkerrq   )r   s    r   registerr      s    
/78r    N)r   r   rV   rW   )4r   
__future__r   collections.abcr   typingr   r   r   r   astroidr	   mccaber
   Mccabe_PathGraphr   Mccabe_PathGraphingAstVisitorpylintr   pylint.checkers.utilsr   pylint.interfacesr   pylint.lintr   AssertAssign	AugAssignDeleteRaiseYieldImportCall	SubscriptPassContinueBreakGlobalReturnExprAwaitrY   IfTryForWhiler[   FunctionDefr   BaseCheckerrq   r   rZ   r    r   <module>r      s-  
 5 " $ 5 5  0 J  < "$	LL	LL	OO	LL	KK	KK	LL	JJ	OO	JJ	NN	KK	LL	LL	JJ	KK& uxxEIIu{{BCeOU[[%BSBS$ST 
  {: {|.(.. .b9r    