U
    ;vf                      @   s   d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	 e
eejddZdZdd	 Zeed
 eed eddZdd Zdd ZdddZdddZdS )z;Implementation of __array_function__ overrides from NEP-18.    N)add_docstringimplement_array_function_get_implementing_args)
getargspecZ!NUMPY_EXPERIMENTAL_ARRAY_FUNCTION   ar  like : array_like, optional
        Reference object to allow the creation of arrays which are not
        NumPy arrays. If an array-like passed in as ``like`` supports
        the ``__array_function__`` protocol, the result will be defined
        by it. In this case, it ensures the creation of an array object
        compatible with that passed in via this argument.c                 C   s   | j d k	r| j dt| _ | S )Nz${ARRAY_FUNCTION_LIKE})__doc__replacearray_function_like_doc)
public_api r   8/tmp/pip-unpacked-wheel-sdaoq5e2/numpy/core/overrides.pyset_array_function_like_doc   s    
r   a  
    Implement a function with checks for __array_function__ overrides.

    All arguments are required, and can only be passed by position.

    Parameters
    ----------
    implementation : function
        Function that implements the operation on NumPy array without
        overrides when called like ``implementation(*args, **kwargs)``.
    public_api : function
        Function exposed by NumPy's public API originally called like
        ``public_api(*args, **kwargs)`` on which arguments are now being
        checked.
    relevant_args : iterable
        Iterable of arguments to check for __array_function__ methods.
    args : tuple
        Arbitrary positional arguments originally passed into ``public_api``.
    kwargs : dict
        Arbitrary keyword arguments originally passed into ``public_api``.

    Returns
    -------
    Result from calling ``implementation()`` or an ``__array_function__``
    method, as appropriate.

    Raises
    ------
    TypeError : if no implementation is found.
    ar  
    Collect arguments on which to call __array_function__.

    Parameters
    ----------
    relevant_args : iterable of array-like
        Iterable of possibly array-like arguments to check for
        __array_function__ methods.

    Returns
    -------
    Sequence of arguments with __array_function__ methods, in the order in
    which they should be called.
    ArgSpeczargs varargs keywords defaultsc                 C   s   t t|  }t t| }|j|jksn|j|jksn|j|jksnt|jt|jksn|jdk	rzt|jt|jkrztd|  |jdk	r|jdt|j krtddS )z:Verify that a dispatcher function has the right signature.NzGimplementation and dispatcher for %s have different function signaturesNzBdispatcher functions can only use None for default argument values)	r   r   argsvarargskeywordsbooldefaultslenRuntimeError)implementation
dispatcherZimplementation_specZdispatcher_specr   r   r   verify_matching_signaturesX   s.    


r   c                    s    fdd}|S )zDecorator for overriding __module__ on a function or class.

    Example usage::

        @set_module('numpy')
        def example():
            pass

        assert example.__module__ == 'numpy'
    c                    s    d k	r | _ | S r   )
__module__)funcmoduler   r   	decoratory   s    zset_module.<locals>.decoratorr   )r   r   r   r   r   
set_modulen   s    r   TFc                    s0   t s fdd}|S  fdd}|S )a  Decorator for adding dispatch with the __array_function__ protocol.

    See NEP-18 for example usage.

    Parameters
    ----------
    dispatcher : callable
        Function that when called like ``dispatcher(*args, **kwargs)`` with
        arguments from the NumPy function call returns an iterable of
        array-like arguments to check for ``__array_function__``.
    module : str, optional
        __module__ attribute to set on new function, e.g., ``module='numpy'``.
        By default, module is copied from the decorated function.
    verify : bool, optional
        If True, verify the that the signature of the dispatcher and decorated
        function signatures match exactly: all required and optional arguments
        should appear in order with the same names, but the default values for
        all optional arguments should be ``None``. Only disable verification
        if the dispatcher's signature needs to deviate for some particular
        reason, e.g., because the function has a signature like
        ``func(*args, **kwargs)``.
    docs_from_dispatcher : bool, optional
        If True, copy docs from the dispatcher function onto the dispatched
        function, rather than from the implementation. This is useful for
        functions defined in C, which otherwise don't have docstrings.

    Returns
    -------
    Function suitable for decorating the implementation of a NumPy function.
    c                    s"   rt |  j d k	r| _| S r   )r   r   r   r   )r   docs_from_dispatcherr   r   r   r      s
    z*array_function_dispatch.<locals>.decoratorc                    sf   rt   rt j t  fddjj jdd_d k	r\_ _	S )Nc               
      s   z | |}W n` t k
rn } zB|jd } j}t|trD||sF ||j}t |d W 5 d }~X Y nX t|| |S )Nr   )	TypeErrorr   __name__
isinstancestr
startswithr   r   )r   kwargsZrelevant_argsexcmsgZ	disp_nameZnew_msg)r   r   r
   use_liker   r   r
      s"    

    z>array_function_dispatch.<locals>.decorator.<locals>.public_apiz<__array_function__ internals>)co_nameco_filename)
r   r   r   	functoolswraps__code__r   r#   r   _implementationr    r   r!   r   r*   verify)r   r
   r   r      s    
)ARRAY_FUNCTION_ENABLED)r   r   r2   r!   r*   r   r   r1   r   array_function_dispatch   s
    !,r4   c                    s    fdd}|S )zDLike array_function_dispatcher, but with function arguments flipped.c                    s   t |  dS )N)r2   r!   )r4   )r   r!   r   r   r2   r   r   r      s      z1array_function_from_dispatcher.<locals>.decoratorr   )r   r   r2   r!   r   r   r5   r   array_function_from_dispatcher   s    r6   )NTFF)NTT)r   collectionsr-   osZnumpy.core._multiarray_umathr   r   r   Znumpy.compat._inspectr   r   intenvirongetr3   r	   r   
namedtupler   r   r   r4   r6   r   r   r   r   <module>   s<   		#    
Z     