
    e;                        d Z ddlm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mZmZmZmZmZ dd
lmZ dZ	 d	 	 	 	 	 	 	 ddZ G d de      Zy)z5Special methods checker and helper function's module.    )annotations)CallableN)basesnodesutil)InferenceContext)InferenceResult)BaseChecker)	PYMETHODSSPECIAL_METHODS_PARAMSdecorated_withis_function_body_ellipsisonly_required_for_messages
safe_infer)PyLinter__next__c                    	 | j                  ||      }t        |      }	 t        |       y# t        j                  $ r Y yt        $ r Y yw xY w# t        j                  $ r Y yt        $ r |cY S w xY w)zSafely 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 inferred value.
    )contextN)infer_call_resultnextastroidInferenceErrorStopIteration)nodecallerr   inferitvalues        Q/usr/lib/python3/dist-packages/pylint/checkers/classes/special_methods_checker.py_safe_infer_call_resultr      s    (((AW
W !!  
 !!  s,   - A AAAA2%
A21A2c                      e Zd ZdZdZddde dddd	gifd
ddddgifddddddddddddZd< fdZ edddddddd d!d"d#d$d%      d=d&       Z	e	Z
d=d'Zed>d(       Zed?d)       Zed?d*       Zed?d+       Zed?d,       Zed?d-       Zed?d.       Zed?d/       Zd@d0Zd@d1Zd@d2Zd@d3Zd@d4Zd@d5Zd@d6Zd@d7Z	 	 	 	 	 	 d@d8Zd@d9Z	 	 	 	 	 	 d@d:Z	 	 	 	 	 	 d@d;Z  xZ!S )ASpecialMethodsCheckerzOChecker which verifies that special methods
    are implemented correctly.
    classesz__iter__ returns non-iteratornon-iterator-returnedzVUsed when an __iter__ method returns something which is not an iterable (i.e. has no `z	` method)	old_names)W0234zold-non-iterator-returned-1)E0234zold-non-iterator-returned-2z6The special method %r expects %s param(s), %d %s given#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.)E0235zbad-context-manager)z,__len__ does not return non-negative integerinvalid-length-returnedzPUsed when a __len__ method returns something which is not a non-negative integer)z__bool__ does not return boolinvalid-bool-returnedzAUsed when a __bool__ method returns something which is not a bool)z__index__ does not return intinvalid-index-returnedzGUsed when an __index__ method returns something which is not an integer)z__repr__ does not return strinvalid-repr-returnedzCUsed when a __repr__ method returns something which is not a string)z__str__ does not return strinvalid-str-returnedzBUsed when a __str__ method returns something which is not a string)z__bytes__ does not return bytesinvalid-bytes-returnedzAUsed when a __bytes__ method returns something which is not bytes)z__hash__ does not return intinvalid-hash-returnedzEUsed when a __hash__ method returns something which is not an integer)z4__length_hint__ does not return non-negative integerinvalid-length-hint-returnedzXUsed when a __length_hint__ method returns something which is not a non-negative integer)z__format__ does not return strinvalid-format-returnedzEUsed when a __format__ method returns something which is not a string)z&__getnewargs__ does not return a tupleinvalid-getnewargs-returnedzHUsed when a __getnewargs__ method returns something which is not a tuple)zB__getnewargs_ex__ does not return a tuple containing (tuple, dict)invalid-getnewargs-ex-returnedzbUsed when a __getnewargs_ex__ method returns something which is not of the form tuple(tuple, dict))E0301E0302E0303E0304E0305E0306E0307E0308E0309E0310E0311E0312E0313c                <   t         |   |       | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  d| _        y )N)__iter____len____bool__	__index____repr____str__	__bytes____hash____length_hint__
__format____getnewargs____getnewargs_ex__)super__init___check_iter
_check_len_check_bool_check_index_check_repr
_check_str_check_bytes_check_hash_check_length_hint_check_format_check_getnewargs_check_getnewargs_ex_protocol_map)selflinter	__class__s     r   rO   zSpecialMethodsChecker.__init__   s      ((((**((**((#66,,"44!%!:!:
 	    r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   c                   |j                         sy t        ||      }|rC|j                  | j                  v r+t	        |      s  | j                  |j                     ||       |j                  t
        v r| j                  |       y y N)	is_methodr   namer\   r   r   "_check_unexpected_method_signaturer]   r   inferreds      r   visit_functiondefz'SpecialMethodsChecker.visit_functiondef   sv      ~~*46 		T///-d3)Dtyy)$999	!33D9 "r`   c                   t         |j                     }|y |j                  j                  s|j                  j                  sy t	        |dg      r|j                  j                  }n|j                  j                  dd  }t        |      t        |j                  j                        z
  }t        |j                  j                        }||z   }d}t        |t              r||v}d|d    d|d    }n;||z
  }|dk(  rd}n.|dk  rd}n&|dkD  r!||z
  dk\  xs |j                  j                   }|r,|dk  rdnd	}	| j                  d
|j                  |||	f|       y y )Nzbuiltins.staticmethod   Fzbetween r   z or Twaswerer'   )argsr   )
r   rd   rm   varargr   lendefaults
isinstancetupleadd_message)
r]   r   expected_paramsall_args	mandatoryoptionalcurrent_paramsemitrestverbs
             r   re   z8SpecialMethodsChecker._check_unexpected_method_signature   se   0;"yy~~dii&6&6 $!8 9:yy~~Hyy~~ab)HMC		(:(:$;;	tyy))*"X-ou- O3D !));(<DQRAS@TUO #Y.Dqy%_2Fdii6F6FG*a/5VD5ii.$G   r`   c                    t        | t        j                        xr, | j                  |k(  xr t        | t        j
                         S rb   )rq   r   Instancerd   r   Const)r   type_s     r   _is_wrapped_typez&SpecialMethodsChecker._is_wrapped_type   s>     tU^^, 2		U"2tU[[11	
r`   c                    t         j                  | d      ryt        | t        j                        xr t        | j
                  t              S )NintT)r!   r   rq   r   r~   r   r   r   s    r   _is_intzSpecialMethodsChecker._is_int   6     11$>$,LDJJ1LLr`   c                    t         j                  | d      ryt        | t        j                        xr t        | j
                  t              S )NstrT)r!   r   rq   r   r~   r   r   r   s    r   _is_strzSpecialMethodsChecker._is_str  r   r`   c                    t         j                  | d      ryt        | t        j                        xr t        | j
                  t              S )NboolT)r!   r   rq   r   r~   r   r   r   s    r   _is_boolzSpecialMethodsChecker._is_bool  6     11$?$,MDJJ1MMr`   c                    t         j                  | d      ryt        | t        j                        xr t        | j
                  t              S )NbytesT)r!   r   rq   r   r~   r   r   r   s    r   	_is_byteszSpecialMethodsChecker._is_bytes  6     11$@$,NDJJ1NNr`   c                    t         j                  | d      ryt        | t        j                        xr t        | j
                  t              S )Nrr   T)r!   r   rq   r   r~   r   rr   r   s    r   	_is_tuplezSpecialMethodsChecker._is_tuple  r   r`   c                    t         j                  | d      ryt        | t        j                        xr t        | j
                  t              S )NdictT)r!   r   rq   r   r~   r   r   r   s    r   _is_dictzSpecialMethodsChecker._is_dict"  r   r`   c                   t        | t        j                        ryt        | t        j                        ryt        | t        j
                        r	 | j                  t               yt        | t        j                        rC| j                         }|r1t        |t        j                        r	 |j                  t               yy# t        j                  $ r Y yw xY w# t        j                  $ r Y yw xY w)NTF)rq   r   	Generatorr   ComprehensionScoper}   
local_attrNEXT_METHODr   NotFoundErrorClassDef	metaclass)r   r   s     r   _is_iteratorz"SpecialMethodsChecker._is_iterator)  s    dEOO,dE445dENN+, enn-(IZ	5>>B((5  ((   ,, s$   C /C CCC54C5c                N    | j                  |      s| j                  d|       y y )Nr#   r   )r   rs   rf   s      r   rP   z!SpecialMethodsChecker._check_iterB  s)      *44@ +r`   c                    | j                  |      s| j                  d|       y t        |t        j                        r$|j
                  dk  r| j                  d|       y y y )Nr)   r   r   r   rs   rq   r   r~   r   rf   s      r   rQ   z SpecialMethodsChecker._check_lenF  sX    ||H%6TB%++.8>>A3E6TB 4F.r`   c                N    | j                  |      s| j                  d|       y y )Nr*   r   )r   rs   rf   s      r   rR   z!SpecialMethodsChecker._check_boolL  s'    }}X&44@ 'r`   c                N    | j                  |      s| j                  d|       y y )Nr+   r   r   rs   rf   s      r   rS   z"SpecialMethodsChecker._check_indexP  s'    ||H%5DA &r`   c                N    | j                  |      s| j                  d|       y y )Nr,   r   r   rs   rf   s      r   rT   z!SpecialMethodsChecker._check_reprT  '    ||H%44@ &r`   c                N    | j                  |      s| j                  d|       y y )Nr-   r   r   rf   s      r   rU   z SpecialMethodsChecker._check_strX  s'    ||H%3$? &r`   c                N    | j                  |      s| j                  d|       y y )Nr.   r   )r   rs   rf   s      r   rV   z"SpecialMethodsChecker._check_bytes\  s'    ~~h'5DA (r`   c                N    | j                  |      s| j                  d|       y y )Nr/   r   r   rf   s      r   rW   z!SpecialMethodsChecker._check_hash`  r   r`   c                    | j                  |      s| j                  d|       y t        |t        j                        r$|j
                  dk  r| j                  d|       y y y )Nr0   r   r   r   rf   s      r   rX   z(SpecialMethodsChecker._check_length_hintd  sZ     ||H%;$G%++.8>>A3E;$G 4F.r`   c                N    | j                  |      s| j                  d|       y y )Nr1   r   r   rf   s      r   rY   z#SpecialMethodsChecker._check_formatl  s'    ||H%6TB &r`   c                N    | j                  |      s| j                  d|       y y )Nr2   r   )r   rs   rf   s      r   rZ   z'SpecialMethodsChecker._check_getnewargsp  s)     ~~h':F (r`   c                   | j                  |      s| j                  d|       y t        |t        j                        sy d}t        |j                        dk7  rd}n|j                  d   | j                   f|j                  d   | j                  ffD ]T  \  }}t        |t        j                        rt        |      }|s.t        |t        j                        rI ||      rRd} n |r| j                  d|       y y )Nr3   r   F   Tr   rj   )r   rs   rq   r   Tuplero   eltsr   Callr   r   UninferableBase)r]   r   rg   found_errorargchecks         r   r[   z*SpecialMethodsChecker._check_getnewargs_exv  s     ~~h'=DI(EKK0x}}"K q!4>>2q!4==1 

U c5::.$S/Cz#t/C/CD :&*
 =DI r`   )r^   r   returnNone)r   nodes.FunctionDefr   r   )r   r	   r   r   r   r   )r   r	   r   r   )r   r   rg   r	   r   r   )"__name__
__module____qualname____doc__rd   r   msgsrO   r   rh   visit_asyncfunctiondefre   staticmethodr   r   r   r   r   r   r   r   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   __classcell__)r_   s   @r   r!   r!   8   s<    D ,#&&1])= <<
 E12 ;<=

















WQDf
&  -!  &!%(::  /0d 
 
 M M M M N N O O O O N N  0ACABA@BAH%H1@H	HCG%G1@G	GJ%J1@J	Jr`   r!   rb   )r   r   r   r   r   zInferenceContext | Noner   zInferenceResult | None)r   
__future__r   collections.abcr   r   r   r   r   astroid.contextr   astroid.typingr	   pylint.checkersr
   pylint.checkers.utilsr   r   r   r   r   r   pylint.lint.pylinterr   r   r   r!    r`   r   <module>r      st   
 < " $  & & , * '  * (,
 % 	4[JK [Jr`   