o
    Nrf                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ er"d 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Zd'ddZejd(d#d$ZdS ))    )annotationsN)TYPE_CHECKING)HighLevelGraphLayerc                 C  s   | d S N    xr   r   X/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/dask/utils_test.pyinc      r   c                 C  s   | d S r   r   r	   r   r   r   dec   r   r   c                 C  s   | | S Nr   r
   yr   r   r   add   r   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s)J d S )Nr   :x:yr   r   :z   r      r   r   getselfdr   r   r   test_get.   s   zGetFunctionTestMixin.test_getc                 C  sb   dt dftddfd}z| |d}W n
 ty   Y d S w d}|d| d7 }J || jj)	Nr   r   r   r   badkey-Expected `{}` with badkey to raise KeyError.

Obtained '
' instead.)r   r   r"   KeyErrorformat__name__r$   r%   resultmsgr   r   r   test_badkey5   s   z GetFunctionTestMixin.test_badkeyc                 C  sf   ddt ddgfd}z| |dgdg}W n
 ty   Y d S w d}|d| d	7 }J || jj)
Nr   r   r
   r   r
   r   zr'   r(   r)   r*   )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   r2   )r   r   r3   r    r4   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s*J | |dg dks6J | |dddgksBJ | |dd	gksMJ | |d
g dg dgks]J | |dd	ddggd	gdgksoJ d S )N)r   r   r    r   r   r   cr   )r   r   r;   r%   efr%      r<   r=   )r   r4   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s!J d S )	Nr   r   r
   r   r2   ))r   r   r3   r    r9   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   s    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   r2   r3      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   s   " 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s,J d S )Nr   )r   r   r
   r   r3   r   )r   r   r   r   rD   )dask.highlevelgraphr   r   r   r"   )r$   r   layersdependenciesgraphr   r   r   test_with_HighLevelGraph|   s
   "
z-GetFunctionTestMixin.test_with_HighLevelGraphN)r-   
__module____qualname____doc__r&   r1   r5   r8   r:   r?   r@   rC   rE   rJ   rO   r   r   r   r   r      s    r   c              	   C  s&   zt | W S  ttfy   Y dS w )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
   rX   hlgr   prefixstrreturnr   c                 C  s@   | j  D ]\}}||r|  S qtd|dt| j  )zIGet the first layer from a HighLevelGraph whose name starts with a prefixzNo layer starts with z: )rL   items
startswithr+   list)rY   rZ   keyZlyrr   r   r   	hlg_layer   s
   
ra   rG   intc                 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   rc   	conditionboolcategorytype[Warning]messagec                 c  s|    | r$ddl }|j||d}|V  W d   dS 1 sw   Y  dS t }|V  W d   dS 1 s7w   Y  dS )z*Conditionally check if a warning is raisedr   N)match)pytestZwarns
contextlibnullcontext)rd   rf   rh   rj   ctxr   r   r   _check_warning   s   "
"rn   )r   )rY   r   rZ   r[   r\   r   )rY   r   rG   rb   r\   r   )rd   re   rf   rg   rh   r[   )
__future__r   rk   rS   r   typingr   rK   r   r   r   r   r   r   r   rX   ra   rc   contextmanagerrn   r   r   r   r   <module>   s"    
h

