a
    tDf                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ erDd dlmZm	Z	 dd Z
dd Zd	d
 Zd!ddZG dd dZdd ZddddddZddddddZejdddddd ZdS )"    )annotationsN)TYPE_CHECKING)HighLevelGraphLayerc                 C  s   | d S N    xr   r   \/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/dask/utils_test.pyinc   s    r   c                 C  s   | d S r   r   r	   r   r   r   dec   s    r   c                 C  s   | | S Nr   r
   yr   r   r   add   s    r   皙?c                 C  s   t | | | S r   )timesleep)abdelayr   r   r   slowadd   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	dd Z
dd Zdd Zdd Zdd ZdS )GetFunctionTestMixina  
    The GetFunctionTestCase class can be imported and used to test foreign
    implementations of the `get` function specification. It aims to enforce all
    known expectations of `get` functions.

    To use the class, inherit from it and override the `get` function. For
    example:

    > from dask.utils_test import GetFunctionTestMixin
    > class TestCustomGet(GetFunctionTestMixin):
         get = staticmethod(myget)

    Note that the foreign `myget` function has to be explicitly decorated as a
    staticmethod.
    c                 C  sV   dt dftddfd}| |ddks*J | |ddks>J | |ddksRJ d S )Nr   :x:yr   r   :z   r      r   r   getselfdr   r   r   test_get.   s    zGetFunctionTestMixin.test_getc                 C  sh   dt dftddfd}z| |d}W n ty8   Y n,0 d}|d| d7 }d	sdJ || jjd S )
Nr   r   r   r   badkey-Expected `{}` with badkey to raise KeyError.

Obtained '
' instead.F)r   r   r!   KeyErrorformat__name__r#   r$   resultmsgr   r   r   test_badkey5   s    z GetFunctionTestMixin.test_badkeyc                 C  sl   ddt ddgfd}z| |dgdg}W n ty<   Y n,0 d}|d| d	7 }d
shJ || jjd S )Nr   r   r
   r   r
   r   zr&   r'   r(   r)   F)sumr!   r*   r+   r,   r-   r   r   r   test_nested_badkey@   s    z'GetFunctionTestMixin.test_nested_badkeyc                 C  s(   dt ddfd}| |ddks$J d S )Nr   r
   
   r   r      )r   r!   r"   r   r   r   test_data_not_in_dict_is_okL   s    z0GetFunctionTestMixin.test_data_not_in_dict_is_okc                 C  sD   ddt ddgfd}| |ddgdks,J | |ddks@J d S )	Nr   r   r
   r   r1   )r   r   r2   r   r3   r!   r"   r   r   r   test_get_with_listP   s    z'GetFunctionTestMixin.test_get_with_listc                 C  s   g dddt dfgtdfgddgtdfdgtdfgdgd}| |dg dksTJ | |dg dkslJ | |dddgksJ | |dd	gksJ | |d
g dg dgksJ | |dd	ddggd	gdgksJ d S )N)r   r   r   r   r   r   cr   )r   r   r:   r$   efr$      r;   r<   )r   r3   r!   r"   r   r   r   test_get_with_list_top_levelV   s    
 z1GetFunctionTestMixin.test_get_with_list_top_levelc                 C  sF   ddt ddgfd}| |dgdgdks.J | |ddksBJ d S )	Nr   r   r
   r   r1   ))r   r   r2   r   r8   r"   r   r   r   test_get_with_nested_listf   s    z.GetFunctionTestMixin.test_get_with_nested_listc                 C  s2   dd }d|ddhfd}|  |ddks.J d S )Nc                 S  s   | t | S r   )lenr   r   r   r   <lambda>m       zPGetFunctionTestMixin.test_get_works_with_unhashables_in_values.<locals>.<lambda>r   r
   r   r   r   )r!   )r#   r<   r$   r   r   r   )test_get_works_with_unhashables_in_valuesl   s    z>GetFunctionTestMixin.test_get_works_with_unhashables_in_valuesc                 C  s2   dt dftt dfdfd}| |ddks.J d S )Nr   r
   r   r1   r2      r    r"   r   r   r   test_nested_tasksr   s    z&GetFunctionTestMixin.test_nested_tasksc                 C  s2   dd t dD }d|d< | |ddks.J d S )Nc                 S  s"   i | ]}d |d  t d | fqS )zx%dr   )r   ).0ir   r   r   
<dictcomp>x   rB   z=GetFunctionTestMixin.test_get_stack_limit.<locals>.<dictcomp>i'  r   Zx0Zx10000)ranger!   r"   r   r   r   test_get_stack_limitw   s    z)GetFunctionTestMixin.test_get_stack_limitc                 C  s\   ddl m} dtdfddttdfdfid}d	d
hd}|||}| |ddksXJ d S )Nr   )r   r   r
   r   r2   r   )r   r   r   r   rD   )dask.highlevelgraphr   r   r   r!   )r#   r   layersZdependenciesgraphr   r   r   test_with_HighLevelGraph|   s
    "
z-GetFunctionTestMixin.test_with_HighLevelGraphN)r,   
__module____qualname____doc__r%   r0   r4   r7   r9   r>   r?   rC   rE   rJ   rN   r   r   r   r   r      s   r   c              	   C  s*   zt | W S  ttfy$   Y dS 0 dS )z>Import a module and return it; in case of failure; return NoneN)	importlibimport_moduleImportErrorAttributeError)namer   r   r   import_or_none   s    rW   r   strr   )hlgprefixreturnc                 C  sD   | j  D ]\}}||r
|  S q
td|dt| j  dS )zIGet the first layer from a HighLevelGraph whose name starts with a prefixzNo layer starts with z: N)rL   items
startswithr*   list)rY   rZ   keyZlyrr   r   r   	hlg_layer   s    

r`   int)rY   rG   r[   c                 C  s   | j |  |  S )zDGet the layer from a HighLevelGraph at position ``i``, topologically)rL   Z_toposort_layers)rY   rG   r   r   r   hlg_layer_topological   s    rb   boolztype[Warning])	conditioncategorymessagec                 c  st   | rBddl }|j||d}|V  W d   qp1 s60    Y  n.t }|V  W d   n1 sf0    Y  dS )z*Conditionally check if a warning is raisedr   N)match)pytestZwarns
contextlibnullcontext)rd   re   rf   rh   ctxr   r   r   _check_warning   s    &
rl   )r   )
__future__r   ri   rR   r   typingr   rK   r   r   r   r   r   r   r   rW   r`   rb   contextmanagerrl   r   r   r   r   <module>   s    
h