
    >ie"                       d dl mZ 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 Zd Z G d d	ej        j        j                  Z ej        e            G 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'        (                                '                    dd          fej'        (                                '                    dd           eej'        (                                '                    d                    f eej'        (                                '                    d                     eej'        (                                '                    d                    fg          d                         Z)ej                            d e ej*        d                    df ej+         ej*        d                    df e ej*        d                    dfej,        -                     ej*        d          g d          df ej*        d          dfddddg	          d             Z.ej                            dedfej	        dfedfej,        j/        dfej0        dfe1dfe2dfg          d             Z3d Z4 G d d ej        j        j                  Z5ej                            d! ej+        d"d#g           ej6        d"d#g          g          d$             Z7 G d% d&          Z8ej                            d! ej+        d"d#g           ej6        d"d#g          g          d'             Z9d( Z:dS ))    )annotationsN)Array)is_valid_array_chunkis_valid_chunk_type)	assert_eqc                      fd}|S )z
    Wrap a function.
    c                     t          | j                  |i |}t          |t          j                  s|n t          |           |          S N)getattrarr
isinstancenpndarraytype)selfakw	func_names      >lib/python3.11/site-packages/dask/array/tests/test_dispatch.pywrappedzwrap.<locals>.wrapped   sL    (GDHi((!2r22"1bj11DqqztDzz!}}D     )r   r   s   ` r   wrapr      s(    
E E E E E Nr   c                ,     t            fd            }|S )z!
    Wrap a simple property.
    c                .    t          | j                  S r
   )r   r   )r   r   r   	prop_names      r   r   z"dispatch_property.<locals>.wrapped   s    tx+++r   )property)r   r   s   ` r   dispatch_propertyr      s/    
 , , , , X, Nr   c                  
   e Zd ZdZdZd Zd Zd Z ed          Z	 ed          Z
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                    || _         d S r
   r   )r   r   s     r   __init__zEncapsulateNDArray.__init__2   s    r   c                6    t          j        | j        g|R i |S r
   r   asarrayr   r   argskwargss      r   	__array__zEncapsulateNDArray.__array__5   $    z$(4T444V444r   c                t    t           fd|D                       st          S t           fd|D                       }t           fd|D                       }t          |            j                            ||||          }t          |t          j                  s|n t                     |          S )Nc              3     K   | ]:}t          |t                    t          j        ft          j        z             V  ;d S r
   )
issubclassr   r   r   
ScalarType.0tir   s     r   	<genexpr>z8EncapsulateNDArray.__array_function__.<locals>.<genexpr>9   sR       
 
IKJrDJJ
3bmCDD
 
 
 
 
 
r   c              3  d   K   | ]*}t          |t                              s|n|j        V  +d S r
   r   r   r   )r2   r   r   s     r   r4   z8EncapsulateNDArray.__array_function__.<locals>.<genexpr>=   sO       
 
DGz#tDzz22?CC
 
 
 
 
 
r   c              3  V   K   | ]#}t          |t                              |V  $d S r
   )r/   r   r1   s     r   r4   z8EncapsulateNDArray.__array_function__.<locals>.<genexpr>@   s8      CC
2tDzz(B(BC"CCCCCCr   )
allNotImplementedtupleprintr   __array_function__r   r   r   r   )r   ftarrsr   r   s   `     r   r<   z%EncapsulateNDArray.__array_function__8   s     
 
 
 
OP
 
 
 
 
 	" "! 
 
 
 
KO
 
 
 
 
 CCCCqCCCCCaH''1dB77"1bj11DqqztDzz!}}Dr   __getitem____setitem__c                    t           fd|D                       st          S t           fd|D                       } t          ||          |i |}t	          |t
          j                  s|n t                     |          S )Nc              3     K   | ]:}t          |t                    t          j        ft          j        z             V  ;d S r
   )r   r   r   r   r0   r2   ir   s     r   r4   z5EncapsulateNDArray.__array_ufunc__.<locals>.<genexpr>J   sR       
 
HIJq4::rz2R]BCC
 
 
 
 
 
r   c              3  d   K   | ]*}t          |t                              s|n|j        V  +d S r
   r6   rD   s     r   r4   z5EncapsulateNDArray.__array_ufunc__.<locals>.<genexpr>N   s>      UU
1d4jj 9 9DqqquUUUUUUr   )r8   r9   r:   r   r   r   r   r   )r   ufuncmethodinputsr*   r   s   `     r   __array_ufunc__z"EncapsulateNDArray.__array_ufunc__I   s     
 
 
 
MS
 
 
 
 
 	" "!UUUUfUUUUU"GE6""F5f55"1bj11DqqztDzz!}}Dr   shapendimdtypeastypesumprodreshapesqueezeN)__name__
__module____qualname____doc____array_priority__r$   r+   r<   r   r@   rA   rJ   r   rK   rL   rM   rN   rO   rP   rQ   rR   r   r   r   r    r    &   s            5 5 5E E E $}%%K$}%%KE E E g&&EV$$Dg&&ET(^^F
$u++C4<<Dd9ooGd9ooGGGr   r    c                      e Zd ZdZd Zd Zd Zd Zd Zd Z	 e
d          Z e
d	          Z e
d
          Zd Z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                "    || _         || _        d S r
   )r   attrs)r   r   r[   s      r   r$   zWrappedArray.__init__h   s    


r   c                6    t          j        | j        g|R i |S r
   r&   r(   s      r   r+   zWrappedArray.__array__l   r,   r   c              #     K   |D ]w}t          |t          |                     r
|j        V  )t          |t          t          f          r. t          |          |                     |                    V  s|V  xd S r
   )r   r   r   r:   list_downcast_args)r   r)   args      r   r_   zWrappedArray._downcast_argso   s       	 	C#tDzz** gC%// d3ii 3 3C 8 8999999					 	r   c                    t          |                     |                    } t          |            t          ||          |i |fi | j        S r
   )r:   r_   r   r   r[   )r   rG   rH   rI   r*   s        r   rJ   zWrappedArray.__array_ufunc__x   sW    t**62233tDzz0'%00&CFCCRRtzRRRr   c                    t          |                     |                    } t          |            ||i |fi | j        S r
   )r:   r_   r   r[   )r   functypesr)   r*   s        r   r<   zWrappedArray.__array_function__|   sL    T((..//tDzz$$///>>4:>>>r   c                    dS )N.r   )r   s    r   __dask_graph__zWrappedArray.__dask_graph__   s	     sr   rK   rL   rM   c                N     t          |           | j        |         fi | j        S r
   )r   r   r[   )r   keys     r   r@   zWrappedArray.__getitem__   s(    tDzz$(3-664:666r   c                    || j         |<   d S r
   r#   )r   rh   values      r   rA   zWrappedArray.__setitem__   s    r   N)rS   rT   rU   rV   r$   r+   r_   rJ   r<   rf   r   rK   rL   rM   r@   rA   r   r   r   rY   rY   `   s           5 5 5  S S S? ? ?  
 g&&EV$$Dg&&E7 7 7    r   rY   opzarr_upcast, arr_downcast)
   rl   )   rm   )chunksc                    t           | ||                    t           | ||                    cxk    rt          |          k    sn J dS )z9Test proper dispatch on binary operators and NumPy ufuncsN)r   )rk   
arr_upcastarr_downcasts      r   %test_binary_operation_type_precedencerr      sz    P 	RR
L))**<,,--	 	 	 	
	 	 	 	 	 	 	 	r   zarr, result   FT)TFTF)NT)g        F)r   F) Fc                ,    t          |           |u sJ dS )z)Test is_valid_array_chunk for correctnessN)r   )r   results     r   test_is_valid_array_chunkrw      s#    &  $$......r   zarr_type, resultc                ,    t          |           |u sJ dS )z(Test is_valid_chunk_type for correctnessN)r   )arr_typerv   s     r   test_is_valid_chunk_typerz      s#     x((F222222r   c                    t          j        t          j        d                    } t	          t          j        d                    }|                     |          t          u sJ d|_        | t          j        |          z   }t          |t           j	                  sJ t          |dt          j        d          z  d           dS )zODirectly test Dask defering to an upcast type and the ability to still wrap it.rs   N   F)
check_type)da
from_arrayr   arangerY   __add__r9   rf   r   r   r   )r   bress      r   &test_direct_deferral_wrapping_overrider      s    
bill##ARYq\\""A99Q<<>))))A
bmA
Cc28$$$$$c1ry||#666666r   c                      e Zd Zd ZdS )$UnknownScalarThatUnderstandsArrayOpsc                    |                     dd          }||z   D ]B}t          |d          r0t          |t          j        t
          t          f          s	t          c S Ct                      S )Noutr   rJ   )gethasattrr   r   r   r   r   r9   )r   rG   rH   rI   r*   outputsitems          r   rJ   z4UnknownScalarThatUnderstandsArrayOps.__array_ufunc__   sw    **UB''W$ 	& 	&Dt.// &
rz5*NO9 9 & &%%%3555r   N)rS   rT   rU   rJ   r   r   r   r   r      s#        6 6 6 6 6r   r   r      r|   c                J   t                      }t          | |z            t           k    sJ t          || z            t           k    sJ t          t          j        ||                     t           k    sJ t          t          j        | |                    t           k    sJ d S r
   )r   r   r   multiplyr   ss     r   7test_delegation_unknown_scalar_that_understands_arr_opsr      s    ,..Aa==@@@@@C==@@@@@As##$$(LLLLLC##$$(LLLLLLLr   c                      e Zd ZdZd ZeZdS )UnknownScalarNc                    dS )N*   r   )r   others     r   __mul__zUnknownScalar.__mul__
  s    rr   )rS   rT   rU   rJ   r   __rmul__r   r   r   r   r     s)        O   HHHr   r   c                    t                      }| |z  dk    sJ || z  dk    sJ t          j        t          d          5  t	          j        ||            d d d            d S # 1 swxY w Y   d S )Nr   z/operand 'UnknownScalar' does not support ufuncs)match)r   pytestraises	TypeErrorr   r   r   s     r   test_delegation_unknown_scalarr     s    A7b====s7b====	J
 
 
   	As                 s    A##A'*A'c                     t          j        g d          } t          | dk    g d           t          d| k    g d           d| v sJ d S )N)r   r   .dr   )FFTFr   )r~   r   r   )r   s    r   test_delegation_specific_casesr     s_    
***++Aa3h333444cQh333444!888888r   );
__future__r   operatornumpyr   r   
dask.arrayarrayr~   r   dask.array.chunk_typesr   r   dask.array.utilsr   r   r   libmixinsNDArrayOperatorsMixinr    register_chunk_typerY   markparametrizeaddeqgtgeltlemodmulnepowsubtruedivfloordivsubtractrandomdefault_rngrr   r   r   mamasked_arrayrw   MaskedArrayr   floatintrz   r   r   r'   r   r   r   r   r   r   r   <module>r      sJ   " " " " " "                   L L L L L L L L & & & & & &	 	 		 	 	4 4 4 4 4< 4 4 4n  ) * * *- - - - -26=6 - - -` 

 (  L..0077AABBI!!##**8F*CC	

 I!!##**8F*CCry4466==hGGHH	

 L..0077AABBry4466==hGGHH	
 " # ) J 	ibill	#	#U+	yry||	$	$e,		IBIaLL	)	)40			IBIaLL*D*D*D	E	EtL	1t 	 "/ /# "/
 	u	5	T"		D!	T		e 3 3 3
	7 	7 	7	6 	6 	6 	6 	626=+N 	6 	6 	6 1v!6!6

Aq68J8J KLLM M MLM        1v!6!6

Aq68J8J KLL  ML    r   