11import asyncio
22import time
3+ from typing import Callable , Iterator , Union
34
45import pytest
6+ from distributed .client import Client
57
68from adaptive .learner import Learner1D , Learner2D
79from adaptive .runner import (
1517)
1618
1719
18- def blocking_runner (learner , goal ) :
20+ def blocking_runner (learner : Union [ Learner1D , Learner2D ], goal : Callable ) -> None :
1921 BlockingRunner (learner , goal , executor = SequentialExecutor ())
2022
2123
22- def async_runner (learner , goal ) :
24+ def async_runner (learner : Union [ Learner1D , Learner2D ], goal : Callable ) -> None :
2325 runner = AsyncRunner (learner , goal , executor = SequentialExecutor ())
2426 asyncio .get_event_loop ().run_until_complete (runner .task )
2527
2628
2729runners = [simple , blocking_runner , async_runner ]
2830
2931
30- def trivial_goal (learner ) :
32+ def trivial_goal (learner : Union [ Learner1D , Learner2D ]) -> bool :
3133 return learner .npoints > 10
3234
3335
3436@pytest .mark .parametrize ("runner" , runners )
35- def test_simple (runner ) :
37+ def test_simple (runner : Callable ) -> None :
3638 """Test that the runners actually run."""
3739
3840 def f (x ):
@@ -44,7 +46,7 @@ def f(x):
4446
4547
4648@pytest .mark .parametrize ("runner" , runners )
47- def test_nonconforming_output (runner ) :
49+ def test_nonconforming_output (runner : Callable ) -> None :
4850 """Test that using a runner works with a 2D learner, even when the
4951 learned function outputs a 1-vector. This tests against the regression
5052 flagged in https://github.com/python-adaptive/adaptive/issues/81.
@@ -56,7 +58,7 @@ def f(x):
5658 runner (Learner2D (f , [(- 1 , 1 ), (- 1 , 1 )]), trivial_goal )
5759
5860
59- def test_aync_def_function ():
61+ def test_aync_def_function () -> None :
6062 async def f (x ):
6163 return x
6264
@@ -69,7 +71,7 @@ async def f(x):
6971
7072
7173@pytest .fixture (scope = "session" )
72- def ipyparallel_executor ():
74+ def ipyparallel_executor () -> Iterator [ Client ] :
7375 from ipyparallel import Client
7476 import pexpect
7577
@@ -81,7 +83,7 @@ def ipyparallel_executor():
8183
8284
8385@pytest .fixture (scope = "session" )
84- def dask_executor ():
86+ def dask_executor () -> Iterator [ Client ] :
8587 from distributed import Client
8688
8789 client = Client (n_workers = 1 )
@@ -93,7 +95,7 @@ def linear(x):
9395 return x
9496
9597
96- def test_concurrent_futures_executor ():
98+ def test_concurrent_futures_executor () -> None :
9799 from concurrent .futures import ProcessPoolExecutor
98100
99101 BlockingRunner (
@@ -103,7 +105,7 @@ def test_concurrent_futures_executor():
103105 )
104106
105107
106- def test_stop_after_goal ():
108+ def test_stop_after_goal () -> None :
107109 seconds_to_wait = 0.2 # don't make this too large or the test will take ages
108110 start_time = time .time ()
109111 BlockingRunner (Learner1D (linear , (- 1 , 1 )), stop_after (seconds = seconds_to_wait ))
@@ -112,15 +114,15 @@ def test_stop_after_goal():
112114
113115
114116@pytest .mark .skipif (not with_ipyparallel , reason = "IPyparallel is not installed" )
115- def test_ipyparallel_executor (ipyparallel_executor ) :
117+ def test_ipyparallel_executor (ipyparallel_executor : Client ) -> None :
116118 learner = Learner1D (linear , (- 1 , 1 ))
117119 BlockingRunner (learner , trivial_goal , executor = ipyparallel_executor )
118120 assert learner .npoints > 0
119121
120122
121123@pytest .mark .timeout (60 )
122124@pytest .mark .skipif (not with_distributed , reason = "dask.distributed is not installed" )
123- def test_distributed_executor (dask_executor ) :
125+ def test_distributed_executor (dask_executor : Client ) -> None :
124126 learner = Learner1D (linear , (- 1 , 1 ))
125127 BlockingRunner (learner , trivial_goal , executor = dask_executor )
126128 assert learner .npoints > 0
0 commit comments