o
    $i                  	   @   s  d dl Z d dlZd dl mZ d dlmZmZmZmZ d dlm	Z	 e
eZdZdede jfdd	Zddededee fddZdee deedf deeef ddfddZdee deedf deeef dee fddZdee deee eeef f fddZdS )    N)	Parameter)AnyDictListTuple)	is_cythons   __RAY_DUMMY__funcreturnc                    sh   t  r/g d}t fdd|D r( }dd  |D ]}t |t|| qnt dt S )aB  Get signature parameters.

    Support Cython functions by grabbing relevant attributes from the Cython
    function and attaching to a no-op function. This is somewhat brittle, since
    inspect may change, but given that inspect is written to a PEP, we hope
    it is relatively stable. Future versions of Python may allow overloading
    the inspect 'isfunction' and 'ismethod' functions / create ABC for Python
    functions. Until then, it appears that Cython won't do anything about
    compatability with the inspect module.

    Args:
        func: The function whose signature should be checked.

    Returns:
        A function signature object, which includes the names of the keyword
            arguments as well as their default values.

    Raises:
        TypeError: A type error if the signature is not supported
    )__code____annotations____defaults____kwdefaults__c                 3   s    | ]}t  |V  qd S N)hasattr).0attrr    R/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_common/signature.py	<genexpr>,   s    z get_signature.<locals>.<genexpr>c                   S   s   d S r   r   r   r   r   r   r   /   s   zget_signature.<locals>.funcz( is not a Python function we can process)r   allsetattrgetattr	TypeErrorinspect	signature)r   attrsoriginal_funcr   r   r   r   get_signature   s   
r   Fignore_firstc                 C   sD   t t| j }|r t|dkrtd| j d|dd }|S )a\  Extract the function signature from the function.

    Args:
        func: The function whose signature should be extracted.
        ignore_first: True if the first argument should be ignored. This should
            be used when func is a method of a class.

    Returns:
        List of Parameter objects representing the function signature.
    r   z5Methods must take a 'self' argument, but the method 'z' does not have one.   N)listr   
parametersvalueslen
ValueError__name__)r   r   signature_parametersr   r   r   extract_signature:   s   r(   r'   args.kwargsc              
   C   sJ   t j| d}z|j|i | W dS  ty$ } ztt|dd}~ww )a  Validates the arguments against the signature.

    Args:
        signature_parameters: The list of Parameter objects
            representing the function signature, obtained from
            `extract_signature`.
        args: The positional arguments passed into the function.
        kwargs: The keyword arguments passed into the function.

    Raises:
        TypeError: Raised if arguments do not fit in the function signature.
    )r"   N)r   	Signaturebindr   str)r'   r)   r*   reconstructed_signatureexcr   r   r   validate_argsR   s   r0   c                 C   sH   t | || g }|D ]}|t|g7 }q
| D ]
\}}|||g7 }q|S )a  Validates the arguments against the signature and flattens them.

    The flat list representation is a serializable format for arguments.
    Since the flatbuffer representation of function arguments is a list, we
    combine both keyword arguments and positional arguments. We represent
    this with two entries per argument value - [DUMMY_TYPE, x] for positional
    arguments and [KEY, VALUE] for keyword arguments. See the below example.
    See `recover_args` for logic restoring the flat list back to args/kwargs.

    Args:
        signature_parameters: The list of Parameter objects
            representing the function signature, obtained from
            `extract_signature`.
        args: The positional arguments passed into the function.
        kwargs: The keyword arguments passed into the function.

    Returns:
        List of args and kwargs. Non-keyword arguments are prefixed
            by internal enum DUMMY_TYPE.

    Raises:
        TypeError: Raised if arguments do not fit in the function signature.
    )r0   
DUMMY_TYPEitems)r'   r)   r*   	list_argsargkeywordr   r   r   flatten_argsh   s   r6   flattened_argsc                 C   sp   t | d dksJ dg }i }tdt | dD ]}| | | |d  }}|tkr/|| q|||< q||fS )aD  Recreates `args` and `kwargs` from the flattened arg list.

    Args:
        flattened_args: List of args and kwargs. This should be the output of
            `flatten_args`.

    Returns:
        args: The non-keyword arguments passed into the function.
        kwargs: The keyword arguments passed into the function.
       r   zAFlattened arguments need to be even-numbered. See `flatten_args`.r    )r$   ranger1   append)r7   r)   r*   
name_indexnamer4   r   r   r   recover_args   s   
r=   )F)r   loggingr   typingr   r   r   r   ray._private.inspect_utilr   	getLoggerr&   loggerr1   r+   r   boolr(   r-   r0   r6   r=   r   r   r   r   <module>   s8    
(





.$