U
    /e)"                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ dd Zdd ZG d	d
 d
ejjjZee G dd dejjjZejde je je je je je je je je je j e j!e j"e j#ejej$gejdeej%%dej%j%dddfej%j%dddeej%%dfeej%%deej%%dfgdd Z&ejdee'ddfe(e'ddfee'ddfej)*e'dddddgdfe'ddfddddg	dd Z+ejdedfejdfedfej)j,dfej-dfe.dfe/dfgdd  Z0d!d" Z1G d#d$ d$ejjjZ2ejd%e(d&d'ge3d&d'ggd(d) Z4G d*d+ d+Z5ejd%e(d&d'ge3d&d'ggd,d- Z6d.d/ Z7dS )0    N)Array)is_valid_array_chunkis_valid_chunk_type)	assert_eqc                    s    fdd}|S )z
    Wrap a function.
    c                    s.   t | j ||}t|tjs"|S t| |S N)getattrarr
isinstancenpndarraytypeselfakw	func_name B/tmp/pip-unpacked-wheel-dbjnr7gq/dask/array/tests/test_dispatch.pywrapped   s    zwrap.<locals>.wrappedr   )r   r   r   r   r   wrap   s    r   c                    s   t  fdd}|S )z!
    Wrap a simple property.
    c                    s   t | j S r   )r   r   r   	prop_namer   r   r      s    z"dispatch_property.<locals>.wrapped)property)r   r   r   r   r   dispatch_property   s    r   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zed	Z	ed
Z
dd ZedZedZedZedZedZedZedZedZdS )EncapsulateNDArraya  
    A class that "mocks" ndarray by encapsulating an ndarray and using
    protocols to "look like" an ndarray. Basically tests whether Dask
    works fine with something that is essentially an array but uses
    protocols instead of being an actual array. Must be manually
    registered as a valid chunk type to be considered a downcast type
    of Dask array in the type casting hierarchy.
       c                 C   s
   || _ d S r   r   )r   r   r   r   r   __init__0   s    zEncapsulateNDArray.__init__c                 O   s   t j| jf||S r   r
   asarrayr   r   argskwargsr   r   r   	__array__3   s    zEncapsulateNDArray.__array__c                    s|   t  fdd|D stS t fdd|D }t fdd|D }t|  j||||}t|tjsp|S t	 |S )Nc                 3   s(   | ] }t |t tjftj V  qd S r   )
issubclassr   r
   r   
ScalarType.0Ztir   r   r   	<genexpr>7   s    z8EncapsulateNDArray.__array_function__.<locals>.<genexpr>c                 3   s&   | ]}t |t s|n|jV  qd S r   r	   r   r   )r(   r   r)   r   r   r*   ;   s    c                 3   s    | ]}t |t s|V  qd S r   )r%   r   r'   r)   r   r   r*   >   s      )
allNotImplementedtupleprintr   __array_function__r	   r
   r   r   )r   ftZarrsr   r   r   r)   r   r0   6   s    z%EncapsulateNDArray.__array_function____getitem____setitem__c                    s\   t  fdd|D stS t fdd|D }t||||}t|tjsP|S t |S )Nc                 3   s(   | ] }t |t tjftj V  qd S r   )r	   r   r
   r   r&   r(   ir)   r   r   r*   H   s    z5EncapsulateNDArray.__array_ufunc__.<locals>.<genexpr>c                 3   s&   | ]}t |t s|n|jV  qd S r   r+   r5   r)   r   r   r*   L   s     )r,   r-   r.   r   r	   r
   r   r   )r   ufuncmethodinputsr#   r   r   r)   r   __array_ufunc__G   s    z"EncapsulateNDArray.__array_ufunc__shapendimdtypeastypesumprodreshapesqueezeN)__name__
__module____qualname____doc__Z__array_priority__r   r$   r0   r   r3   r4   r:   r   r;   r<   r=   r>   r?   r@   rA   rB   r   r   r   r   r   $   s    		r   c                   @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dZe
dZe
dZdd Zdd ZdS )WrappedArraya  
    Another mock duck array class (like EncapsulateNDArray), but
    designed to be above Dask in the type casting hierarchy (that is,
    WrappedArray wraps Dask Array) and be even more minimal in API.
    Tests that Dask defers properly to upcast types.
    c                 K   s   || _ || _d S r   )r   attrs)r   r   rH   r   r   r   r   f   s    zWrappedArray.__init__c                 O   s   t j| jf||S r   r   r!   r   r   r   r$   j   s    zWrappedArray.__array__c                 c   sP   |D ]F}t |t| r |jV  qt |ttfrDt|| |V  q|V  qd S r   )r	   r   r   r.   list_downcast_args)r   r"   argr   r   r   rJ   m   s    
zWrappedArray._downcast_argsc                 O   s,   t | |}t| t||||f| jS r   )r.   rJ   r   r   rH   )r   r7   r8   r9   r#   r   r   r   r:   v   s    zWrappedArray.__array_ufunc__c                 C   s&   t | |}t| |||f| jS r   )r.   rJ   r   rH   )r   functypesr"   r#   r   r   r   r0   z   s    zWrappedArray.__array_function__c                 C   s   dS )N.r   r)   r   r   r   __dask_graph__~   s    zWrappedArray.__dask_graph__r;   r<   r=   c                 C   s   t | | j| f| jS r   )r   r   rH   )r   keyr   r   r   r3      s    zWrappedArray.__getitem__c                 C   s   || j |< d S r   r   )r   rO   valuer   r   r   r4      s    zWrappedArray.__setitem__N)rC   rD   rE   rF   r   r$   rJ   r:   r0   rN   r   r;   r<   r=   r3   r4   r   r   r   r   rG   ^   s   	rG   opzarr_upcast, arr_downcast)
   rR   )   rS   )chunksc                 C   s6   t | ||t | ||  kr,t |ks2n tdS )z9Test proper dispatch on binary operators and NumPy ufuncsN)r   AssertionError)rQ   Z
arr_upcastZarr_downcastr   r   r   %test_binary_operation_type_precedence   s
    (rV   zarr, result   FT)NT)g        F)r   F) Fc                 C   s   t | |kstdS )z)Test is_valid_array_chunk for correctnessN)r   rU   )r   resultr   r   r   test_is_valid_array_chunk   s    rZ   zarr_type, resultc                 C   s   t | |kstdS )z(Test is_valid_chunk_type for correctnessN)r   rU   )Zarr_typerY   r   r   r   test_is_valid_chunk_type   s    r[   c                  C   sp   t td} ttd}| |tks0td|_| t | }t	|t j
sTtt|dtd dd dS )zODirectly test Dask defering to an upcast type and the ability to still wrap it.rW   N   F)Z
check_type)da
from_arrayr
   arangerG   __add__r-   rU   rN   r	   r   r   )r   bresr   r   r   &test_direct_deferral_wrapping_override   s    rc   c                   @   s   e Zd Zdd ZdS )$UnknownScalarThatUnderstandsArrayOpsc                 O   sD   | dd}|| D ](}t|drt|tjttfst  S qt S )Noutr   r:   )gethasattrr	   r
   r   r   rd   r-   )r   r7   r8   r9   r#   outputsitemr   r   r   r:      s     

z4UnknownScalarThatUnderstandsArrayOps.__array_ufunc__N)rC   rD   rE   r:   r   r   r   r   rd      s   rd   r      r\   c                 C   sb   t  }t| | t kstt||  t ks.ttt|| t ksFttt| |t ks^td S r   )rd   r   rU   r
   multiplyr   sr   r   r   7test_delegation_unknown_scalar_that_understands_arr_ops   s
    rn   c                   @   s   e Zd ZdZdd ZeZdS )UnknownScalarNc                 C   s   dS )N*   r   )r   otherr   r   r   __mul__  s    zUnknownScalar.__mul__)rC   rD   rE   r:   rr   __rmul__r   r   r   r   ro     s   ro   c              	   C   sP   t  }| | dkst||  dks&ttjtdd t||  W 5 Q R X d S )Nrp   z/operand 'UnknownScalar' does not support ufuncs)match)ro   rU   pytestZraises	TypeErrorr
   rk   rl   r   r   r   test_delegation_unknown_scalar  s     rw   c                  C   sN   t ddddg} t| dkddddg td| kddddg d| ksJtd S )Nr   ra   .dFT)r]   r^   r   rU   )r   r   r   r   test_delegation_specific_cases  s    rz   )8operatorZnumpyr
   ru   Z
dask.arrayarrayr]   r   Zdask.array.chunk_typesr   r   Zdask.array.utilsr   r   r   libZmixinsZNDArrayOperatorsMixinr   Zregister_chunk_typerG   markZparametrizeaddeqgtgeltlemodmulnepowsubtruedivfloordivsubtractrandomrV   r_   r^   maZmasked_arrayrZ   ZMaskedArrayr   floatintr[   rc   rd   r    rn   ro   rw   rz   r   r   r   r   <module>   s   7
0	


$
		$

