o
    g9T                     @   s&  d Z ddlZddlmZ ddlmZmZmZ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ddddddddd
ZedZeddhZdZdZdZedZde	jdefdd Z d!d" Z!d#d$ Z"de
jd%ee# dee
j fd&d'Z$G d(d) d)eZ%d*d+ Z&dS ),zcheck for signs of poor design    N)defaultdict)	FrozenSetListSetcast)nodes)utils)BaseChecker)check_messages)IAstroidChecker)CheckerStats)zToo many ancestors (%s/%s)too-many-ancestorsznUsed when class has too many parent classes, try to reduce this to get a simpler (and so easier to use) class.)z$Too many instance attributes (%s/%s)too-many-instance-attributeszsUsed when class has too many instance attributes, try to reduce this to get a simpler (and so easier to use) class.)zToo few public methods (%s/%s)too-few-public-methodszLUsed when class has too few public methods, so be sure it's really worth it.)zToo many public methods (%s/%s)too-many-public-methodsznUsed when class has too many public methods, try to reduce this to get a simpler (and so easier to use) class.)z"Too many return statements (%s/%s)too-many-return-statementszWUsed when a function or method has too many return statement, making it hard to follow.)zToo many branches (%s/%s)too-many-brancheszOUsed when a function or method has too many branches, making it hard to follow.)zToo many arguments (%s/%s)too-many-argumentsz8Used when a function or method takes too many arguments.)z Too many local variables (%s/%s)too-many-localsz<Used when a function or method has too many local variables.)zToo many statements (%s/%s)too-many-statementszpUsed when a function or method has too many statements. You should then split it in smaller functions / methods.)z4Too many boolean expressions in if statement (%s/%s)too-many-boolean-expressionsz@Used when an if statement contains too many boolean expressions.)
R0901R0902R0903R0904R0911R0912R0913R0914R0915R0916z^_{2}[a-z]+_{2}$	dataclassattrsdataclassesztyping.NamedTupleztyping.TypedDict)Kzbuiltins.objectzbuiltins.tuplezbuiltins.dictzbuiltins.listzbuiltins.setzbulitins.frozensetzcollections.ChainMapzcollections.Counterzcollections.OrderedDictzcollections.UserDictzcollections.UserListzcollections.UserStringzcollections.defaultdictzcollections.dequezcollections.namedtuplez_collections_abc.Awaitablez_collections_abc.Coroutinez_collections_abc.AsyncIterablez_collections_abc.AsyncIteratorz_collections_abc.AsyncGeneratorz_collections_abc.Hashablez_collections_abc.Iterablez_collections_abc.Iteratorz_collections_abc.Generatorz_collections_abc.Reversiblez_collections_abc.Sizedz_collections_abc.Containerz_collections_abc.Collectionz_collections_abc.Setz_collections_abc.MutableSetz_collections_abc.Mappingz_collections_abc.MutableMappingz_collections_abc.MappingViewz_collections_abc.KeysViewz_collections_abc.ItemsViewz_collections_abc.ValuesViewz_collections_abc.Sequencez _collections_abc.MutableSequencez_collections_abc.ByteStringztyping.Tupleztyping.Listztyping.Dictz
typing.Setztyping.FrozenSetztyping.Dequeztyping.DefaultDictztyping.OrderedDictztyping.Counterztyping.ChainMapztyping.Awaitableztyping.Coroutineztyping.AsyncIterableztyping.AsyncIteratorztyping.AsyncGeneratorztyping.Iterableztyping.Iteratorztyping.Generatorztyping.Reversibleztyping.Containerztyping.Collectionztyping.AbstractSetztyping.MutableSetztyping.Mappingztyping.MutableMappingztyping.Sequenceztyping.MutableSequenceztyping.ByteStringztyping.MappingViewztyping.KeysViewztyping.ItemsViewztyping.ValuesViewztyping.ContextManagerztyping.AsyncContextMangerztyping.Hashableztyping.Sizednodereturnc                 C   s   |   D ]}|jdkr| jdkr dS | ttfv r  dS q| js&dS t|  j}| jj	D ]2}t
|tjr<|j}t
|tjtjfsFq1t
|tjrP|j}n|j}|tv rc|ts`t|v rc dS q1dS )z6Check if a class is exempt from too-few-public-methodsEnumenumTF)	ancestorsnamerootqnameTYPING_NAMEDTUPLETYPING_TYPEDDICT
decoratorssetlocalsr   
isinstanceastroidCallfuncName	AttributeattrnameDATACLASSES_DECORATORSintersectionDATACLASS_IMPORT)r$   ancestorroot_locals	decoratorr)    r>   ]/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pylint/checkers/design_analysis.py_is_exempt_from_public_methods   s0   r@   c                 C   s8   d}|   D ]}t|tjr|t|7 }q|d7 }q|S )zCounts the number of boolean expressions in BoolOp `bool_op` (recursive)

    example: a and (b or c or (d and e)) ==> 5 boolean expressions
    r      )get_childrenr1   r2   BoolOp_count_boolean_expressions)bool_opnb_bool_expr	bool_exprr>   r>   r?   rD      s   
rD   c                 C   sF   t dd |  D }|  D ]}t|jr |jdkr |d7 }q|S )Nc                 s        | ]}|j d sdV  qdS _rA   Nr)   
startswith.0methodr>   r>   r?   	<genexpr>   s    z*_count_methods_in_class.<locals>.<genexpr>__init__rA   )summethods	mymethodsSPECIAL_OBJsearchr)   )r$   all_methodsrO   r>   r>   r?   _count_methods_in_class   s   rX   ignored_parentsc                 C   s`   t  }tttj t| jdd}|r.| }| |v rq|	| |
|jdd |s|S )a  Get parents of ``node``, excluding ancestors of ``ignored_parents``.

    If we have the following inheritance diagram:

             F
            /
        D  E
         \/
          B  C
           \/
            A      # class A(B, C): ...

    And ``ignored_parents`` is ``{"E"}``, then this function will return
    ``{A, B, C, D}`` -- both ``E`` and its ancestors are excluded.
    F)recurs)r/   r   r   r   ClassDeflistr(   popr+   addextend)r$   rY   parents
to_exploreparentr>   r>   r?   _get_parents   s   
rc   c                   @   s(  e Zd ZdZef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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'dddd(d	ffZ
dYd*d+Zd,d- Zd.d/ Zejjd0d1 Zed2d3d4d5d6ejd7d)fd8d9Zed4d5d6ejd7d)fd:d;Zed<d=d>d?d@dAd6ejd7d)fdBdCZeZed<d=d>d?d@d6ejd7d)fdDdEZeZdFejd7d)fdGdHZd6ejd7d)fdIdJZd6ej d7d)fdKdLZ!d6ej"d7d)fdMdNZ#edOd6ej$d7d)fdPdQZ%dRdS Z&d6ej'd7d)fdTdUZ(e(Z)dZdWdXZ*d)S )[MisdesignCheckerzchecks for sign of poor/misdesign:
    * number of methods, attributes, local variables...
    * size, complexity of functions, methods
    designzmax-args   intz<int>z2Maximum number of arguments for function / method.)defaulttypemetavarhelpz
max-locals   z4Maximum number of locals for function / method body.zmax-returns   z<Maximum number of return / yield for function / method body.zmax-branches   z4Maximum number of branch for function / method body.zmax-statements2   z7Maximum number of statements in function / method body.zmax-parents   z<num>z2Maximum number of parents for a class (see R0901).zignored-parentsr>   csvz%<comma separated list of class names>zOList of qualified class names to ignore when counting class parents (see R0901)zmax-attributesz5Maximum number of attributes for a class (see R0902).zmin-public-methods   z9Minimum number of public methods for a class (see R0903).zmax-public-methods   z9Maximum number of public methods for a class (see R0904).zmax-bool-exprzEMaximum number of boolean expressions in an if statement (see R0916).Nc                 C   s(   t | | i | _d | _d | _d | _d S N)r	   rQ   stats_returns	_branches_stmts)selflinterr>   r>   r?   rQ     s
   
zMisdesignChecker.__init__c                 C   s&   | j  | _g | _tt| _g | _dS )zinitialize visit variablesN)r{   	add_statsrv   rw   r   rh   rx   ry   rz   r>   r>   r?   open  s   

zMisdesignChecker.openc                 C   s*   t | jD ]\}}| j|  |7  < qd S ru   )	enumeratery   )rz   amountirJ   r>   r>   r?   _inc_all_stmts  s   zMisdesignChecker._inc_all_stmtsc                 C   s   t j| dd dS )Nzignored-argument-names)ri   )r   get_global_optionr}   r>   r>   r?   _ignored_argument_names  s   z(MisdesignChecker._ignored_argument_namesr   r   r   r   r$   r%   c                 C   sx   t |t| jj}t|}|| jjkr | jd||| jjfd t|j| jj	kr:| jd|t|j| jj	fd dS dS )zEcheck size of inheritance hierarchy and number of instance attributesr   r$   argsr   N)
rc   STDLIB_CLASSES_IGNORE_ANCESTORunionconfigrY   lenmax_parentsadd_messageinstance_attrsmax_attributes)rz   r$   r`   
nb_parentsr>   r>   r?   visit_classdef  s"   

zMisdesignChecker.visit_classdefc                 C   s   t dd | D }|| jjkr| jd||| jjfd |jdks&t|r(dS t|}|| jjk r@| jd||| jjfd dS dS )zcheck number of public methodsc                 s   rH   rI   rK   rM   r>   r>   r?   rP         
z2MisdesignChecker.leave_classdef.<locals>.<genexpr>r   r   classNr   )	rR   rT   r   max_public_methodsr   rj   r@   rX   min_public_methods)rz   r$   
my_methodsrW   r>   r>   r?   leave_classdef  s&   


zMisdesignChecker.leave_classdefr   r   r   r   r   zkeyword-arg-before-varargc                    s   | j d |jj}| j |dur;d} r t fdd|D }t|| }|| jjkr:| jd|t|| jjfd nd}t|j	| }|| jj
krV| jd||| jj
fd | jd dS )	zdcheck function name, docstring, arguments, redefinition,
        variable names, max locals
        r   Nc                 3   s     | ]}  |jrd V  qdS )rA   N)matchr)   )rN   argignored_argument_namesr>   r?   rP     r   z5MisdesignChecker.visit_functiondef.<locals>.<genexpr>r   r   r   rA   )rw   appendr   r   rR   r   r   max_argsr   r0   
max_localsry   )rz   r$   r   ignored_args_numargnumlocnumr>   r   r?   visit_functiondef  s0   z"MisdesignChecker.visit_functiondefc                 C   s   | j  }|| jjkr| jd||| jjfd | j| }|| jjkr.| jd||| jjfd | j }|| jjkrG| jd||| jjfd dS dS )zkmost of the work is done here on close:
        checks for max returns, branch, return in __init__
        r   r   r   r   N)	rw   r]   r   max_returnsr   rx   max_branchesry   max_statements)rz   r$   returnsbranchesstmtsr>   r>   r?   leave_functiondef  s,   






z"MisdesignChecker.leave_functiondefrJ   c                 C   s    | j sdS | j d  d7  < dS )zcount number of returnsNrA   )rw   )rz   rJ   r>   r>   r?   visit_return0  s   zMisdesignChecker.visit_returnc                 C   s   |j r
| d dS dS )zWdefault visit method -> increments the statements counter if
        necessary
        rA   N)is_statementr   rz   r$   r>   r>   r?   visit_default6  s   zMisdesignChecker.visit_defaultc                 C   s2   t |j}|jr|d7 }| || | | dS increments the branches counterrA   N)r   handlersorelse_inc_branchr   rz   r$   r   r>   r>   r?   visit_tryexcept=  s
   
z MisdesignChecker.visit_tryexceptc                 C   s   |  |d | d dS )r   rs   N)r   r   r   r>   r>   r?   visit_tryfinallyE  s   z!MisdesignChecker.visit_tryfinallyr   c                 C   sV   |  | d}|jrt|jdkst|jd tjs|d7 }| || | | dS )z>increments the branches counter and checks boolean expressionsrA   r   N)_check_boolean_expressionsr   r   r1   r2   Ifr   r   r   r>   r>   r?   visit_ifJ  s   
 zMisdesignChecker.visit_ifc                 C   sJ   |j }t|tjsdS t|}|| jjkr#| jd||| jjfd dS dS )zwGo through "if" node `node` and counts its boolean expressions

        if the "if" node test is a BoolOp node
        Nr   r   )testr1   r2   rC   rD   r   max_bool_exprr   )rz   r$   	conditionrF   r>   r>   r?   r   W  s   

z+MisdesignChecker._check_boolean_expressionsc                 C   s"   d}|j r	|d7 }| || dS r   )r   r   r   r>   r>   r?   visit_whileg  s   zMisdesignChecker.visit_whilerA   c                 C   s   | j |   |7  < dS )r   N)rx   scope)rz   r$   branchesnumr>   r>   r?   r   p  s   zMisdesignChecker._inc_branchru   )rA   )+__name__
__module____qualname____doc__r   __implements__r)   MSGSmsgspriorityoptionsrQ   r~   r   r2   r.   cachedpropertyr   r
   r   r[   r   r   FunctionDefr   visit_asyncfunctiondefr   leave_asyncfunctiondefReturnr   NodeNGr   	TryExceptr   
TryFinallyr   r   r   r   Whiler   	visit_forr   r>   r>   r>   r?   rd     s    






k
$"rd   c                 C   s   |  t|  dS )z-required method to auto register this checkerN)register_checkerrd   )r{   r>   r>   r?   registeru  s   r   )'r   recollectionsr   typingr   r   r   r   r2   r   pylintr   pylint.checkersr	   pylint.checkers.utilsr
   pylint.interfacesr   pylint.typingr   r   compilerU   	frozensetr8   r:   r,   r-   r   r[   boolr@   rD   rX   strrc   rd   r   r>   r>   r>   r?   <module>   sX   
;Q 

  e