1
1
import asyncio
2
2
import time
3
+ from typing import Callable , Iterator , Union
3
4
4
5
import pytest
6
+ from distributed .client import Client
5
7
6
8
from adaptive .learner import Learner1D , Learner2D
7
9
from adaptive .runner import (
15
17
)
16
18
17
19
18
- def blocking_runner (learner , goal ) :
20
+ def blocking_runner (learner : Union [ Learner1D , Learner2D ], goal : Callable ) -> None :
19
21
BlockingRunner (learner , goal , executor = SequentialExecutor ())
20
22
21
23
22
- def async_runner (learner , goal ) :
24
+ def async_runner (learner : Union [ Learner1D , Learner2D ], goal : Callable ) -> None :
23
25
runner = AsyncRunner (learner , goal , executor = SequentialExecutor ())
24
26
asyncio .get_event_loop ().run_until_complete (runner .task )
25
27
26
28
27
29
runners = [simple , blocking_runner , async_runner ]
28
30
29
31
30
- def trivial_goal (learner ) :
32
+ def trivial_goal (learner : Union [ Learner1D , Learner2D ]) -> bool :
31
33
return learner .npoints > 10
32
34
33
35
34
36
@pytest .mark .parametrize ("runner" , runners )
35
- def test_simple (runner ) :
37
+ def test_simple (runner : Callable ) -> None :
36
38
"""Test that the runners actually run."""
37
39
38
40
def f (x ):
@@ -44,7 +46,7 @@ def f(x):
44
46
45
47
46
48
@pytest .mark .parametrize ("runner" , runners )
47
- def test_nonconforming_output (runner ) :
49
+ def test_nonconforming_output (runner : Callable ) -> None :
48
50
"""Test that using a runner works with a 2D learner, even when the
49
51
learned function outputs a 1-vector. This tests against the regression
50
52
flagged in https://github.com/python-adaptive/adaptive/issues/81.
@@ -56,7 +58,7 @@ def f(x):
56
58
runner (Learner2D (f , [(- 1 , 1 ), (- 1 , 1 )]), trivial_goal )
57
59
58
60
59
- def test_aync_def_function ():
61
+ def test_aync_def_function () -> None :
60
62
async def f (x ):
61
63
return x
62
64
@@ -69,7 +71,7 @@ async def f(x):
69
71
70
72
71
73
@pytest .fixture (scope = "session" )
72
- def ipyparallel_executor ():
74
+ def ipyparallel_executor () -> Iterator [ Client ] :
73
75
from ipyparallel import Client
74
76
import pexpect
75
77
@@ -81,7 +83,7 @@ def ipyparallel_executor():
81
83
82
84
83
85
@pytest .fixture (scope = "session" )
84
- def dask_executor ():
86
+ def dask_executor () -> Iterator [ Client ] :
85
87
from distributed import Client
86
88
87
89
client = Client (n_workers = 1 )
@@ -93,7 +95,7 @@ def linear(x):
93
95
return x
94
96
95
97
96
- def test_concurrent_futures_executor ():
98
+ def test_concurrent_futures_executor () -> None :
97
99
from concurrent .futures import ProcessPoolExecutor
98
100
99
101
BlockingRunner (
@@ -103,7 +105,7 @@ def test_concurrent_futures_executor():
103
105
)
104
106
105
107
106
- def test_stop_after_goal ():
108
+ def test_stop_after_goal () -> None :
107
109
seconds_to_wait = 0.2 # don't make this too large or the test will take ages
108
110
start_time = time .time ()
109
111
BlockingRunner (Learner1D (linear , (- 1 , 1 )), stop_after (seconds = seconds_to_wait ))
@@ -112,15 +114,15 @@ def test_stop_after_goal():
112
114
113
115
114
116
@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 :
116
118
learner = Learner1D (linear , (- 1 , 1 ))
117
119
BlockingRunner (learner , trivial_goal , executor = ipyparallel_executor )
118
120
assert learner .npoints > 0
119
121
120
122
121
123
@pytest .mark .timeout (60 )
122
124
@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 :
124
126
learner = Learner1D (linear , (- 1 , 1 ))
125
127
BlockingRunner (learner , trivial_goal , executor = dask_executor )
126
128
assert learner .npoints > 0
0 commit comments