Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 5627182

Browse files
authored
Use ParamSpec in type hints for synapse.logging.context (#12150)
Signed-off-by: Sean Quah <[email protected]>
1 parent 0dc9c56 commit 5627182

File tree

5 files changed

+37
-25
lines changed

5 files changed

+37
-25
lines changed

changelog.d/12150.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use `ParamSpec` in type hints for `synapse.logging.context`.

synapse/handlers/initial_sync.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ async def _snapshot_all_rooms(
153153

154154
public_room_ids = await self.store.get_public_room_ids()
155155

156-
limit = pagin_config.limit
157-
if limit is None:
156+
if pagin_config.limit is not None:
157+
limit = pagin_config.limit
158+
else:
158159
limit = 10
159160

160161
serializer_options = SerializeEventConfig(as_client_event=as_client_event)

synapse/logging/context.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from types import TracebackType
3030
from typing import (
3131
TYPE_CHECKING,
32-
Any,
3332
Awaitable,
3433
Callable,
3534
Optional,
@@ -41,7 +40,7 @@
4140
)
4241

4342
import attr
44-
from typing_extensions import Literal
43+
from typing_extensions import Literal, ParamSpec
4544

4645
from twisted.internet import defer, threads
4746
from twisted.python.threadpool import ThreadPool
@@ -719,40 +718,41 @@ def nested_logging_context(suffix: str) -> LoggingContext:
719718
)
720719

721720

721+
P = ParamSpec("P")
722722
R = TypeVar("R")
723723

724724

725725
@overload
726726
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]"]:
729729
# The `type: ignore[misc]` above suppresses
730730
# "Overloaded function signatures 1 and 2 overlap with incompatible return types"
731731
...
732732

733733

734734
@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]"]:
736736
...
737737

738738

739739
def preserve_fn(
740740
f: Union[
741-
Callable[..., R],
742-
Callable[..., Awaitable[R]],
741+
Callable[P, R],
742+
Callable[P, Awaitable[R]],
743743
]
744-
) -> Callable[..., "defer.Deferred[R]"]:
744+
) -> Callable[P, "defer.Deferred[R]"]:
745745
"""Function decorator which wraps the function with run_in_background"""
746746

747-
def g(*args: Any, **kwargs: Any) -> "defer.Deferred[R]":
747+
def g(*args: P.args, **kwargs: P.kwargs) -> "defer.Deferred[R]":
748748
return run_in_background(f, *args, **kwargs)
749749

750750
return g
751751

752752

753753
@overload
754754
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
756756
) -> "defer.Deferred[R]":
757757
# The `type: ignore[misc]` above suppresses
758758
# "Overloaded function signatures 1 and 2 overlap with incompatible return types"
@@ -761,18 +761,22 @@ def run_in_background( # type: ignore[misc]
761761

762762
@overload
763763
def run_in_background(
764-
f: Callable[..., R], *args: Any, **kwargs: Any
764+
f: Callable[P, R], *args: P.args, **kwargs: P.kwargs
765765
) -> "defer.Deferred[R]":
766766
...
767767

768768

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.
770774
f: Union[
771-
Callable[..., R],
772-
Callable[..., Awaitable[R]],
775+
Callable[P, R],
776+
Callable[P, Awaitable[R]],
773777
],
774-
*args: Any,
775-
**kwargs: Any,
778+
*args: P.args,
779+
**kwargs: P.kwargs,
776780
) -> "defer.Deferred[R]":
777781
"""Calls a function, ensuring that the current context is restored after
778782
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:
872876

873877

874878
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
876880
) -> "defer.Deferred[R]":
877881
"""
878882
Calls the function `f` using a thread from the reactor's default threadpool and
@@ -908,9 +912,9 @@ def defer_to_thread(
908912
def defer_to_threadpool(
909913
reactor: "ISynapseReactor",
910914
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,
914918
) -> "defer.Deferred[R]":
915919
"""
916920
A wrapper for twisted.internet.threads.deferToThreadpool, which handles

synapse/python_dependencies.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
"netaddr>=0.7.18",
7777
"Jinja2>=2.9",
7878
"bleach>=1.4.3",
79-
"typing-extensions>=3.7.4",
79+
# We use `ParamSpec`, which was added in `typing-extensions` 3.10.0.0.
80+
"typing-extensions>=3.10.0",
8081
# We enforce that we have a `cryptography` version that bundles an `openssl`
8182
# with the latest security patches.
8283
"cryptography>=3.4.7",

synapse/rest/media/v1/storage_provider.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logging
1717
import os
1818
import shutil
19-
from typing import TYPE_CHECKING, Optional
19+
from typing import TYPE_CHECKING, Callable, Optional
2020

2121
from synapse.config._base import Config
2222
from synapse.logging.context import defer_to_thread, run_in_background
@@ -150,8 +150,13 @@ async def store_file(self, path: str, file_info: FileInfo) -> None:
150150
dirname = os.path.dirname(backup_fname)
151151
os.makedirs(dirname, exist_ok=True)
152152

153+
# mypy needs help inferring the type of the second parameter, which is generic
154+
shutil_copyfile: Callable[[str, str], str] = shutil.copyfile
153155
await defer_to_thread(
154-
self.hs.get_reactor(), shutil.copyfile, primary_fname, backup_fname
156+
self.hs.get_reactor(),
157+
shutil_copyfile,
158+
primary_fname,
159+
backup_fname,
155160
)
156161

157162
async def fetch(self, path: str, file_info: FileInfo) -> Optional[Responder]:

0 commit comments

Comments
 (0)