3
tûœ]í@  ã               @   sÚ   d Z ddlZddlmZ ddlZddl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 d8d9d:d;d<d=d>d?d@dAd'œ
Zejd(ƒZed)d*hƒZd+Zd,Zejed-œd.d/„Zd0d1„ Zd2d3„ ZG d4d5„ d5eƒZd6d7„ ZdS )Bzcheck for signs of poor designé    N)Údefaultdict)ÚBoolOpÚIfÚ
decorators)Úutils)ÚBaseChecker)Úcheck_messages)ÚIAstroidCheckerúToo many ancestors (%s/%s)útoo-many-ancestorsúnUsed when class has too many parent classes, try to reduce this to get a simpler (and so easier to use) class.ú$Too many instance attributes (%s/%s)útoo-many-instance-attributesúsUsed when class has too many instance attributes, try to reduce this to get a simpler (and so easier to use) class.úToo few public methods (%s/%s)útoo-few-public-methodsúLUsed when class has too few public methods, so be sure it's really worth it.úToo many public methods (%s/%s)útoo-many-public-methodsúnUsed when class has too many public methods, try to reduce this to get a simpler (and so easier to use) class.ú"Too many return statements (%s/%s)útoo-many-return-statementsúWUsed when a function or method has too many return statement, making it hard to follow.úToo many branches (%s/%s)útoo-many-branchesúOUsed when a function or method has too many branches, making it hard to follow.úToo many arguments (%s/%s)útoo-many-argumentsú8Used when a function or method takes too many arguments.ú Too many local variables (%s/%s)útoo-many-localsú<Used when a function or method has too many local variables.úToo many statements (%s/%s)útoo-many-statementsúpUsed when a function or method has too many statements. You should then split it in smaller functions / methods.ú4Too many boolean expressions in if statement (%s/%s)útoo-many-boolean-expressionsú@Used when an if statement contains too many boolean expressions.)
ZR0901ZR0902ZR0903ZR0904ZR0911ZR0912ZR0913ZR0914ZR0915ZR0916z^_{2}[a-z]+_{2}$Z	dataclassÚattrsZdataclassesztyping.NamedTuple)ÚnodeÚreturnc             C   sÊ   x<| j ƒ D ]0}|jdkr*|jƒ jdkr*dS |jƒ tkr
dS q
W | jsHdS t| jƒ jƒ}xn| jjD ]b}t	|t
jƒrv|j}t	|t
jt
jfƒsŠq`t	|t
jƒrž|j}n|j}|tkr`|jtƒs¾t|kr`dS q`W dS )z6Check if a class is exempt from too-few-public-methodsÚEnumÚenumTF)Ú	ancestorsÚnameÚrootÚqnameÚTYPING_NAMEDTUPLEr   ÚsetÚlocalsZnodesÚ
isinstanceÚastroidÚCallÚfuncÚNameÚ	AttributeÚattrnameÚDATACLASSES_DECORATORSÚintersectionÚDATACLASS_IMPORT)r)   ZancestorZroot_localsZ	decoratorr.   © r>   úA/tmp/pip-build-fn42xldr/pylint/pylint/checkers/design_analysis.pyÚ_is_exempt_from_public_methods`   s*    
r@   c             C   s:   d}x0| j ƒ D ]$}t|tƒr*|t|ƒ7 }q|d7 }qW |S )z‘Counts the number of boolean expressions in BoolOp `bool_op` (recursive)

    example: a and (b or c or (d and e)) ==> 5 boolean expressions
    r   é   )Zget_childrenr4   r   Ú_count_boolean_expressions)Zbool_opÚnb_bool_exprZ	bool_exprr>   r>   r?   rB   €   s    
rB   c             C   sJ   t dd„ | jƒ D ƒƒ}x.| jƒ D ]"}tj|jƒr |jdkr |d7 }q W |S )Nc             s   s   | ]}|j 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_methodsrG   r>   r>   r?   Ú_count_methods_in_classŽ   s
    rP   c               @   sž  e Zd ZdZefZdZeZdPZ	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
dQd$d%„Zd&d'„ Zd(d)„ Zejd*d+„ ƒZed,d-d.d/ƒd0d1„ ƒZed.d/ƒd2d3„ ƒZed4d5d6d7d8d9ƒd:d;„ ƒZeZed4d5d6d7d8ƒd<d=„ ƒZeZd>d?„ Zd@dA„ ZdBdC„ ZdDdE„ ZedFƒdGdH„ ƒZdIdJ„ ZdKdL„ ZeZdRdNdO„Z d#S )SÚMisdesignCheckerzŒchecks for sign of poor/misdesign:
    * number of methods, attributes, local variables...
    * size, complexity of functions, methods
    Zdesigné   z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-statementsé2   z7Maximum number of statements in function / method body.zmax-parentsé   z<num>z2Maximum number of parents for a class (see R0901).zmax-attributesz5Maximum number of attributes for a class (see R0902).zmin-public-methodsz9Minimum 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 j| |ƒ d | _d | _d | _d | _d S )N)r   rI   ÚstatsÚ_returnsÚ	_branchesÚ_stmts)ÚselfÚlinterr>   r>   r?   rI     s
    zMisdesignChecker.__init__c             C   s&   | j jƒ | _g | _ttƒ| _g | _dS )zinitialize visit variablesN)rd   Z	add_statsr_   r`   r   rT   ra   rb   )rc   r>   r>   r?   Úopen  s    
zMisdesignChecker.openc             C   s.   x(t t| jƒƒD ]}| j|  |7  < qW d S )N)ÚrangeÚlenrb   )rc   ÚamountÚir>   r>   r?   Ú_inc_all_stmts  s    zMisdesignChecker._inc_all_stmtsc             C   s   t j| dd d�S )Nzignored-argument-names)rU   )r   Zget_global_option)rc   r>   r>   r?   Ú_ignored_argument_names  s    z(MisdesignChecker._ignored_argument_namesztoo-many-ancestorsztoo-many-instance-attributesztoo-few-public-methodsztoo-many-public-methodsc             C   sh   t t|jƒ ƒƒ}|| jjkr4| jd||| jjfd� t |jƒ| jjkrd| jd|t |jƒ| jjfd� dS )zNcheck size of inheritance hierarchy and number of instance attributes
        ztoo-many-ancestors)r)   Úargsztoo-many-instance-attributesN)rg   Úlistr-   ÚconfigZmax_parentsÚadd_messageZinstance_attrsZmax_attributes)rc   r)   Z
nb_parentsr>   r>   r?   Úvisit_classdef  s    	zMisdesignChecker.visit_classdefc             C   s€   t dd„ |jƒ D ƒƒ}|| jjkr:| jd||| jjfd� |jdksLt|ƒrPdS t|ƒ}|| jjk r|| jd||| jjfd� dS )zcheck number of public methodsc             s   s   | ]}|j jd ƒsdV  qdS )rD   rA   N)r.   rE   )rF   rG   r>   r>   r?   rH   :  s    z2MisdesignChecker.leave_classdef.<locals>.<genexpr>ztoo-many-public-methods)r)   rl   ÚclassNztoo-few-public-methods)	rJ   rL   rn   Zmax_public_methodsro   rV   r@   rP   Zmin_public_methods)rc   r)   Z
my_methodsrO   r>   r>   r?   Úleave_classdef6  s    
zMisdesignChecker.leave_classdefztoo-many-return-statementsztoo-many-branchesztoo-many-argumentsztoo-many-localsztoo-many-statementszkeyword-arg-before-varargc                s¼   | j jdƒ |jj}| j‰ |dk	rvd}ˆ r@t‡ fdd„|D ƒƒ}t|ƒ| }|| jjkrz| jd|t|ƒ| jjfd� nd}t|j	ƒ| }|| jj
kr¬| jd||| jj
fd� | jjdƒ dS )	zdcheck function name, docstring, arguments, redefinition,
        variable names, max locals
        r   Nc             3   s   | ]}ˆ j |jƒrd V  qdS )rA   N)Úmatchr.   )rF   Úarg)Úignored_argument_namesr>   r?   rH   p  s    z5MisdesignChecker.visit_functiondef.<locals>.<genexpr>ztoo-many-arguments)r)   rl   ztoo-many-localsrA   )r`   Úappendrl   rk   rJ   rg   rn   Zmax_argsro   r3   Z
max_localsrb   )rc   r)   rl   Zignored_args_numZargnumZlocnumr>   )ru   r?   Úvisit_functiondef[  s(    z"MisdesignChecker.visit_functiondefc             C   sŽ   | j 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jkrŠ| jd||| jjfd� dS )zkmost of the work is done here on close:
        checks for max returns, branch, return in __init__
        ztoo-many-return-statements)r)   rl   ztoo-many-branchesztoo-many-statementsN)	r`   Úpoprn   Zmax_returnsro   ra   Zmax_branchesrb   Zmax_statements)rc   r)   ZreturnsÚbranchesZstmtsr>   r>   r?   Úleave_functiondef‡  s$    


z"MisdesignChecker.leave_functiondefc             C   s    | j s
dS | j d  d7  < dS )zcount number of returnsNrA   éÿÿÿÿ)r`   )rc   rD   r>   r>   r?   Úvisit_return«  s    zMisdesignChecker.visit_returnc             C   s   |j r| jdƒ dS )zWdefault visit method -> increments the statements counter if
        necessary
        rA   N)Zis_statementrj   )rc   r)   r>   r>   r?   Úvisit_default±  s    zMisdesignChecker.visit_defaultc             C   s2   t |jƒ}|jr|d7 }| j||ƒ | j|ƒ dS )zincrements the branches counterrA   N)rg   ÚhandlersÚorelseÚ_inc_branchrj   )rc   r)   ry   r>   r>   r?   Úvisit_tryexcept¸  s
    
z MisdesignChecker.visit_tryexceptc             C   s   | j |dƒ | jdƒ dS )zincrements the branches counterrR   N)r€   rj   )rc   r)   r>   r>   r?   Úvisit_tryfinallyÀ  s    z!MisdesignChecker.visit_tryfinallyztoo-many-boolean-expressionsc             C   sV   | j |ƒ d}|jr<t|jƒdks4t|jd tƒ r<|d7 }| j||ƒ | j|ƒ dS )z>increments the branches counter and checks boolean expressionsrA   r   N)Ú_check_boolean_expressionsr   rg   r4   r   r€   rj   )rc   r)   ry   r>   r>   r?   Úvisit_ifÅ  s    
&zMisdesignChecker.visit_ifc             C   sD   |j }t|tƒsdS t|ƒ}|| jjkr@| jd||| jjfd� dS )zwGo through "if" node `node` and counts its boolean expressions

        if the "if" node test is a BoolOp node
        Nztoo-many-boolean-expressions)r)   rl   )Útestr4   r   rB   rn   Zmax_bool_exprro   )rc   r)   Ú	conditionrC   r>   r>   r?   rƒ   Ð  s    
z+MisdesignChecker._check_boolean_expressionsc             C   s"   d}|j r|d7 }| j||ƒ dS )zincrements the branches counterrA   N)r   r€   )rc   r)   ry   r>   r>   r?   Úvisit_whileà  s    zMisdesignChecker.visit_whilerA   c             C   s   | j |jƒ   |7  < dS )zincrements the branches counterN)ra   Zscope)rc   r)   Zbranchesnumr>   r>   r?   r€   é  s    zMisdesignChecker._inc_branchéþÿÿÿ)N)rA   )!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   Z__implements__r.   ÚMSGSZmsgsÚpriorityÚoptionsrI   re   rj   r   Zcachedpropertyrk   r   rp   rr   rw   Zvisit_asyncfunctiondefrz   Zleave_asyncfunctiondefr|   r}   r�   r‚   r„   rƒ   r‡   Z	visit_forr€   r>   r>   r>   r?   rQ   ˜   s²   
%$rQ   c             C   s   | j t| ƒƒ dS )z.required method to auto register this checker N)Zregister_checkerrQ   )rd   r>   r>   r?   Úregisterî  s    r�   )r
   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r    r!   )r"   r#   r$   )r%   r&   r'   )rŒ   ÚreÚcollectionsr   r5   r   r   r   Zpylintr   Zpylint.checkersr   Zpylint.checkers.utilsr   Zpylint.interfacesr	   r�   ÚcompilerM   Ú	frozensetr;   r=   r1   ÚClassDefÚboolr@   rB   rP   rQ   r�   r>   r>   r>   r?   Ú<module>   sb                       
 
  X