29
29
CancelScope ,
30
30
CapacityLimiter ,
31
31
Event ,
32
+ TrioDeprecationWarning ,
32
33
_core ,
33
34
fail_after ,
34
35
move_on_after ,
@@ -337,10 +338,10 @@ def f(q: stdlib_queue.Queue[str]) -> None:
337
338
q .get ()
338
339
register [0 ] = "finished"
339
340
340
- async def child (q : stdlib_queue .Queue [None ], cancellable : bool ) -> None :
341
+ async def child (q : stdlib_queue .Queue [None ], abandon_on_cancel : bool ) -> None :
341
342
record .append ("start" )
342
343
try :
343
- return await to_thread_run_sync (f , q , cancellable = cancellable )
344
+ return await to_thread_run_sync (f , q , abandon_on_cancel = abandon_on_cancel )
344
345
finally :
345
346
record .append ("exit" )
346
347
@@ -402,7 +403,7 @@ def thread_fn() -> None:
402
403
403
404
async def main () -> None :
404
405
async def child () -> None :
405
- await to_thread_run_sync (thread_fn , cancellable = True )
406
+ await to_thread_run_sync (thread_fn , abandon_on_cancel = True )
406
407
407
408
async with _core .open_nursery () as nursery :
408
409
nursery .start_soon (child )
@@ -491,7 +492,10 @@ def thread_fn(cancel_scope: CancelScope) -> None:
491
492
async def run_thread (event : Event ) -> None :
492
493
with _core .CancelScope () as cancel_scope :
493
494
await to_thread_run_sync (
494
- thread_fn , cancel_scope , limiter = limiter_arg , cancellable = cancel
495
+ thread_fn ,
496
+ cancel_scope ,
497
+ abandon_on_cancel = cancel ,
498
+ limiter = limiter_arg ,
495
499
)
496
500
print ("run_thread finished, cancelled:" , cancel_scope .cancelled_caught )
497
501
event .set ()
@@ -553,7 +557,7 @@ def release_on_behalf_of(self, borrower: Task) -> None:
553
557
554
558
# TODO: should CapacityLimiter have an abc or protocol so users can modify it?
555
559
# because currently it's `final` so writing code like this is not allowed.
556
- await to_thread_run_sync (lambda : None , limiter = CustomLimiter ()) # type: ignore[arg-type ]
560
+ await to_thread_run_sync (lambda : None , limiter = CustomLimiter ()) # type: ignore[call-overload ]
557
561
assert record == ["acquire" , "release" ]
558
562
559
563
@@ -571,7 +575,7 @@ def release_on_behalf_of(self, borrower: Task) -> NoReturn:
571
575
bs = BadCapacityLimiter ()
572
576
573
577
with pytest .raises (ValueError ) as excinfo :
574
- await to_thread_run_sync (lambda : None , limiter = bs ) # type: ignore[arg-type ]
578
+ await to_thread_run_sync (lambda : None , limiter = bs ) # type: ignore[call-overload ]
575
579
assert excinfo .value .__context__ is None
576
580
assert record == ["acquire" , "release" ]
577
581
record = []
@@ -580,7 +584,7 @@ def release_on_behalf_of(self, borrower: Task) -> NoReturn:
580
584
# chains with it
581
585
d : dict [str , object ] = {}
582
586
with pytest .raises (ValueError ) as excinfo :
583
- await to_thread_run_sync (lambda : d ["x" ], limiter = bs ) # type: ignore[arg-type ]
587
+ await to_thread_run_sync (lambda : d ["x" ], limiter = bs ) # type: ignore[call-overload ]
584
588
assert isinstance (excinfo .value .__context__ , KeyError )
585
589
assert record == ["acquire" , "release" ]
586
590
@@ -881,15 +885,15 @@ async def test_trio_token_weak_referenceable() -> None:
881
885
assert token is weak_reference ()
882
886
883
887
884
- async def test_unsafe_cancellable_kwarg () -> None :
888
+ async def test_unsafe_abandon_on_cancel_kwarg () -> None :
885
889
# This is a stand in for a numpy ndarray or other objects
886
890
# that (maybe surprisingly) lack a notion of truthiness
887
891
class BadBool :
888
892
def __bool__ (self ) -> bool :
889
893
raise NotImplementedError
890
894
891
895
with pytest .raises (NotImplementedError ):
892
- await to_thread_run_sync (int , cancellable = BadBool ()) # type: ignore[arg-type ]
896
+ await to_thread_run_sync (int , abandon_on_cancel = BadBool ()) # type: ignore[call-overload ]
893
897
894
898
895
899
async def test_from_thread_reuses_task () -> None :
@@ -933,7 +937,7 @@ def sync_check() -> None:
933
937
assert not queue .get_nowait ()
934
938
935
939
with _core .CancelScope () as cancel_scope :
936
- await to_thread_run_sync (sync_check , cancellable = True )
940
+ await to_thread_run_sync (sync_check , abandon_on_cancel = True )
937
941
938
942
assert cancel_scope .cancelled_caught
939
943
assert not await to_thread_run_sync (partial (queue .get , timeout = 1 ))
@@ -957,7 +961,7 @@ def async_check() -> None:
957
961
assert not queue .get_nowait ()
958
962
959
963
with _core .CancelScope () as cancel_scope :
960
- await to_thread_run_sync (async_check , cancellable = True )
964
+ await to_thread_run_sync (async_check , abandon_on_cancel = True )
961
965
962
966
assert cancel_scope .cancelled_caught
963
967
assert not await to_thread_run_sync (partial (queue .get , timeout = 1 ))
@@ -976,11 +980,11 @@ async def async_time_bomb() -> None:
976
980
async def test_from_thread_check_cancelled () -> None :
977
981
q : stdlib_queue .Queue [str ] = stdlib_queue .Queue ()
978
982
979
- async def child (cancellable : bool , scope : CancelScope ) -> None :
983
+ async def child (abandon_on_cancel : bool , scope : CancelScope ) -> None :
980
984
with scope :
981
985
record .append ("start" )
982
986
try :
983
- return await to_thread_run_sync (f , cancellable = cancellable )
987
+ return await to_thread_run_sync (f , abandon_on_cancel = abandon_on_cancel )
984
988
except _core .Cancelled :
985
989
record .append ("cancel" )
986
990
raise
@@ -1009,7 +1013,7 @@ def f() -> None:
1009
1013
# implicit assertion, Cancelled not raised via nursery
1010
1014
assert record [1 ] == "exit"
1011
1015
1012
- # cancellable =False case: a cancel will pop out but be handled by
1016
+ # abandon_on_cancel =False case: a cancel will pop out but be handled by
1013
1017
# the appropriate cancel scope
1014
1018
record = []
1015
1019
ev = threading .Event ()
@@ -1025,7 +1029,7 @@ def f() -> None:
1025
1029
assert "cancel" in record
1026
1030
assert record [- 1 ] == "exit"
1027
1031
1028
- # cancellable =True case: slightly different thread behavior needed
1032
+ # abandon_on_cancel =True case: slightly different thread behavior needed
1029
1033
# check thread is cancelled "soon" after abandonment
1030
1034
def f () -> None : # type: ignore[no-redef] # noqa: F811
1031
1035
ev .wait ()
@@ -1068,9 +1072,25 @@ async def test_reentry_doesnt_deadlock() -> None:
1068
1072
1069
1073
async def child () -> None :
1070
1074
while True :
1071
- await to_thread_run_sync (from_thread_run , sleep , 0 , cancellable = False )
1075
+ await to_thread_run_sync (from_thread_run , sleep , 0 , abandon_on_cancel = False )
1072
1076
1073
1077
with move_on_after (2 ):
1074
1078
async with _core .open_nursery () as nursery :
1075
1079
for _ in range (4 ):
1076
1080
nursery .start_soon (child )
1081
+
1082
+
1083
+ async def test_cancellable_and_abandon_raises () -> None :
1084
+ with pytest .raises (ValueError ):
1085
+ await to_thread_run_sync (bool , cancellable = True , abandon_on_cancel = False ) # type: ignore[call-overload]
1086
+
1087
+ with pytest .raises (ValueError ):
1088
+ await to_thread_run_sync (bool , cancellable = True , abandon_on_cancel = True ) # type: ignore[call-overload]
1089
+
1090
+
1091
+ async def test_cancellable_warns () -> None :
1092
+ with pytest .warns (TrioDeprecationWarning ):
1093
+ await to_thread_run_sync (bool , cancellable = False )
1094
+
1095
+ with pytest .warns (TrioDeprecationWarning ):
1096
+ await to_thread_run_sync (bool , cancellable = True )
0 commit comments