B
    S\                 @   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ejee eeje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 )	LruWrappedModelzSpecial 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   <lib/python3.7/site-packages/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  V  d S )N)r   Z
safe_infer)r   Z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 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   sF  t j| }t|j}|dk r(td|dkr@|js@td|jd }yt|j	|d}W n, tj
k
r } ztj|W d d }~X Y nX |tjkrtdt|tjstdt|jjpd|jjpd}td	d
 |D }t|j| rt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 functionr   c             s   s    | ]}t |tjr|jV  qd S )N)
isinstanceastroidZ
AssignNamename).0Z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
kwonlyargssetr
   ZPartialFunctionr$   r'   r(   r)   r   Zpostinitr+   r,   r-   r.   r/   iter)	r   r   ZcallZnumber_of_positionalZpartial_functionZinferred_wrapped_functionexcZfunction_parametersZparameter_namesr   r   r   _functools_partial_inference@   sR    








r6   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_cachey   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$   Z	AttributeZattrnameexpr)r   memberr   r   r   r9      s    r9   r   )r?   )N)N)r   r;   r   	itertoolsr   r#   r   r   r   r   Zastroid.interpreterr   r	   r
   Z	LRU_CACHEZFunctionModelr   r    r6   r:   r9   Z_looks_like_partialZregister_transformr2   r8   Zinference_tipr   r   r   r   <module>   s.    

9