File tree Expand file tree Collapse file tree 3 files changed +21
-1
lines changed Expand file tree Collapse file tree 3 files changed +21
-1
lines changed Original file line number Diff line number Diff line change
1
+ Added a helpful error message if an async function is passed to `trio.to_thread.run_sync `.
Original file line number Diff line number Diff line change @@ -165,7 +165,17 @@ def do_release_then_return_result():
165
165
def worker_fn ():
166
166
TOKEN_LOCAL .token = current_trio_token
167
167
try :
168
- return sync_fn (* args )
168
+ ret = sync_fn (* args )
169
+
170
+ if inspect .iscoroutine (ret ):
171
+ # Manually close coroutine to avoid RuntimeWarnings
172
+ ret .close ()
173
+ raise TypeError (
174
+ "Trio expected a sync function, but {!r} appears to be "
175
+ "asynchronous" .format (getattr (sync_fn , "__qualname__" , sync_fn ))
176
+ )
177
+
178
+ return ret
169
179
finally :
170
180
del TOKEN_LOCAL .token
171
181
Original file line number Diff line number Diff line change @@ -456,6 +456,15 @@ def thread_fn():
456
456
assert callee_token == caller_token
457
457
458
458
459
+ async def test_trio_to_thread_run_sync_expected_error ():
460
+ # Test correct error when passed async function
461
+ async def async_fn (): # pragma: no cover
462
+ pass
463
+
464
+ with pytest .raises (TypeError , match = "expected a sync function" ):
465
+ await to_thread_run_sync (async_fn )
466
+
467
+
459
468
async def test_trio_from_thread_run_sync ():
460
469
# Test that to_thread_run_sync correctly "hands off" the trio token to
461
470
# trio.from_thread.run_sync()
You can’t perform that action at this time.
0 commit comments