o
    g3                     @   sn   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
 d dlmZmZ d dlmZ G dd	 d	Zd
S )    )Optional)nodes)Instance)Context)CallContextInferenceContext)InferenceError	NoDefaultUninferablec                   @   sf   e Zd ZdZ	ddefddZeddee fddZ	d	d
 Z
dd ZdddZdddZdd ZdS )CallSitea  Class for understanding arguments passed into a call site

    It needs a call context, which contains the arguments and the
    keyword arguments that were passed into a given call site.
    In order to infer what an argument represents, call :meth:`infer_argument`
    with the corresponding function node and the argument name.

    :param callcontext:
        An instance of :class:`astroid.context.CallContext`, that holds
        the arguments for the call site.
    :param argument_context_map:
        Additional contexts per node, passed in from :attr:`astroid.context.Context.extra_context`
    :param context:
        An instance of :class:`astroid.context.Context`.
    Ncallcontextc                 C   sr   |d u ri }|| _ |j}|j}t | _| j||d| _| j||d| _dd | jD | _	dd | j
 D | _d S )Ncontextc                 S   s   g | ]}|t ur|qS  r
   .0argr   r   O/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/astroid/arguments.py
<listcomp>4   s    z%CallSite.__init__.<locals>.<listcomp>c                 S   s   i | ]\}}|t ur||qS r   r
   r   keyvaluer   r   r   
<dictcomp>7   
    z%CallSite.__init__.<locals>.<dictcomp>)argument_context_mapargskeywordssetduplicated_keywords_unpack_args_unpacked_args_unpack_keywords_unpacked_kwargspositional_argumentsitemskeyword_arguments)selfr   r   r   r   r   r   r   r   __init__(   s   zCallSite.__init__r   c                 C   s$   |pt  }t|j|j}| ||dS )zxGet a CallSite object from the given Call node.

        context will be used to force a single inference path.
        r   )r   r   r   r   )cls	call_noder   r   r   r   r   	from_call=   s   
zCallSite.from_callc                 C      t | jt | jkS )an  Check if in the current CallSite were passed *invalid* arguments

        This can mean multiple things. For instance, if an unpacking
        of an invalid object was passed, then this method will return True.
        Other cases can be when the arguments can't be inferred by astroid,
        for example, by passing objects which aren't known statically.
        )lenr$   r!   r'   r   r   r   has_invalid_argumentsI      zCallSite.has_invalid_argumentsc                 C   r,   )af  Check if in the current CallSite were passed *invalid* keyword arguments

        For instance, unpacking a dictionary with integer keys is invalid
        (**{1:2}), because the keys must be strings, which will make this
        method to return True. Other cases where this might return True if
        objects which can't be inferred were passed.
        )r-   r&   r#   r.   r   r   r   has_invalid_keywordsS   r0   zCallSite.has_invalid_keywordsc           	   
   C   s:  i }|pt  }| j|_|D ]\}}|d u rz
t|j|d}W n ty,   t||< Y q ty4   Y qw t|t	j
s@t||< q|jD ]Q\}}z
t|j|d}W n ty^   t||< Y qC tyf   Y qCw t|t	jsrt||< qCt|jts}t||< qC|j|v rt||j< | j|j qC|||j< qCq|||< q|S )Nr   )r   r   extra_contextnextinferr   r   StopIteration
isinstancer   Dictr%   Constr   strr   add)	r'   r   r   valuesnamer   inferreddict_key
dict_valuer   r   r   r"   ]   sN   



zCallSite._unpack_keywordsc              	   C   s   g }|pt  }| j|_|D ]K}t|tjrSzt|jj|d}W n t	y.   |
t Y q ty6   Y qw |tu rA|
t qt|dsL|
t q||j q|
| q|S )Nr   elts)r   r   r2   r6   r   Starredr3   r   r4   r   appendr   r5   hasattrextendr@   )r'   r   r   r;   r   r=   r   r   r   r       s,   




zCallSite._unpack_argsc              	      s0  || j v rtd| |||dz	| j| |W S  ty    Y nw t| jt|jjkr=|jjs=|jj	s=td| |||d| jdt|jj }| jt|jjd }|j
|d }dd |jjD   fdd	| j D }t|t|jjk r|jjD ]}|j|v r||j}	||	 q~|durt|d
d}
|dkr|jdv r|
du r|jdkr|r|d j|dS |
du r|j }
t|
tjr|j }||
 u rt|
fS |jdkrt|
ts|
 }
t|
fS |jdkrt|
fS |jdv r|
r|d8 }z	| j| |W S  ty   Y nw |jj|krJ|  r,td| j| j| |||dtj |jj!|jj"|jd}|#dd | D  t|fS |jj|krz| $ rctd| j| j%| |||dtj&|jj!|jj"|jd}|#| t|fS z
|j'||W S  t(y   Y nw td| |||d)a  infer a function argument value according to the call context

        Arguments:
            funcnode: The function being called.
            name: The name of the argument whose value is being inferred.
            context: Inference context object
        z:The arguments passed to {func!r}  have duplicate keywords.)	call_sitefuncr   r   zJToo many positional arguments passed to {func!r} that does not have *args.Nr   c                 S   s   h | ]}|j qS r   )r<   r   r   r   r   	<setcomp>   s    z*CallSite.infer_argument.<locals>.<setcomp>c                    s   i | ]\}}| vr||qS r   r   r   
kwonlyargsr   r   r      r   z+CallSite.infer_argument.<locals>.<dictcomp>	boundnode>   methodclassmethodrK   r   rL      zInference failed to find values for all keyword arguments to {func!r}: {unpacked_kwargs!r} doesn't correspond to {keyword_arguments!r}.)r&   unpacked_kwargsrE   rF   r   r   )lineno
col_offsetparentc                 S   s   g | ]\}}t ||fqS r   )r   const_factoryr   r   r   r   r     s    z+CallSite.infer_argument.<locals>.<listcomp>zInference failed to find values for all positional arguments to {func!r}: {unpacked_args!r} doesn't correspond to {positional_arguments!r}.)r$   unpacked_argsrE   rF   r   r   z-No value found for argument {arg} to {func!r}))r   r   r&   r4   KeyErrorr-   r$   r   varargposonlyargsfind_argnamerI   r%   r<   poprB   getattrtyperQ   framer6   r   ClassDefscope	metaclassiterr   instantiate_class
IndexErrorkwargr1   r#   r7   rO   rP   postinitr/   r!   Tupledefault_valuer	   )r'   funcnoder<   r   
positionalrU   argindexkwargsfunc_argr   rJ   method_scoperb   r   r   rH   r   infer_argument   s   
	


















zCallSite.infer_argument)NN)N)__name__
__module____qualname____doc__r   r(   rL   r   r   r+   r/   r1   r"   r    rl   r   r   r   r   r      s    




,r   N)typingr   astroidr   astroid.basesr   astroid.constr   astroid.contextr   r   astroid.exceptionsr   r	   astroid.utilr   r   r   r   r   r   <module>   s   