29
29
from types import TracebackType
30
30
from typing import (
31
31
TYPE_CHECKING ,
32
- Any ,
33
32
Awaitable ,
34
33
Callable ,
35
34
Optional ,
41
40
)
42
41
43
42
import attr
44
- from typing_extensions import Literal
43
+ from typing_extensions import Literal , ParamSpec
45
44
46
45
from twisted .internet import defer , threads
47
46
from twisted .python .threadpool import ThreadPool
@@ -719,40 +718,41 @@ def nested_logging_context(suffix: str) -> LoggingContext:
719
718
)
720
719
721
720
721
+ P = ParamSpec ("P" )
722
722
R = TypeVar ("R" )
723
723
724
724
725
725
@overload
726
726
def preserve_fn ( # type: ignore[misc]
727
- f : Callable [... , Awaitable [R ]],
728
- ) -> Callable [... , "defer.Deferred[R]" ]:
727
+ f : Callable [P , Awaitable [R ]],
728
+ ) -> Callable [P , "defer.Deferred[R]" ]:
729
729
# The `type: ignore[misc]` above suppresses
730
730
# "Overloaded function signatures 1 and 2 overlap with incompatible return types"
731
731
...
732
732
733
733
734
734
@overload
735
- def preserve_fn (f : Callable [... , R ]) -> Callable [... , "defer.Deferred[R]" ]:
735
+ def preserve_fn (f : Callable [P , R ]) -> Callable [P , "defer.Deferred[R]" ]:
736
736
...
737
737
738
738
739
739
def preserve_fn (
740
740
f : Union [
741
- Callable [... , R ],
742
- Callable [... , Awaitable [R ]],
741
+ Callable [P , R ],
742
+ Callable [P , Awaitable [R ]],
743
743
]
744
- ) -> Callable [... , "defer.Deferred[R]" ]:
744
+ ) -> Callable [P , "defer.Deferred[R]" ]:
745
745
"""Function decorator which wraps the function with run_in_background"""
746
746
747
- def g (* args : Any , ** kwargs : Any ) -> "defer.Deferred[R]" :
747
+ def g (* args : P . args , ** kwargs : P . kwargs ) -> "defer.Deferred[R]" :
748
748
return run_in_background (f , * args , ** kwargs )
749
749
750
750
return g
751
751
752
752
753
753
@overload
754
754
def run_in_background ( # type: ignore[misc]
755
- f : Callable [... , Awaitable [R ]], * args : Any , ** kwargs : Any
755
+ f : Callable [P , Awaitable [R ]], * args : P . args , ** kwargs : P . kwargs
756
756
) -> "defer.Deferred[R]" :
757
757
# The `type: ignore[misc]` above suppresses
758
758
# "Overloaded function signatures 1 and 2 overlap with incompatible return types"
@@ -761,18 +761,22 @@ def run_in_background( # type: ignore[misc]
761
761
762
762
@overload
763
763
def run_in_background (
764
- f : Callable [... , R ], * args : Any , ** kwargs : Any
764
+ f : Callable [P , R ], * args : P . args , ** kwargs : P . kwargs
765
765
) -> "defer.Deferred[R]" :
766
766
...
767
767
768
768
769
- def run_in_background (
769
+ def run_in_background ( # type: ignore[misc]
770
+ # The `type: ignore[misc]` above suppresses
771
+ # "Overloaded function implementation does not accept all possible arguments of signature 1"
772
+ # "Overloaded function implementation does not accept all possible arguments of signature 2"
773
+ # which seems like a bug in mypy.
770
774
f : Union [
771
- Callable [... , R ],
772
- Callable [... , Awaitable [R ]],
775
+ Callable [P , R ],
776
+ Callable [P , Awaitable [R ]],
773
777
],
774
- * args : Any ,
775
- ** kwargs : Any ,
778
+ * args : P . args ,
779
+ ** kwargs : P . kwargs ,
776
780
) -> "defer.Deferred[R]" :
777
781
"""Calls a function, ensuring that the current context is restored after
778
782
return from the function, and that the sentinel context is set once the
@@ -872,7 +876,7 @@ def _set_context_cb(result: ResultT, context: LoggingContext) -> ResultT:
872
876
873
877
874
878
def defer_to_thread (
875
- reactor : "ISynapseReactor" , f : Callable [... , R ], * args : Any , ** kwargs : Any
879
+ reactor : "ISynapseReactor" , f : Callable [P , R ], * args : P . args , ** kwargs : P . kwargs
876
880
) -> "defer.Deferred[R]" :
877
881
"""
878
882
Calls the function `f` using a thread from the reactor's default threadpool and
@@ -908,9 +912,9 @@ def defer_to_thread(
908
912
def defer_to_threadpool (
909
913
reactor : "ISynapseReactor" ,
910
914
threadpool : ThreadPool ,
911
- f : Callable [... , R ],
912
- * args : Any ,
913
- ** kwargs : Any ,
915
+ f : Callable [P , R ],
916
+ * args : P . args ,
917
+ ** kwargs : P . kwargs ,
914
918
) -> "defer.Deferred[R]" :
915
919
"""
916
920
A wrapper for twisted.internet.threads.deferToThreadpool, which handles
0 commit comments