3
tûœ]  ã               @   sè   d Z ddlmZ ddl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lmZ dZG dd„ dejƒZddd„Zddd„Zdd„ Zdd„ Zeedd�Zejejeeƒ ejejejeƒeƒ dS )z9Astroid hooks for understanding functools library module.é    )Úpartial)ÚchainN)Ú	arguments)ÚBoundMethod)Úextract_node)Úhelpers)Úobjectmodel)ÚMANAGER)Úobjectszfunctools.lru_cachec               @   s4   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZdS )	ÚLruWrappedModelz²Special attribute model for functions decorated with functools.lru_cache.

    The said decorators patches at decoration time some functions onto
    the decorated function.
    c             C   s   | j S )N)Ú	_instance)Úself© r   ú@/tmp/pip-build-fn42xldr/astroid/astroid/brain/brain_functools.pyÚattr___wrapped__   s    z LruWrappedModel.attr___wrapped__c                s,   t dƒ‰ G ‡ fdd„dtƒ}|| j| jd�S )NzQ
        from functools import _CacheInfo
        _CacheInfo(0, 0, 0, 0)
        c                   s   e Zd Zd‡ fdd„	ZdS )z=LruWrappedModel.attr_cache_info.<locals>.CacheInfoBoundMethodNc             3   s   t jˆ ƒV  d S )N)r   Z
safe_infer)r   ÚcallerÚcontext)Ú
cache_infor   r   Úinfer_call_result*   s    zOLruWrappedModel.attr_cache_info.<locals>.CacheInfoBoundMethod.infer_call_result)N)Ú__name__Ú
__module__Ú__qualname__r   r   )r   r   r   ÚCacheInfoBoundMethod)   s   r   )ÚproxyÚbound)r   r   r   )r   r   r   )r   r   Úattr_cache_info    s    zLruWrappedModel.attr_cache_infoc             C   s   t dƒ}t|| jjjƒ d�S )Nzdef cache_clear(self): pass)r   r   )r   r   r   ÚparentZscope)r   Únoder   r   r   Úattr_cache_clear/   s    z LruWrappedModel.attr_cache_clearN)r   r   r   Ú__doc__Úpropertyr   r   r   r   r   r   r   r      s   r   c             C   s   t ƒ | ƒ| _d S )N)r   Zspecial_attributes)r   r   r   r   r   Ú_transform_lru_cache5   s    r!   c       	      C   sT  t jj| ƒ}t|jƒ}|dk r(tjdƒ‚|dkrB|j rBtjdƒ‚|jd }yt|j	|d�ƒ}W n, tj
k
rŒ } ztj|‚W Y d d }~X nX |tjkr¢tjdƒ‚t|tjƒs¸tjdƒ‚t|jjpÄf |jjpÎf |jjpØf ƒ}tdd	„ |D ƒƒ}t|jƒ| �rtjd
ƒ‚tj||j|j|j|j|jd�}|j|j|j|j|j|j|jd� t |fƒS )Né   z-functools.partial takes at least one argumentz>functools.partial needs at least to have some filled argumentsr   )r   z!Cannot infer the wrapped functionz&The wrapped function is not a functionc             s   s    | ]}t |tjƒr|jV  qd S )N)Ú
isinstanceÚastroidZ
AssignNameÚname)Ú.0Úparamr   r   r   ú	<genexpr>^   s   z/_functools_partial_inference.<locals>.<genexpr>z,wrapped function received unknown parameters)r%   ÚdocÚlinenoÚ
col_offsetr   )ÚargsÚbodyÚ
decoratorsÚreturnsÚtype_comment_returnsÚtype_comment_args)!r   ZCallSiteZ	from_callÚlenZpositional_argumentsr$   ZUseInferenceDefaultZkeyword_argumentsÚnextZinferZInferenceErrorZUninferabler#   ÚFunctionDefr   r,   ZposonlyargsÚ
kwonlyargsÚsetr
   ZPartialFunctionr%   r)   r*   r+   r   Zpostinitr-   r.   r/   r0   r1   Úiter)	r   r   ÚcallZnumber_of_positionalZpartial_functionZinferred_wrapped_functionÚexcZfunction_parametersZparameter_namesr   r   r   Ú_functools_partial_inference@   sT    









r:   c             C   s<   | j s
dS x,| j jD ] }t|tjƒs&qt|dƒrdS qW dS )z=Check if the given function node is decorated with lru_cache.FÚ	lru_cacheT)r.   Znodesr#   r$   ÚCallÚ_looks_like_functools_member)r   Z	decoratorr   r   r   Ú_looks_like_lru_cachez   s    
r>   c             C   sV   t | jtjƒr| jj|kS t | jtjƒrR| jj|koPt | jjtjƒoP| jjjdkS dS )z8Check if the given Call node is a functools.partial callÚ	functoolsN)r#   Úfuncr$   ÚNamer%   Ú	AttributeÚattrnameÚexpr)r   Úmemberr   r   r   r=   †   s    r=   r   )rE   )N)N)r   r?   r   Ú	itertoolsr   r$   r   r   r   r   Zastroid.interpreterr   r	   r
   Z	LRU_CACHEZFunctionModelr   r!   r:   r>   r=   Z_looks_like_partialZregister_transformr4   r<   Zinference_tipr   r   r   r   Ú<module>   s.    

: