
    Q3j                     *   d dl Z d dlmZ d dlmZmZmZmZmZ d dl	m
Z
mZ e j                  j                  d e       g e        e       gg      d        Ze j                  j                  dd e       eg      d        Ze j                  j                  d	g d
      d        Zd Zd Ze j                  j                  dddg      e j                  j                  dddg      e j                  j                  deeg      d                      Zd Zy)    N)clone)FailingCallbackMaxIterEstimatorNotValidCallbackRecordingAutoPropagatedCallbackRecordingCallback)Paralleldelayed	callbacksc                     t               } |j                  |  }t        |d      sJ |j                  | k(  sJ ||u sJ y)z,Sanity check for the `set_callbacks` method._skl_callbacksN)r   set_callbackshasattrr   )r   	estimatorset_callbacks_returns      Y/DATA/.local/lib/python3.12/site-packages/sklearn/callback/tests/test_callback_support.pytest_set_callbacksr      sR     !"I2922I>9.///##y0009,,,    callbackc                     t               }t        j                  t        d      5  |j	                  |        ddd       y# 1 sw Y   yxY w)zMCheck the error message when not passing a valid callback to `set_callbacks`.z?callbacks must be instances following the FitCallback protocol.matchN)r   pytestraises	TypeErrorr   )r   r   s     r   test_set_callbacks_errorr   $   s>     !"I	O
 	)	
 
 
s   AA
fail_at)setupon_fit_task_beginon_fit_task_endteardownc                 ,   t        |       }t               j                  |      }t        j                  t
        d|        5  |j                          ddd       |j                  d      dk(  sJ |j                  d      dk(  sJ y# 1 sw Y   6xY w)z6Check that a failing callback is properly teared down.r   zFailing callback failed at r   Nr      r!   )r   r   r   r   r   
ValueErrorfitcount_hooks)r   r   r   s      r   test_callback_errorr(   0   s    
 w/H "00:I	z+Fwi)P	Q 
R (A---
+q000	 
R	Qs   B

Bc                     t        d      } t               }t               j                  | |      }t	        j
                  t        d      5  |j                          ddd       | j                  d      dk(  sJ | j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ y# 1 sw Y   bxY w)	z?Check that teardown only runs for callbacks that entered setup.r   r#   z Failing callback failed at setupr   Nr$   r!   r   )	r   r   r   r   r   r   r%   r&   r'   )
callback_1
callback_2r   s      r   :test_teardown_matches_setup_calls_on_partial_setup_failurer,   >   s     1J"$J "00ZHI	z)K	L 
M !!'*a///!!*-222 !!'*a///!!*-222 
M	Ls   B>>Cc                     t        d      } t        d      }t               j                  | |      }t        j                  t
        d      5 }|j                          ddd       t        j                  j                        dk(  sJ | j                  d      dk(  sJ |j                  d      dk(  sJ y# 1 sw Y   ZxY w)zuCheck that all teardown are called even if some fail.

    All errors are raised together in one ExceptionGroup.
    r!   r#   z/The following callback teardown errors occurredr   N   r$   )r   r   r   r   r   ExceptionGroupr&   lenvalue
exceptionsr'   )r*   r+   r   exc_infos       r   )test_multiple_teardown_errors_are_groupedr4   O   s    
 !4J 4J "00ZHI	O
	

 x~~(()Q...!!*-222!!*-222
 
s   B88Cn_jobsr$   r.   preferthreads	processesCallbackc                 B   d fd}d\  }} |       }t        |      j                  |      } |||| |       |j                  d      |k(  sJ |j                  d      |d|z   z  k(  sJ |j                  d      |d|z   z  k(  sJ |j                  d	      |k(  sJ y
)a  Check callbacks on estimators within function not supporting callbacks.

    Since the outer function does not support callbacks, there's no shared root context
    and the context trees of each sub-estimator are independent. As a result, the
    callback acts as a regular non-propagated callback: its on_fit_begin and on_fit_end
    are called once for each fit of the sub-estimator and the number of tasks is the sum
    of the number of tasks from all the sub-estimators.
    c                 6    t        |       j                          y N)r   r&   r   s    r   clone_and_fitz8test_function_no_callback_support.<locals>.clone_and_fitq   s    ir   c                 T      t        ||       fdt        |      D               y )N)r5   r6   c              3   @   K   | ]  } t                      y wr<   )r
   ).0_r>   r   s     r   	<genexpr>zBtest_function_no_callback_support.<locals>.func.<locals>.<genexpr>u   s"      /
7D!"GM"9-}s   )r	   range)r   n_fitsr5   r6   r>   s   `   r   funcz/test_function_no_callback_support.<locals>.funct   s%    .v. /
7<V}/
 	
r   )      )max_iterr   r   r$   r    r!   N)r   r   r'   )	r5   r6   r9   rF   rE   rI   r   r   r>   s	           @r   !test_function_no_callback_supportrJ   b   s    

 FHzH (3AA(KIFFF+(F222 34!h,8OOOO 12fH6MMMM
+v555r   c                      t               } | j                  t                      t        | d      sJ | j                          t        | d      rJ | j                          t        | d      rJ y)zGCheck that setting no callbacks removes the `_skl_callbacks` attribute.r   N)r   r   r   r   r=   s    r   test_set_callback_emptyrL      sl     "I-/09.///y"2333 y"23333r   )r   sklearn.baser   sklearn.callback.tests._utilsr   r   r   r   r   sklearn.utils.parallelr	   r
   markparametrizer   r   r(   r,   r4   rJ   rL    r   r   <module>rS      s3      5 				=?@	-	- d,<,>@Q%RS* T* L113"3& Aq6*I{#;<"$CD6 = +
6>4r   