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

Commit

Permalink
Add type hints to wrapper method for registering endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Dec 1, 2021
1 parent 8b0b3c3 commit 6872bcf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ disallow_untyped_defs = True
[mypy-synapse.federation.transport.client]
disallow_untyped_defs = False

[mypy-synapse.federation.transport.server._base]
disallow_untyped_defs = False

[mypy-synapse.handlers.*]
disallow_untyped_defs = True

Expand Down
26 changes: 15 additions & 11 deletions synapse/federation/transport/server/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import functools
import logging
import re
from typing import Optional, Tuple
from typing import Any, Awaitable, Callable, Optional, Tuple, cast

from synapse.api.errors import Codes, FederationDeniedError, SynapseError
from synapse.api.urls import FEDERATION_V1_PREFIX
from synapse.http.server import HttpServer
from synapse.http.server import HttpServer, ServletCallback
from synapse.http.servlet import parse_json_object_from_request
from synapse.http.site import SynapseRequest
from synapse.logging import opentracing
Expand Down Expand Up @@ -239,23 +239,27 @@ def __init__(
self.ratelimiter = ratelimiter
self.server_name = server_name

def _wrap(self, func):
def _wrap(
self, func: Callable[..., Awaitable[Tuple[int, JsonDict]]]
) -> ServletCallback:
authenticator = self.authenticator
ratelimiter = self.ratelimiter

@functools.wraps(func)
async def new_func(request, *args, **kwargs):
async def new_func(
request: SynapseRequest, *args: Any, **kwargs: str
) -> Optional[Tuple[int, Any]]:
"""A callback which can be passed to HttpServer.RegisterPaths
Args:
request (twisted.web.http.Request):
request:
*args: unused?
**kwargs (dict[unicode, unicode]): the dict mapping keys to path
components as specified in the path match regexp.
**kwargs: the dict mapping keys to path components as specified
in the path match regexp.
Returns:
Tuple[int, object]|None: (response code, response object) as returned by
the callback method. None if the request has already been handled.
(response code, response object) as returned by the callback method.
None if the request has already been handled.
"""
content = None
if request.method in [b"PUT", b"POST"]:
Expand Down Expand Up @@ -309,7 +313,7 @@ async def new_func(request, *args, **kwargs):
"client disconnected before we started processing "
"request"
)
return -1, None
return None
response = await func(
origin, content, request.args, *args, **kwargs
)
Expand All @@ -320,7 +324,7 @@ async def new_func(request, *args, **kwargs):

return response

return new_func
return cast(ServletCallback, new_func)

def register(self, server: HttpServer) -> None:
pattern = re.compile("^" + self.PREFIX + self.PATH + "$")
Expand Down

0 comments on commit 6872bcf

Please sign in to comment.