B
    A!p\                 @   s&  d Z ddlmZ ddlZddlmZmZ ddl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lmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z) ej*dkrdZ+ndZ+ddddhZ,e-ddZ.e-ddZ/dd Z0dd Z1dd Z2dd  Z3d!d" Z4d#d$ Z5d%d& Z6d'd( Z7d)d* Z8d+d, Z9d-d. Z:d/d0 Z;d1d2 Z<d3d4 Z=dbd5d6Z>d7d8 Z?d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVZ@G dWdX dXZAG dYdZ dZeZBG d[d\ d\eZCdcd^d_ZDd`da ZEdS )dz classes checker for Python code
    )
generatorsN)chainzip_longest)	GeneratorBUILTINS)InconsistentMroErrorDuplicateBasesError)
decorators)objects)function_to_method)IAstroidChecker)BaseChecker)	PYMETHODSSPECIAL_METHODS_PARAMSoverrides_a_methodcheck_messagesis_attr_privateis_attr_protectednode_frame_classis_builtin_objectdecorated_with_propertyunimplemented_abstract_methodsdecorated_withclass_is_abstract
safe_inferhas_known_basesis_iterableis_comprehension)get_global_option)   r   __next__nextboolrangeslice
memoryview_CallSignaturez!args kws starred_args starred_kws_ParameterSignaturezargs kwonlyargs varargs kwargsc             C   s   i }g }g }g }xb| j pg D ]T}|j|j }}|d krPt|tjrP||j qt|tjrh|j||< qd ||< qW x\| jD ]R}t|tj	rt|jtjr||jj q|t|tjr||j q||d  q|W t
||||S )N)keywordsargvalue
isinstanceastroidNameappendnameargsZStarredr&   )callkwsr0   starred_kwsstarred_argskeywordr)   r*    r6   6lib/python3.7/site-packages/pylint/checkers/classes.py_signature_from_callU   s$    r8   c             C   s:   | j }| j}dd | jD }dd | jD }t||||S )Nc             S   s   g | ]}|j d kr|j qS )self)r/   ).0r)   r6   r6   r7   
<listcomp>u   s    z-_signature_from_arguments.<locals>.<listcomp>c             S   s   g | ]
}|j qS r6   )r/   )r:   r)   r6   r6   r7   r;   v   s    )kwargvarargr0   
kwonlyargsr'   )Z	argumentsr<   r=   r0   r>   r6   r6   r7   _signature_from_argumentsr   s
    r?   c       
         s   | j r| j  jk}n j }| jr0| j jk}n j }t fdd| jD }| j jk}d} jrx0 jD ]&}| jk}|| jk}	|sn|	snd}P qnW t|||||fS )z8Check if a definition signature is equivalent to a call.c             3   s   | ]}| j kV  qd S )N)r2   )r:   kw)r1   r6   r7   	<genexpr>   s    z1_definition_equivalent_to_call.<locals>.<genexpr>TF)kwargsr3   Zvarargsr4   allr>   r0   r2   )
Z
definitionr1   Zsame_kw_variadicsZsame_args_variadicsZsame_kwonlyargsZ	same_argsZno_additional_kwarg_argumentsr5   Zis_argZ	is_kwonlyr6   )r1   r7   _definition_equivalent_to_callz   s,    

rD   c             C   s"   | j j }| jdkr|dd  }|S )N)classmethodmethod   )r0   type)rF   Z
positionalr6   r6   r7   _positional_parameters   s    
rI   c             C   s    x|D ]}t | |r|S qW dS )z
    Return the type of the node if it exists in potential_types.

    Args:
        node (astroid.node): node to get the type of.
        potential_types (tuple): potential types of the node.

    Returns:
        type: type of the node or None.
    N)r+   )nodeZpotential_typesZpotential_typer6   r6   r7   _get_node_type   s    

rK   c             C   s   t | |t ||kS )a}  
    Check equality of nodes based on the comparison of their attributes named attr_name.

    Args:
        node_a (astroid.node): first node to compare.
        node_b (astroid.node): second node to compare.
        attr_name (str): name of the nodes attribute to use for comparison.

    Returns:
        bool: True if node_a.attr_name == node_b.attr_name, False otherwise.
    )getattr)Znode_aZnode_bZ	attr_namer6   r6   r7   _check_arg_equality   s    rM   c          	      s.  | j dks|j dkrdS t| j | j}dd |D }t  x|D ]}y| |}W n tjjk
rn    }Y nX y||}W n tjjk
r    }Y nX  fdd||fD }t|rt	|sdS tj
dtjdtjd	tjd	i}td
d |D }	t||	}
|
r@t||
sdS t||||
 s@dS q@W dS )a0  
    Check if original and overridden methods arguments have different default values

    Return True if one of the overridden arguments has a default
    value different from the default value of the original argument
    If one of the method doesn't have argument (.args is None)
    return False
    NFc             S   s   g | ]
}|j qS r6   )r/   )r:   paramr6   r6   r7   r;      s    z;_has_different_parameters_default_value.<locals>.<listcomp>c                s   g | ]}| kqS r6   r6   )r:   r)   )default_missingr6   r7   r;      s    Tr*   r/   Zeltsc             s   s   | ]
}|V  qd S )Nr6   )r:   Zastroid_typer6   r6   r7   rA      s    z:_has_different_parameters_default_value.<locals>.<genexpr>)r0   r   r>   objectZdefault_valuer,   
exceptionsZ	NoDefaultanyrC   ConstClassDefZTupleZListtuplerK   r+   rM   )original
overriddenall_argsZoriginal_param_namesZ
param_nameZoriginal_defaultZoverridden_defaultZdefault_listZastroid_type_compared_attrZhandled_typesoriginal_typer6   )rO   r7   '_has_different_parameters_default_value   sB    	





rZ   c             C   sd   t | |}xT|D ]L\}}||f}t|s,dS dd |D }tt|j|rLq|j|jkrdS qW dS )NTc             S   s   g | ]
}|j qS r6   )r/   )r:   rN   r6   r6   r7   r;   	  s    z-_has_different_parameters.<locals>.<listcomp>F)r   rC   rR   mapmatchr/   )rV   rW   dummy_parameter_regexZzippedZoriginal_paramZoverridden_paramparamsnamesr6   r6   r7   _has_different_parameters  s    
r`   c       	      C   s   t | }t |}t|||}t| jj|jj|}| jtkrBd }}tdd | jj|jjfD dk}tdd | jj|jjfD dk}t	||||fS )aE  Determine if the two methods have different parameters

    They are considered to have different parameters if:

       * they have different positional parameters, including different names

       * one of the methods is having variadics, while the other is not

       * they have different keyword only parameters.

    Fc             s   s   | ]}|sd V  qdS )rG   Nr6   )r:   rN   r6   r6   r7   rA   2  s    z(_different_parameters.<locals>.<genexpr>rG   c             s   s   | ]}|sd V  qdS )rG   Nr6   )r:   rN   r6   r6   r7   rA   6  s    )
rI   r`   r0   r>   r/   r   sumr<   r=   rR   )	rV   rW   r]   Zoriginal_parametersZoverridden_parametersZdifferent_positionalZdifferent_kwonlyZdifferent_kwargZdifferent_varargr6   r6   r7   _different_parameters  s    

rb   c             C   s   | j tkot| S )N)r/   INVALID_BASE_CLASSESr   )clsr6   r6   r7   _is_invalid_base_class?  s    re   c          
   C   s   |  |}x||D ]t}yXxR| D ]F}t|tjr y| d | d W n tjk
r`   w Y q X dS q W W q tjk
r   dS X qW dS )N__get____set__TF)rL   inferr+   r,   InstanceNotFoundErrorInferenceError)rd   attr
attributesZ	attributeinferredr6   r6   r7   _has_data_descriptorC  s    



ro   c       	      C   s   t | tjsdS x|D ]}y||}W n tjk
r@   wY nX x|D ]}xz|tjD ]j}yt|j	 }W n tj
tfk
r   wZY nX t |tjsqZ|j}t |tjr|j}|j| jkrZdS qZW qHW qW dS )z Check if the func was called in any of the given methods,
    belonging to the *klass*. Returns True if so, False otherwise.
    FT)r+   r,   FunctionDefrL   rj   nodes_of_classCallr!   funcrh   rk   StopIterationZBoundMethod_proxiedZUnboundMethodr/   )	rs   klassmethodsrF   inferedZinfer_methodr1   ZboundZfunc_objr6   r6   r7   _called_in_methodsV  s*    

ry   c          	   C   s   y| | }W n tjk
r$   dS X dt}xj|D ]b}|tjkrFq6yt| }W n tjk
rn   w6Y nX t	|tj
rt|rdS | |kr6dS q6W dS )a4   Check if the given attribute *name* is a property
    in the given *klass*.

    It will look for `property` calls or for functions
    with the given name, decorated by `property` or `property`
    subclasses.
    Returns ``True`` if the name is a property in the given klass,
    ``False`` otherwise.
    Fz{}.propertyT)rL   r,   rj   formatr   Uninferabler!   rh   rk   r+   rp   r   Zpytype)r/   rv   rm   Zproperty_namerl   rx   r6   r6   r7   _is_attribute_propertyq  s$    


r|   c             C   s@   x:|  tjD ]*}|j}t|tjr|jdkr|jsdS qW dS )NsuperTF)rq   r,   rr   rs   r+   r-   r/   r0   )Zfundef_noder1   rs   r6   r6   r7   _has_bare_super_call  s
    r~   c             C   s~   y| j ||d}t|}W n( tjk
r0   dS  tk
rB   dS X yt| dS  tjk
rf   dS  tk
rx   |S X dS )z
    Safely infer the return value of a function.

    Returns None if inference failed or if there is some ambiguity (more than
    one node has been inferred). Otherwise returns infered value.
    )contextN)infer_call_resultr!   r,   rk   rt   )rJ   Zcallerr   Zinferitr*   r6   r6   r7   _safe_infer_call_result  s    r   c             C   s@   t | }t|tjr<| }tdd t| |D r<dS dS )Nc             s   s&   | ]\}}|o|o|j |j kV  qd S )N)r*   )r:   Z
first_slotZsecond_slotr6   r6   r7   rA     s   z)_has_same_layout_slots.<locals>.<genexpr>TF)r!   rh   r+   r,   rT   slotsrC   r   )r   Zassigned_valuern   Zother_slotsr6   r6   r7   _has_same_layout_slots  s    r   )z+Unable to check methods signature (%s / %s)zmethod-check-failedzUsed when Pylint has been unable to check methods signature compatibility for an unexpected reason. Please report this kind if you don't make sense of it.)z4An attribute defined in %s line %s hides this methodzmethod-hiddenz~Used when a class defines a method which is hidden by an instance attribute from an ancestor class or set by some client code.)z1Access to member %r before its definition line %szaccess-member-before-definitionzGUsed when an instance member is accessed before it's actually assigned.)z%Attribute %r defined outside __init__zattribute-defined-outside-initzGUsed when an instance attribute is defined outside the __init__ method.)z1Access to a protected member %s of a client classzprotected-accesszUsed when a protected member (i.e. class member with a name beginning with an underscore) is access outside the class or a descendant of the class where it's defined.)zMethod has no argumentzno-method-argumentzbUsed when a method which should have the bound instance as first argument has no argument defined.)z+Method should have "self" as first argumentzno-self-argumentzUsed when a method has an attribute different the "self" as first argument. This is considered as an error since this is a so common convention that you shouldn't break it!)z0Class method %s should have %s as first argumentzbad-classmethod-argumentzUsed when a class method has a first argument named differently than the value specified in valid-classmethod-first-arg option (default to "cls"), recommended to easily differentiate them from regular instance methods.)z4Metaclass method %s should have %s as first argumentzbad-mcs-method-argumentzUsed when a metaclass method has a first argument named differently than the value specified in valid-classmethod-first-arg option (default to "cls"), recommended to easily differentiate them from regular instance methods.)z:Metaclass class method %s should have %s as first argumentzbad-mcs-classmethod-argumentzUsed when a metaclass class method has a first argument named differently than the value specified in valid-metaclass-classmethod-first-arg option (default to "mcs"), recommended to easily differentiate them from regular instance methods.)z'Static method with %r as first argumentzbad-staticmethod-argumentzUsed when a static method has "self" or a value specified in valid-classmethod-first-arg option or valid-metaclass-classmethod-first-arg option as first argument.)zMethod could be a functionzno-self-usezYUsed when a method doesn't use its bound instance, and so could be written as a function.)z#Parameters differ from %s %r methodzarguments-differztUsed when a method has a different number of arguments than in the implemented interface or in an overridden method.)z#Signature differs from %s %r methodzsignature-differszgUsed when a method signature is different than in the implemented interface or in an overridden method.)z7Method %r is abstract in class %r but is not overriddenzabstract-methodzbUsed when an abstract method (i.e. raise NotImplementedError) is not overridden in concrete class.)z0__init__ method from base class %r is not calledzsuper-init-not-calledzaUsed when an ancestor class method has an __init__ method which is not called by a derived class.)zClass has no __init__ methodzno-initzEUsed when a class has no __init__ method, neither its parent classes.)z9__init__ method from a non direct base class %r is calledznon-parent-init-calledznUsed when an __init__ method is called on a class which is not in the direct ancestors for the analysed class.)z%Useless super delegation in method %rzuseless-super-delegationzUsed whenever we can detect that an overridden method is useless, relying on super() delegation to do the same thing as another method from the MRO.)zCInvalid object %r in __slots__, must contain only non empty stringszinvalid-slots-objectz=Used when an invalid (non-string) object occurs in __slots__.)z4Assigning to attribute %r not defined in class slotszassigning-non-slotzCUsed when assigning to an attribute not defined in the class slots.)zInvalid __slots__ objectzinvalid-slotszhUsed when an invalid __slots__ is found in class. Only a string, an iterable or a sequence is permitted.)z$Inheriting %r, which is not a class.zinherit-non-classz?Used when a class inherits from something which is not a class.)z1Inconsistent method resolution order for class %rzinconsistent-mroz>Used when a class has an inconsistent method resolution order.)zDuplicate bases for class %rzduplicate-basesz&Used when a class has duplicate bases.)z9Consider using a decorator instead of calling classmethodzno-classmethod-decoratorzGUsed when a class method is defined without using the decorator syntax.)z:Consider using a decorator instead of calling staticmethodzno-staticmethod-decoratorzHUsed when a static method is defined without using the decorator syntax.)z/Class __slots__ should be a non-string iterablezsingle-string-used-for-slotszHUsed when a class __slots__ is a simple string, rather than an iterable.)zJClass %r inherits from object, can be safely removed from bases in python3zuseless-object-inheritancezoUsed when a class inherit from object, which under python3 is implicit, hence can be safely removed from bases.)ZF0202ZE0202ZE0203ZW0201ZW0212ZE0211ZE0213ZC0202ZC0203ZC0204ZW0211ZR0201ZW0221ZW0222ZW0223ZW0231ZW0232ZW0233ZW0235ZE0236ZE0237ZE0238ZE0239ZE0240ZE0241ZR0202ZR0203ZC0205ZR0205c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ScopeAccessMapz'Store the accessed variables per scope.c             C   s   t dd | _d S )Nc               S   s
   t tS )N)collectionsdefaultdictlistr6   r6   r6   r7   <lambda>s  s    z)ScopeAccessMap.__init__.<locals>.<lambda>)r   r   _scopes)r9   r6   r6   r7   __init__r  s    zScopeAccessMap.__init__c             C   s.   t |}|dkrdS | j| |j | dS )zSet the given node as accessed.N)r   r   attrnamer.   )r9   rJ   framer6   r6   r7   set_accessedu  s    zScopeAccessMap.set_accessedc             C   s   | j |i S )z/Get the accessed variables for the given scope.)r   get)r9   scoper6   r6   r7   accessed~  s    zScopeAccessMap.accessedN)__name__
__module____qualname____doc__r   r   r   r6   r6   r6   r7   r   o  s   	r   c            	   @   sf  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fZ
dQddZejdd Zejdd Zdd Zdd Zdd  Zd!d" Zd#d$ ZeZd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zed3d4d5d6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dRdCdDZ$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dS )SClassCheckerzchecks for :
    * methods without self as first argument
    * overridden methods signature
    * access only to existent members via self
    * attributes not defined in the __init__ method
    * unreachable code
    classeszdefining-attr-methods)r   __new__ZsetUpZcsvz<method names>zGList of method names used to declare (i.e. assign) instance attributes.)defaultrH   metavarhelpzvalid-classmethod-first-arg)rd   z<argument names>z=List of valid names for the first argument in a class method.z%valid-metaclass-classmethod-first-argzGList of valid names for the first argument in a metaclass class method.zexclude-protected)_asdict_fields_replaceZ_source_makez<protected access exclusions>zQList of member names, which should be excluded from the protected access warning.Nc             C   s$   t | | t | _g | _d | _d S )N)r   r   r   	_accessed_first_attrs_meth_could_be_func)r9   linterr6   r6   r7   r     s    zClassChecker.__init__c             C   s   t | dd dS )Nzdummy-variables-rgx)r   )r   )r9   r6   r6   r7   
_dummy_rgx  s    zClassChecker._dummy_rgxc             C   s   t | dddS )Nzignore-mixin-membersT)r   )r   )r9   r6   r6   r7   _ignore_mixin  s    zClassChecker._ignore_mixinc             C   st   |  | |jdkrRt|rRy|d W n& tjk
rP   | jd||d Y nX | | | | | 	| dS )z&init visit variable _accessed
        classr   zno-init)r0   rJ   N)
_check_bases_classesrH   r   
local_attrr,   rj   add_message_check_slots_check_proper_bases_check_consistent_mro)r9   rJ   r6   r6   r7   visit_classdef  s    


zClassChecker.visit_classdefc             C   sn   y|   W n\ tk
r2   | jd|j|d Y n8 tk
rV   | jd|j|d Y n tk
rh   Y nX dS )z<Detect that a class has a consistent mro or duplicate bases.zinconsistent-mro)r0   rJ   zduplicate-basesN)mror   r   r/   r   NotImplementedError)r9   rJ   r6   r6   r7   r     s    z"ClassChecker._check_consistent_mroc             C   s   x|j D ]}t|}|tjdfkr$qt|tjrB|dtf rBqt|tjrVt	|rj| j
d| |d |jtjkr| j
d|j|d qW dS )z`
        Detect that a class inherits something which is not
        a class or a type.
        Nz%s.typezinherit-non-class)r0   rJ   zuseless-object-inheritance)basesr   r,   r{   r+   ri   is_subtype_ofr   rT   re   r   	as_stringr/   rP   r   )r9   rJ   baseancestorr6   r6   r7   r     s    z ClassChecker._check_proper_basesc          
      s`  | j r |jdd  dkr dS | j|}|jdkrB| || | jdsRdS | j	j
|  x|j D ]\}} fdd|D }|sqntfdd	|D rqnx||D ]4}d
}x$|j| D ]}| jkrd}qW |rP qW y|| W qn tjk
rV   xD|D ]<}| jkrt| |r<q| jd||d qW Y qnX qnW dS )zclose a class node:
        check that instance attributes are defined in __init__ and check
        access to existent members
        NZmixin	metaclasszattribute-defined-outside-initc                s2   g | ]*}t | tjtjfs|  kr|qS r6   )r+   	statementr,   ZDelete	AugAssignroot)r:   n)current_moduler6   r7   r;      s   z/ClassChecker.leave_classdef.<locals>.<listcomp>c             3   s   | ]}|  j kV  qd S )N)r   r/   )r:   rJ   )defining_methodsr6   r7   rA   (  s    z.ClassChecker.leave_classdef.<locals>.<genexpr>FT)r0   rJ   )r   r/   lowerr   r   rH   _check_accessed_membersr   is_message_enabledconfigZdefining_attr_methodsr   Zinstance_attrsitemsrR   instance_attr_ancestorsr   r   r,   rj   ry   r   )r9   Zcnoder   rl   nodesparentZattr_definedrJ   r6   )r   r   r7   leave_classdef  sB    


zClassChecker.leave_classdefc       	   	   C   s0  |  sdS | | |j }d| _| ||jdk |jdkrP| | dS xZ|	|jD ]J}y||j }W n t
k
r   w^Y nX t|tjsq^| ||d| P q^W |jrx|jjD ]}t|tjr|jdkrdS t|tjr|jdkrdS t|}|sdS t|tjrByt||}W n tjk
r@   dS X y4t|tjtjfrt|drt|d	rtdS W q tjk
r   Y qX qW y~||jd
 }| }t|tjr|jdkr|j }t|tjr|| r| j|j f}| j!d||d W n tj"k
r*   Y nX dS )z"check method arguments, overridingNTr   r   rW   )gettersetterdeleterpropertyrf   rg   r   rF   zmethod-hidden)r0   rJ   )#	is_method_check_useless_super_delegationr   r   r   _check_first_arg_for_typerH   r/   _check_initlocal_attr_ancestorsKeyErrorr+   r,   rp   _check_signaturer	   r   	Attributer   r-   r   r!   r   rk   ri   rT   rL   ZAttributeInferenceErrorinstance_attrr   Zqnamer   
fromlinenor   rj   )	r9   rJ   rv   rW   	meth_node	decoratorrn   Zoverridden_framer0   r6   r6   r7   visit_functiondefG  sj    






zClassChecker.visit_functiondefc          	   C   s  |  r|jrdS |j}t|dkr(dS |d }t|tjtjfsFdS |j}t|tj	rft|j
tjsjdS yt|j
j }W n tjk
r   dS X t|tjsdS |j
j|jkrdS |j }|j|kst|jtjr|jj|jkrdS |j }d}xd||jD ]T}	y|	|j }W n tk
r4   wY nX t|tjrTt|j|jrXdS P qW t|j}
t |}|dk	rdd }||jj!}||jj!}|r|r||krdS t"|
|r| j#d||jfd dS )aQ  Check if the given function node is an useless method override

        We consider it *useless* if it uses the super() builtin, but having
        nothing additional whatsoever than not implementing the method at all.
        If the method uses super() to delegate an operation to the rest of the MRO,
        and if the method called is the same as the current one, the arguments
        passed to super() are the same as the parameters that were passed to
        this method, then the method could be removed altogether, by letting
        other implementation to take precedence.
        NrG   r   c             S   s   dd t d | D S )Nc             S   s   g | ]}|  qS r6   )r   )r:   Z
annotationr6   r6   r7   r;     s    zZClassChecker._check_useless_super_delegation.<locals>.form_annotations.<locals>.<listcomp>)filter)annotationsr6   r6   r7   form_annotations  s    zFClassChecker._check_useless_super_delegation.<locals>.form_annotationszuseless-super-delegation)rJ   r0   )$r   r	   bodylenr+   r,   ZExprZReturnr*   rr   rs   r   r!   exprrh   rk   r
   Superr   r/   r   r   Zmro_pointerrH   ri   r   r   r   rp   rZ   r0   r?   r8   r   rD   r   )r9   Zfunctionr   r   r1   Z
super_callZcurrent_scoperv   r   rW   r^   r0   r   Zcalled_annotationsZoverridden_annotationsr6   r6   r7   r     sd    






z,ClassChecker._check_useless_super_delegationc          
   C   s   d|j krd S x|dD ]}|tjkr*qt|sJt|sJ| jd|d qt|tjrf| jd|d qt	|dsrqt|tj
rdd |jD }n| }|tjkrd S x4|D ],}y| | W q tjk
r   wY qX qW qW d S )N	__slots__zinvalid-slots)rJ   zsingle-string-used-for-slotsiteredc             S   s   g | ]}|d  qS )r   r6   )r:   itemr6   r6   r7   r;     s    z-ClassChecker._check_slots.<locals>.<listcomp>)localsigetattrr,   r{   r   r   r   r+   rS   hasattrZDictr   r   _check_slots_eltrk   )r9   rJ   r   valueseltr6   r6   r7   r     s.    




zClassChecker._check_slotsc             C   sj   xd|  D ]X}|tjkrq
t|tjr2t|jtsH| jd| |d q
|js
| jd| |d q
W d S )Nzinvalid-slots-object)r0   rJ   )	rh   r,   r{   r+   rS   r*   strr   r   )r9   r   rx   r6   r6   r7   r   $  s    

zClassChecker._check_slots_eltc             C   s   |  r|jjdk	r| j  | jds.dS |j }| jr|j	dkr|j
tkr| st||j
st|st|s| jd|d dS )zon method node, check if this method couldn't be a function

        ignore class, static and abstract methods, initializer,
        methods overridden from a parent class.
        Nzno-self-userF   )rJ   )r   r0   r   popr   r   r   r   r   rH   r/   r   is_abstractr   r   r~   r   )r9   rJ   Z
class_noder6   r6   r7   leave_functiondef4  s    



zClassChecker.leave_functiondefc             C   s8   |  |r| j| dS | jds*dS | | dS )zcheck if the getattr is an access to a class member
        if so, register it. Also check for access to protected
        class member from outside its class (but ignore __special__
        methods)
        Nzprotected-access)_uses_mandatory_method_paramr   r   r   r   !_check_protected_attribute_access)r9   rJ   r6   r6   r7   visit_attributeM  s    
zClassChecker.visit_attributec             C   s4   t | tjr&| |r&| j| | | d S )N)r+   Zassign_typer,   r   r   r   r   _check_in_slots)r9   rJ   r6   r6   r7   visit_assignattr\  s
    
zClassChecker.visit_assignattrc                s   t  j}|rt|tjr|j}d|jks0|js4dS | }|dkrHdS t	dd |
 D rbdS t	 fdd|D st	dd |D st j|rdS  j|jkrt| jrdS  jdkrt| jjrdS | jd jf d	 dS )
zU Check that the given AssignAttr node
        is defined in the class slots.
        r   Nc             s   s"   | ]}d |j ko|jdkV  qdS )r   rP   N)r   r/   )r:   r   r6   r6   r7   rA   s  s   z/ClassChecker._check_in_slots.<locals>.<genexpr>c             3   s   | ]}|j  jkV  qd S )N)r*   r   )r:   slot)rJ   r6   r7   rA   x  s    c             s   s   | ]}|j d kV  qdS )__dict__N)r*   )r:   r   r6   r6   r7   rA   {  s    	__class__zassigning-non-slot)r0   rJ   )r   r   r+   r,   ri   ru   r   Znewstyler   rR   	ancestorsr|   r   ro   r   r   r*   r   )r9   rJ   rx   rv   r   r6   )rJ   r7   r   c  s0    

zClassChecker._check_in_slotszprotected-accesszno-classmethod-decoratorzno-staticmethod-decoratorc             C   s@   |  | |jd }t|tjs$d S | |r2d S | | d S )Nr   )_check_classmethod_declarationtargetsr+   r,   Z
AssignAttrr   r   )r9   Zassign_noderJ   r6   r6   r7   visit_assign  s    


zClassChecker.visit_assignc                s   t |jtjsdS |jj}t |tjr0|jdkr4dS |jdkrBdnd}| }t |tjs^dS |jj	d }t |tjszdS |j t
 fdd| D r| j||jd d	 dS )
a[  Checks for uses of classmethod() or staticmethod()

        When a @classmethod or @staticmethod decorator should be used instead.
        A message will be emitted only if the assignment is at a class scope
        and only if the classmethod's argument belongs to the class where it
        is defined.
        `node` is an assign node.
        N)rE   staticmethodrE   zno-classmethod-decoratorzno-staticmethod-decoratorr   c             3   s   | ]} |j kV  qd S )N)r/   )r:   member)method_namer6   r7   rA     s    z>ClassChecker._check_classmethod_declaration.<locals>.<genexpr>)rJ   )r+   r*   r,   rr   rs   r-   r/   r   rT   r0   rR   Z	mymethodsr   r   )r9   rJ   rs   msgZparent_classZclassmeth_argr6   )r   r7   r     s"    	z+ClassChecker._check_classmethod_declarationc             C   s   |j }t|r|| jjkrt|}|j }|dkrH| jd||d dS t|jt	j
rxt|jjt	jrx|jjjdkrxdS | |jrdS ||jks||jks|j }t|t	jrt|jdkrt|jd t	jr|jd j}t||rdS | jd||d dS )a  Given an attribute access node (set or get), check if attribute
        access is legitimate. Call _check_first_attr with node before calling
        this method. Valid cases are:
        * self._attr in a method or cls._attr in a classmethod. Checked by
        _check_first_attr.
        * Klass._attr inside "Klass" class.
        * Klass2._attr inside "Klass" class when Klass2 is a base class of
            Klass.
        Nzprotected-access)rJ   r0   r}   rG   r   )r   r   r   Zexclude_protectedr   r   r   r   r+   r,   rr   rs   r-   r/   _is_type_self_callZ	basenamesr   r   ZAssignr   r   Z
AssignNamer|   )r9   rJ   r   rv   Zcalleestmtr/   r6   r6   r7   r     s.    



z.ClassChecker._check_protected_attribute_accessc             C   sD   t |tjoBt |jtjoB|jjdkoBt|jdkoB| |jd S )NrH   rG   r   )	r+   r,   rr   rs   r-   r/   r   r0   _is_mandatory_method_param)r9   r   r6   r6   r7   r     s
    zClassChecker._is_type_self_callc             C   s*   | j r&|j| j d ks | j d s&d| _dS )zXcheck if the name handle an access to a class member
        if so, register it
        FN)r   r/   r   )r9   rJ   r6   r6   r7   
visit_name  s    zClassChecker.visit_namec       
   	      s@  d}x4|  D ]&\} y|| wW n tjk
r@   Y nX yt|| wW n tk
rj   Y nX y||}W n tjk
r   Y qX  fdd|D }|sq|d  fddt	|D }t
|dkr|d }| }|j}xH D ]@}	|	 |kr|	j|k rt|	 ||s| jd|	||fd qW qW d	S )
z'check that accessed members are defined)AttributeError	ExceptionBaseExceptionc                s   g | ]}| kr|qS r6   r6   )r:   r   )r   r6   r7   r;   +  s    z8ClassChecker._check_accessed_members.<locals>.<listcomp>r   c                s(   g | ] \}}|d ks |   k	r|qS )r   )r   )r:   ir   )r   r6   r7   r;   4  s   rG   zaccess-member-before-definition)rJ   r0   N)r   r   r,   rj   r!   r   rt   r   r   	enumerater   r   r   Zare_exclusiver   r   )
r9   rJ   r   Zexcsrl   ZdefstmtsZdefstmtr   ZlnoZ_noder6   )r   r   r7   r     sF    



z$ClassChecker._check_accessed_membersr   c             C   s*  |j j dkrdS |j j o"| d }| j| | jd }|jdkr|dksd|| jjksd|| jjkrx| jd||d dS d| jd< n|j j s| jd|d	 n|r|jd
kr| 	|| jj|d|j
 n| 	|| jj|d|j
 nH|jd
ks|j
dkr| 	|| jj|d|j
 n|dkr&| jd|d	 dS )al  check the name of first argument, expect:

        * 'self' for a regular method
        * 'cls' for a class method or a metaclass regular method (actually
          valid-classmethod-first-arg value)
        * 'mcs' for a metaclass class method (actually
          valid-metaclass-classmethod-first-arg)
        * not one of the above for a static method
        Nr   r   r   r9   zbad-staticmethod-argument)r0   rJ   zno-method-argument)rJ   rE   zbad-mcs-classmethod-argumentzbad-mcs-method-argument__class_getitem__zbad-classmethod-argumentzno-self-argument)r0   Zargnamesr   r.   rH   r   Zvalid_classmethod_first_argZ%valid_metaclass_classmethod_first_argr   _check_first_arg_configr/   )r9   rJ   r   Z	first_argfirstr6   r6   r7   r   N  sJ    






z&ClassChecker._check_first_arg_for_typec             C   sf   ||krbt |dkr"t|d }n,ddd |d d D }d||d f }| j|||f|d d S )	NrG   r   z, c             s   s   | ]}t |V  qd S )N)repr)r:   vr6   r6   r7   rA     s    z7ClassChecker._check_first_arg_config.<locals>.<genexpr>r   z%s or %r)r0   rJ   )r   r  joinr   )r9   r  r   rJ   messager   Zvalidr6   r6   r7   r    s    z$ClassChecker._check_first_arg_configc             C   sz   dd }t |rdS tt|| dd d}xF|D ]>\}}|j }||krPq4||jkr\q4| jd|||jfd q4W dS )	z^check that the given class node implements abstract methods from
        base classes
        c             S   s   | j ddS )NF)Zpass_is_abstract)r   )rF   r6   r6   r7   r     s    z6ClassChecker._check_bases_classes.<locals>.is_abstractNc             S   s   | d S )Nr   r6   )r   r6   r6   r7   r     s    z3ClassChecker._check_bases_classes.<locals>.<lambda>)keyzabstract-method)rJ   r0   )	r   sortedr   r   r   r   r   r   r/   )r9   rJ   r   rw   r/   rF   ownerr6   r6   r7   r     s    

z!ClassChecker._check_bases_classesc       
   
   C   s  | j ds| j dsdS |j }t|}t|}x&|tjD ]}|j	}t
|tjrF|jdkrjqFt
|jtjrt
|jj	tjr|jj	jdkrdS yx|j D ]}|tjkrqt
|tjrt
|jtjrt|jr|jjdkrdS t
|tjrdS y
||= W q tk
r8   ||kr4| jd||jd Y qX qW W qF tjk
rZ   wFY qFX qFW xR| D ]F\}}t|}	|jdksj|	r|	jdkrqj| jd|j|d qjW dS )	zWcheck that the __init__ method call super or ancestors'__init__
        method
        zsuper-init-not-calledznon-parent-init-calledNr   r}   )rJ   r0   rP   )r0   rJ   )r   r   r   r   _ancestors_to_calldictrq   r,   rr   rs   r+   r   r   r   r-   r/   rh   r{   ri   ru   rT   r   r
   r   r   r   rk   r   r   )
r9   rJ   
klass_nodeto_callZnot_called_yetr   r   rv   rF   rd   r6   r6   r7   r     sN    

	


zClassChecker._check_initc             C   s  t |tjrt |tjs0| jd||f|d dS | }t||}t||}|jjdksd|jjdkrhdS t|jrvdS |j	rx*|j	j
D ]}t |tjr|jdkrdS qW t||| jdr| jd||jf|d n.t|jjt|jjk r| jd||jf|d dS )z@check that the signature of the two given methods match
        zmethod-check-failed)r0   rJ   Nr   )r]   zarguments-differzsignature-differs)r+   r,   rp   r   Zinstantiate_classr   r0   r   r/   r	   r   r   r   rb   r   r   defaults)r9   Zmethod1Z	refmethodZ
class_typerd   instancer   r6   r6   r7   r     s0    



zClassChecker._check_signaturec             C   s   |  |jS )zCheck that attribute lookup name use first attribute variable name

        Name is `self` for method, `cls` for classmethod and `mcs` for metaclass.
        )r   r   )r9   rJ   r6   r6   r7   r     s    z)ClassChecker._uses_mandatory_method_paramc             C   s"   | j o t|tjo |j| j d kS )zCheck if astroid.Name corresponds to first attribute variable name

        Name is `self` for method, `cls` for classmethod and `mcs` for metaclass.
        r   )r   r+   r,   r-   r/   )r9   rJ   r6   r6   r7   r     s    z'ClassChecker._is_mandatory_method_param)N)r   )+r   r   r   r   r   __implements__r/   MSGSmsgspriorityZoptionsr   r	   Zcachedpropertyr   r   r   r   r   r   r   visit_asyncfunctiondefr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   r6   r6   r6   r7   r     sn   
?Qj *'>		=
@	9,r   c               @   s   e Zd ZdZefZdZddde dddgifd	d
dddgifdddi fdZdZ	e
d
dddd ZeZdd Zedd Zdd Zdd ZdS )SpecialMethodsCheckerzOChecker which verifies that special methods
    are implemented correctly.
    r   z__iter__ returns non-iteratorznon-iterator-returnedzaUsed when an __iter__ method returns something which is not an iterable (i.e. has no `%s` method)Z	old_names)ZW0234znon-iterator-returned)ZE0234znon-iterator-returnedz6The special method %r expects %s param(s), %d %s givenz#unexpected-special-method-signaturezEmitted when a special method was defined with an invalid number of parameters. If it has too few or too many, it might not work at all.)ZE0235zbad-context-managerz,__len__ does not return non-negative integerzinvalid-length-returnedzPUsed when a __len__ method returns something which is not a non-negative integer)ZE0301ZE0302ZE0303r   c             C   sL   |  sd S |jdkr | | |jdkr4| | |jtkrH| | d S )N__iter____len__)r   r/   _check_iter
_check_lenr   "_check_unexpected_method_signature)r9   rJ   r6   r6   r7   r   P  s    




z'SpecialMethodsChecker.visit_functiondefc       
      C   s  t |j }|d krd S |jjs*|jjs*d S t|td grD|jj}n|jjdd  }t|t|jj }t|jj}|| }t|t	r||k}d| }nB|| }|dkrd}n,|dk rd}n|dkr|| dkp|jj }|r
|dkrdnd}	| j
d	|j|||	f|d
 d S )Nz.staticmethodrG   zbetween %d or %dr   FTZwasZwerez#unexpected-special-method-signature)r0   rJ   )r   r/   r0   r=   r   r   r   r  r+   rU   r   )
r9   rJ   Zexpected_paramsrX   Z	mandatoryZoptionalZcurrent_paramsemitrestZverbr6   r6   r7   r  a  s6    



z8SpecialMethodsChecker._check_unexpected_method_signaturec             C   s   | t jkrdS t| trdS t| t jrPy| t dS  t jk
rL   Y qX nJt| t jr| 	 }|rt|t jry|t dS  t jk
r   Y nX dS )NTF)
r,   r{   r+   r   ri   r   NEXT_METHODrj   rT   r   )rJ   r   r6   r6   r7   _is_iterator  s&    



z"SpecialMethodsChecker._is_iteratorc             C   s.   t ||}|d k	r*| |s*| jd|d d S )Nznon-iterator-returned)rJ   )r   r#  r   )r9   rJ   rx   r6   r6   r7   r    s    

z!SpecialMethodsChecker._check_iterc             C   s   t ||}|r|tjkrd S t|tjrB|jdkrBt|tjsBd S t|tjs`| jd|d d S |j}t|t	rx|dk r| jd|d d S )Nintzinvalid-length-returned)rJ   r   )
r   r,   r{   r+   ri   r/   rS   r   r*   r$  )r9   rJ   rn   r*   r6   r6   r7   r    s    

z SpecialMethodsChecker._check_lenN)r   r   r   r   r   r  r/   r"  r  r  r   r   r  r  r   r#  r  r  r6   r6   r6   r7   r  *  s8   

/r  r   c          	   C   sN   i }xD| j ddD ]4}yt||||< W q tjk
rD   wY qX qW |S )zreturn a dictionary where keys are the list of base classes providing
    the queried method, and so that should/may be called from the method node
    F)Zrecurs)r   r!   r   r,   rk   )r  rF   r  Z	base_noder6   r6   r7   r    s    r  c             C   s    |  t|  |  t|  dS )z.required method to auto register this checker N)Zregister_checkerr   r  )r   r6   r6   r7   register  s    r%  )N)r   )Fr   Z
__future__r   r   	itertoolsr   r   sysr,   Zastroid.basesr   r   Zastroid.exceptionsr   r   r	   r
   Zastroid.scoped_nodesr   Zpylint.interfacesr   Zpylint.checkersr   Zpylint.checkers.utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zpylint.utilsr   version_infor"  rc   
namedtupler&   r'   r8   r?   rD   rI   rK   rM   rZ   r`   rb   re   ro   ry   r|   r~   r   r   r  r   r   r  r  r%  r6   r6   r6   r7   <module>   s   H
(9. 
       . 
