mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-10-01 08:25:44 -04:00
Use the proper Request in type hints. (#9515)
This also pins the Twisted version in the mypy job for CI until proper type hints are fixed throughout Synapse.
This commit is contained in:
parent
e12077a78a
commit
a0bc9d387e
1
changelog.d/9515.misc
Normal file
1
changelog.d/9515.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Fix incorrect type hints.
|
@ -36,7 +36,7 @@ import attr
|
|||||||
import bcrypt
|
import bcrypt
|
||||||
import pymacaroons
|
import pymacaroons
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.constants import LoginType
|
from synapse.api.constants import LoginType
|
||||||
from synapse.api.errors import (
|
from synapse.api.errors import (
|
||||||
@ -481,7 +481,7 @@ class AuthHandler(BaseHandler):
|
|||||||
sid = authdict["session"]
|
sid = authdict["session"]
|
||||||
|
|
||||||
# Convert the URI and method to strings.
|
# Convert the URI and method to strings.
|
||||||
uri = request.uri.decode("utf-8")
|
uri = request.uri.decode("utf-8") # type: ignore
|
||||||
method = request.method.decode("utf-8")
|
method = request.method.decode("utf-8")
|
||||||
|
|
||||||
# If there's no session ID, create a new session.
|
# If there's no session ID, create a new session.
|
||||||
|
@ -31,8 +31,8 @@ from urllib.parse import urlencode
|
|||||||
import attr
|
import attr
|
||||||
from typing_extensions import NoReturn, Protocol
|
from typing_extensions import NoReturn, Protocol
|
||||||
|
|
||||||
from twisted.web.http import Request
|
|
||||||
from twisted.web.iweb import IRequest
|
from twisted.web.iweb import IRequest
|
||||||
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.constants import LoginType
|
from synapse.api.constants import LoginType
|
||||||
from synapse.api.errors import Codes, NotFoundError, RedirectException, SynapseError
|
from synapse.api.errors import Codes, NotFoundError, RedirectException, SynapseError
|
||||||
|
@ -15,9 +15,10 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, List, Optional, Tuple
|
from typing import TYPE_CHECKING, List, Optional, Tuple
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.http.servlet import parse_json_object_from_request
|
from synapse.http.servlet import parse_json_object_from_request
|
||||||
|
from synapse.http.site import SynapseRequest
|
||||||
from synapse.replication.http._base import ReplicationEndpoint
|
from synapse.replication.http._base import ReplicationEndpoint
|
||||||
from synapse.types import JsonDict, Requester, UserID
|
from synapse.types import JsonDict, Requester, UserID
|
||||||
from synapse.util.distributor import user_left_room
|
from synapse.util.distributor import user_left_room
|
||||||
@ -78,7 +79,7 @@ class ReplicationRemoteJoinRestServlet(ReplicationEndpoint):
|
|||||||
}
|
}
|
||||||
|
|
||||||
async def _handle_request( # type: ignore
|
async def _handle_request( # type: ignore
|
||||||
self, request: Request, room_id: str, user_id: str
|
self, request: SynapseRequest, room_id: str, user_id: str
|
||||||
) -> Tuple[int, JsonDict]:
|
) -> Tuple[int, JsonDict]:
|
||||||
content = parse_json_object_from_request(request)
|
content = parse_json_object_from_request(request)
|
||||||
|
|
||||||
@ -86,7 +87,6 @@ class ReplicationRemoteJoinRestServlet(ReplicationEndpoint):
|
|||||||
event_content = content["content"]
|
event_content = content["content"]
|
||||||
|
|
||||||
requester = Requester.deserialize(self.store, content["requester"])
|
requester = Requester.deserialize(self.store, content["requester"])
|
||||||
|
|
||||||
request.requester = requester
|
request.requester = requester
|
||||||
|
|
||||||
logger.info("remote_join: %s into room: %s", user_id, room_id)
|
logger.info("remote_join: %s into room: %s", user_id, room_id)
|
||||||
@ -147,7 +147,7 @@ class ReplicationRemoteRejectInviteRestServlet(ReplicationEndpoint):
|
|||||||
}
|
}
|
||||||
|
|
||||||
async def _handle_request( # type: ignore
|
async def _handle_request( # type: ignore
|
||||||
self, request: Request, invite_event_id: str
|
self, request: SynapseRequest, invite_event_id: str
|
||||||
) -> Tuple[int, JsonDict]:
|
) -> Tuple[int, JsonDict]:
|
||||||
content = parse_json_object_from_request(request)
|
content = parse_json_object_from_request(request)
|
||||||
|
|
||||||
@ -155,7 +155,6 @@ class ReplicationRemoteRejectInviteRestServlet(ReplicationEndpoint):
|
|||||||
event_content = content["content"]
|
event_content = content["content"]
|
||||||
|
|
||||||
requester = Requester.deserialize(self.store, content["requester"])
|
requester = Requester.deserialize(self.store, content["requester"])
|
||||||
|
|
||||||
request.requester = requester
|
request.requester = requester
|
||||||
|
|
||||||
# hopefully we're now on the master, so this won't recurse!
|
# hopefully we're now on the master, so this won't recurse!
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Tuple
|
from typing import TYPE_CHECKING, Tuple
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
|
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
|
||||||
from synapse.http.servlet import RestServlet, parse_boolean, parse_integer
|
from synapse.http.servlet import RestServlet, parse_boolean, parse_integer
|
||||||
|
@ -18,7 +18,7 @@ import logging
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import TYPE_CHECKING, Optional, Tuple
|
from typing import TYPE_CHECKING, Optional, Tuple
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.constants import (
|
from synapse.api.constants import (
|
||||||
MAX_GROUP_CATEGORYID_LENGTH,
|
MAX_GROUP_CATEGORYID_LENGTH,
|
||||||
|
@ -21,7 +21,7 @@ from typing import Awaitable, Dict, Generator, List, Optional, Tuple
|
|||||||
|
|
||||||
from twisted.internet.interfaces import IConsumer
|
from twisted.internet.interfaces import IConsumer
|
||||||
from twisted.protocols.basic import FileSender
|
from twisted.protocols.basic import FileSender
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import Codes, SynapseError, cs_error
|
from synapse.api.errors import Codes, SynapseError, cs_error
|
||||||
from synapse.http.server import finish_request, respond_with_json
|
from synapse.http.server import finish_request, respond_with_json
|
||||||
@ -49,18 +49,20 @@ TEXT_CONTENT_TYPES = [
|
|||||||
|
|
||||||
def parse_media_id(request: Request) -> Tuple[str, str, Optional[str]]:
|
def parse_media_id(request: Request) -> Tuple[str, str, Optional[str]]:
|
||||||
try:
|
try:
|
||||||
|
# The type on postpath seems incorrect in Twisted 21.2.0.
|
||||||
|
postpath = request.postpath # type: List[bytes] # type: ignore
|
||||||
|
assert postpath
|
||||||
|
|
||||||
# This allows users to append e.g. /test.png to the URL. Useful for
|
# This allows users to append e.g. /test.png to the URL. Useful for
|
||||||
# clients that parse the URL to see content type.
|
# clients that parse the URL to see content type.
|
||||||
server_name, media_id = request.postpath[:2]
|
server_name_bytes, media_id_bytes = postpath[:2]
|
||||||
|
server_name = server_name_bytes.decode("utf-8")
|
||||||
if isinstance(server_name, bytes):
|
media_id = media_id_bytes.decode("utf8")
|
||||||
server_name = server_name.decode("utf-8")
|
|
||||||
media_id = media_id.decode("utf8")
|
|
||||||
|
|
||||||
file_name = None
|
file_name = None
|
||||||
if len(request.postpath) > 2:
|
if len(postpath) > 2:
|
||||||
try:
|
try:
|
||||||
file_name = urllib.parse.unquote(request.postpath[-1].decode("utf-8"))
|
file_name = urllib.parse.unquote(postpath[-1].decode("utf-8"))
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
pass
|
pass
|
||||||
return server_name, media_id, file_name
|
return server_name, media_id, file_name
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.http.server import DirectServeJsonResource, respond_with_json
|
from synapse.http.server import DirectServeJsonResource, respond_with_json
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.http.server import DirectServeJsonResource, set_cors_headers
|
from synapse.http.server import DirectServeJsonResource, set_cors_headers
|
||||||
from synapse.http.servlet import parse_boolean
|
from synapse.http.servlet import parse_boolean
|
||||||
|
@ -22,8 +22,8 @@ from typing import IO, TYPE_CHECKING, Dict, List, Optional, Set, Tuple
|
|||||||
|
|
||||||
import twisted.internet.error
|
import twisted.internet.error
|
||||||
import twisted.web.http
|
import twisted.web.http
|
||||||
from twisted.web.http import Request
|
|
||||||
from twisted.web.resource import Resource
|
from twisted.web.resource import Resource
|
||||||
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import (
|
from synapse.api.errors import (
|
||||||
FederationDeniedError,
|
FederationDeniedError,
|
||||||
|
@ -29,7 +29,7 @@ from urllib import parse as urlparse
|
|||||||
import attr
|
import attr
|
||||||
|
|
||||||
from twisted.internet.error import DNSLookupError
|
from twisted.internet.error import DNSLookupError
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import Codes, SynapseError
|
from synapse.api.errors import Codes, SynapseError
|
||||||
from synapse.http.client import SimpleHttpClient
|
from synapse.http.client import SimpleHttpClient
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional
|
from typing import TYPE_CHECKING, Any, Dict, List, Optional
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
from synapse.http.server import DirectServeJsonResource, set_cors_headers
|
from synapse.http.server import DirectServeJsonResource, set_cors_headers
|
||||||
|
@ -15,9 +15,9 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import IO, TYPE_CHECKING
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import Codes, SynapseError
|
from synapse.api.errors import Codes, SynapseError
|
||||||
from synapse.http.server import DirectServeJsonResource, respond_with_json
|
from synapse.http.server import DirectServeJsonResource, respond_with_json
|
||||||
@ -79,7 +79,9 @@ class UploadResource(DirectServeJsonResource):
|
|||||||
headers = request.requestHeaders
|
headers = request.requestHeaders
|
||||||
|
|
||||||
if headers.hasHeader(b"Content-Type"):
|
if headers.hasHeader(b"Content-Type"):
|
||||||
media_type = headers.getRawHeaders(b"Content-Type")[0].decode("ascii")
|
content_type_headers = headers.getRawHeaders(b"Content-Type")
|
||||||
|
assert content_type_headers # for mypy
|
||||||
|
media_type = content_type_headers[0].decode("ascii")
|
||||||
else:
|
else:
|
||||||
raise SynapseError(msg="Upload request missing 'Content-Type'", code=400)
|
raise SynapseError(msg="Upload request missing 'Content-Type'", code=400)
|
||||||
|
|
||||||
@ -88,8 +90,9 @@ class UploadResource(DirectServeJsonResource):
|
|||||||
# TODO(markjh): parse content-dispostion
|
# TODO(markjh): parse content-dispostion
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
content = request.content # type: IO # type: ignore
|
||||||
content_uri = await self.media_repo.create_content(
|
content_uri = await self.media_repo.create_content(
|
||||||
media_type, upload_name, request.content, content_length, requester.user
|
media_type, upload_name, content, content_length, requester.user
|
||||||
)
|
)
|
||||||
except SpamMediaException:
|
except SpamMediaException:
|
||||||
# For uploading of media we want to respond with a 400, instead of
|
# For uploading of media we want to respond with a 400, instead of
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
from synapse.handlers.sso import get_username_mapping_session_cookie_from_request
|
from synapse.handlers.sso import get_username_mapping_session_cookie_from_request
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Tuple
|
from typing import TYPE_CHECKING, Tuple
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import ThreepidValidationError
|
from synapse.api.errors import ThreepidValidationError
|
||||||
from synapse.config.emailconfig import ThreepidBehaviour
|
from synapse.config.emailconfig import ThreepidBehaviour
|
||||||
|
@ -16,8 +16,8 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, List
|
from typing import TYPE_CHECKING, List
|
||||||
|
|
||||||
from twisted.web.http import Request
|
|
||||||
from twisted.web.resource import Resource
|
from twisted.web.resource import Resource
|
||||||
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
from synapse.handlers.sso import get_username_mapping_session_cookie_from_request
|
from synapse.handlers.sso import get_username_mapping_session_cookie_from_request
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from twisted.web.http import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
from synapse.handlers.sso import get_username_mapping_session_cookie_from_request
|
from synapse.handlers.sso import get_username_mapping_session_cookie_from_request
|
||||||
|
2
tox.ini
2
tox.ini
@ -189,5 +189,7 @@ commands=
|
|||||||
[testenv:mypy]
|
[testenv:mypy]
|
||||||
deps =
|
deps =
|
||||||
{[base]deps}
|
{[base]deps}
|
||||||
|
# Type hints are broken with Twisted > 20.3.0, see https://github.com/matrix-org/synapse/issues/9513
|
||||||
|
twisted==20.3.0
|
||||||
extras = all,mypy
|
extras = all,mypy
|
||||||
commands = mypy
|
commands = mypy
|
||||||
|
Loading…
Reference in New Issue
Block a user