
    dH                       d dl mZ d dlmZ 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mZ d dlmZmZ  G d d	ej                  Z G d
 de
          Zej        d             Z G d d          Z G d d          ZdS )    )annotations)AnyN)DaskManager)ChunkManagerEntrypointget_chunked_array_typeguess_chunkmanagerlist_chunkmanagers)T_ChunksT_NormalizedChunks)has_daskrequires_daskc                  H     e Zd ZU dZded<   edddddf fd	Zd Zd Z xZ	S )	DummyChunkedArrayz
    Mock-up of a chunked array class.

    Adds a (non-functional) .chunks attribute by following this example in the numpy docs
    https://numpy.org/doc/stable/user/basics.subclassing.html#simple-example-adding-an-extra-attribute-to-ndarray
    r   chunksNr   c           	     d    t                                          | ||||||          }||_        |S N)super__new__r   )
clsshapedtypebufferoffsetstridesorderr   obj	__class__s
            @lib/python3.11/site-packages/xarray/tests/test_parallelcompat.pyr   zDummyChunkedArray.__new__   s2     ggooc5%%PP

    c                :    |d S t          |dd           | _        d S )Nr   )getattrr   )selfr   s     r   __array_finalize__z$DummyChunkedArray.__array_finalize__+   s#    ;Fc8T22r   c                <    |                                  }||_        |S r   )copyr   )r"   r   kwargscopieds       r   rechunkzDummyChunkedArray.rechunk0   s    r   )
__name__
__module____qualname____doc____annotations__floatr   r#   r(   __classcell__)r   s   @r   r   r      s           
      3 3 3
      r   r   c            	      l    e Zd ZdZd Zd dZd!d
Z	 	 	 	 d"d#dZd$dZd%dZ	d&dZ
ddddddddddZdS )'DummyChunkManagerz3Mock-up of ChunkManager class for DummyChunkedArrayc                    t           | _        d S r   )r   	array_clsr"   s    r   __init__zDummyChunkManager.__init__9   s    *r   datar   returnboolc                ,    t          |t                    S r   )
isinstancer   r"   r6   s     r   is_chunked_arrayz"DummyChunkManager.is_chunked_array<   s    $ 1222r   r   r   c                    |j         S r   r   r;   s     r   r   zDummyChunkManager.chunks?   s
    {r   Nr   T_Chunks | T_NormalizedChunksr   tuple[int, ...] | Nonelimit
int | Noner   np.dtype | Noneprevious_chunksT_NormalizedChunks | Nonec                ,    ddl m}  ||||||          S )Nr   )normalize_chunks)dask.array.corerG   )r"   r   r   rA   r   rD   rG   s          r   rG   z"DummyChunkManager.normalize_chunksB   s/     	544444ue_MMMr   
np.ndarrayr
   c                ,    ddl m}  |j        ||fi |S )Nr   )array)daskrK   
from_array)r"   r6   r   r&   das        r   rM   zDummyChunkManager.from_arrayN   s2     	%$$$$$r}T644V444r   c                     |j         |fi |S r   )r(   )r"   r6   r   r&   s       r   r(   zDummyChunkManager.rechunkU   s    t|F--f---r   tuple[np.ndarray, ...]c                    ddl m}  ||i |S )Nr   )compute)
dask.arrayrR   )r"   r6   r&   rR   s       r   rR   zDummyChunkManager.computeX   s)    &&&&&&w''''r   Faxesaxiskeepdimsoutput_dtypesoutput_sizes	vectorizeallow_rechunkmetac               :    ddl m}  |||g|R |||||||	|
d|S )Nr   )apply_gufuncrT   )dask.array.gufuncr^   )r"   func	signaturerU   rV   rW   rX   rY   rZ   r[   r\   argsr&   r^   s                 r   r^   zDummyChunkManager.apply_gufunc]   sm     	322222|
 
 
 '%'
 
 
 
 	
r   )r6   r   r7   r8   )r6   r   r7   r   )NNNN)r   r?   r   r@   rA   rB   r   rC   rD   rE   r7   r   )r6   rI   r   r
   r7   r   )r6   r   r7   r   )r6   r   r7   rP   )r)   r*   r+   r,   r5   r<   r   rG   rM   r(   rR   r^    r   r   r1   r1   6   s        ==+ + +3 3 3 3    )- !%59
N 
N 
N 
N 
N5 5 5 5. . . .( ( ( ( 
 
 
 
 
 
 
r   r1   c              #  b   K   t                      |                     dfd           dV  dS )a  
    Mocks the registering of an additional ChunkManagerEntrypoint.

    This preserves the presence of the existing DaskManager, so a test that relies on this and DaskManager both being
    returned from list_chunkmanagers() at once would still work.

    The monkeypatching changes the behavior of list_chunkmanagers when called inside xarray.core.parallelcompat,
    but not when called from this tests file.
    z-xarray.core.parallelcompat.list_chunkmanagersc                 *    dt                      i z  S Ndummy)r1   )preregistered_chunkmanagerss   r   <lambda>z-register_dummy_chunkmanager.<locals>.<lambda>   s    +--.1LL r   N)r	   setattr)monkeypatchrh   s    @r   register_dummy_chunkmanagerrl   ~   sK       #5"6"67LLLL   
EEEEEr   c                      e Zd ZddZddZedd            Zej        	                    e
d          dd            Zedd	            Zd
S )TestGetChunkManagerr7   Nonec                R    t          d          }t          |t                    sJ d S rf   )r   r:   r1   )r"   rl   chunkmanagers      r   test_get_chunkmangerz(TestGetChunkManager.test_get_chunkmanger   s,    )'22,(9:::::::r   c                    t          j        t          d          5  t          d           d d d            d S # 1 swxY w Y   d S )Nzunrecognized chunk manager foomatchfoopytestraises
ValueErrorr   r4   s    r   %test_fail_on_nonexistent_chunkmanagerz9TestGetChunkManager.test_fail_on_nonexistent_chunkmanager   s    ]:-MNNN 	& 	&u%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&   9= =c                R    t          d           }t          |t                    sJ d S r   r   r:   r   )r"   rq   s     r   test_get_dask_if_installedz.TestGetChunkManager.test_get_dask_if_installed   s+    )$//,4444444r   z!requires dask not to be installed)reasonc                    t          j        t          d          5  t          d           d d d            d S # 1 swxY w Y   d S )Nzunrecognized chunk manager daskrt   rL   rw   r4   s    r   #test_dont_get_dask_if_not_installedz7TestGetChunkManager.test_dont_get_dask_if_not_installed   s    ]:-NOOO 	' 	'v&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'r|   c                R    t          d           }t          |t                    sJ d S r   r~   )r"   rl   chunk_managers      r   )test_choose_dask_over_other_chunkmanagersz=TestGetChunkManager.test_choose_dask_over_other_chunkmanagers   s-     +400-5555555r   Nr7   ro   )r)   r*   r+   rr   r{   r   r   rx   markskipifr   r   r   rc   r   r   rn   rn      s        ; ; ; ;& & & & 5 5 5 ]5 [)LMM' ' ' NM' 6 6 6 ]6 6 6r   rn   c                  ^    e Zd Zd
dZd
dZd
dZd
dZed
d            Zed
d            Z	d	S )TestGetChunkedArrayTyper7   ro   c                t    t          g d          }t          |          }t          |t                    sJ d S )N         )r   r   r:   r1   r"   rl   	dummy_arrr   s       r   test_detect_chunked_arraysz2TestGetChunkedArrayType.test_detect_chunked_arrays   s=    %iii00	.y99-):;;;;;;;r   c                   t          g d          }t          |dt          j        ddg          g }t	          |t
                    sJ t          j        t          d          5  t          d           d d d            d S # 1 swxY w Y   d S )Nr         ?      zExpected a chunked arrayrt   g      @)	r   r   nprK   r:   r1   rx   ry   	TypeErrorr   s       r   test_ignore_inmemory_arraysz3TestGetChunkedArrayType.test_ignore_inmemory_arrays   s    %iii00	.C1a&AQAQ0RS-):;;;;;]9,FGGG 	( 	("3'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   $BBBc                    t          j        t          d          5  t          dt	          j        ddg          g  d d d            d S # 1 swxY w Y   d S )NzExpected a chunked array rt   r   r   r   )rx   ry   r   r   r   rK   )r"   rl   s     r   test_raise_if_no_arrays_chunkedz7TestGetChunkedArrayType.test_raise_if_no_arrays_chunked   s    ]9,GHHH 	= 	="S"(Aq6*:*:$;<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=s    A		AAc                    t          g d          }t          j        t          d          5  t	          |           d d d            d S # 1 swxY w Y   d S )Nr   z/Could not find a Chunk Manager which recognisesrt   )r   rx   ry   r   r   )r"   r   s     r   'test_raise_if_no_matching_chunkmanagersz?TestGetChunkedArrayType.test_raise_if_no_matching_chunkmanagers   s    %iii00	]N
 
 
 	. 	. #9---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   A

AAc                    dd l m} |                    g dd          }t          |          }t	          |t
                    sJ d S )Nr   r   r   r>   )rS   rK   rM   r   r:   r   )r"   rN   dask_arrr   s       r   test_detect_dask_if_installedz5TestGetChunkedArrayType.test_detect_dask_if_installed   sV    ==4=88.x88-5555555r   c                    dd l m} t          g d          }|                    g dd          }t	          j        t          d          5  t          ||g  d d d            d S # 1 swxY w Y   d S )Nr   r   r   r>   zreceived multiple typesrt   )rS   rK   r   rM   rx   ry   r   r   )r"   rl   rN   r   r   s        r   test_raise_on_mixed_array_typesz7TestGetChunkedArrayType.test_raise_on_mixed_array_types   s    %iii00	==4=88]9,EFFF 	; 	;"Xy$9::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   A%%A),A)Nr   )
r)   r*   r+   r   r   r   r   r   r   r   rc   r   r   r   r      s        < < < <( ( ( (= = = =. . . . 6 6 6 ]6 ; ; ; ]; ; ;r   r   )
__future__r   typingr   numpyr   rx   xarray.core.daskmanagerr   xarray.core.parallelcompatr   r   r   r	   xarray.core.typesr
   r   xarray.testsr   r   ndarrayr   r1   fixturerl   rn   r   rc   r   r   <module>r      s   " " " " " "            / / / / / /            ; : : : : : : : 0 0 0 0 0 0 0 0         
      FE
 E
 E
 E
 E
. E
 E
 E
P 
 
 
(6 6 6 6 6 6 6 66-; -; -; -; -; -; -; -; -; -;r   