-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Use the proper Request in type hints. #9515
Changes from 7 commits
82df4c4
ad57f41
bea9314
d54cf70
16b96eb
c040e42
b0e0c72
d91327d
8cbc815
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix incorrect type hints. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
import logging | ||
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.replication.http._base import ReplicationEndpoint | ||
|
@@ -85,10 +85,6 @@ async def _handle_request( # type: ignore | |
remote_room_hosts = content["remote_room_hosts"] | ||
event_content = content["content"] | ||
|
||
requester = Requester.deserialize(self.store, content["requester"]) | ||
|
||
request.requester = requester | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here (and below) the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How confident are we? If we're getting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you're asking. Are you asking if we're confident in this change or if the code before this change might have been masking other bugs or something? Note that this is the replication API, which I don't think has any concept of requester? The @erikjohnston do you know what the proper thing to do here is? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think is so that the requester is logged, rather than the default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, OK. The weird thing is that that implies this is actually a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 8cbc815 reverts removing this. |
||
|
||
logger.info("remote_join: %s into room: %s", user_id, room_id) | ||
|
||
event_id, stream_id = await self.federation_handler.do_invite_join( | ||
|
@@ -156,8 +152,6 @@ async def _handle_request( # type: ignore | |
|
||
requester = Requester.deserialize(self.store, content["requester"]) | ||
|
||
request.requester = requester | ||
|
||
# hopefully we're now on the master, so this won't recurse! | ||
event_id, stream_id = await self.member_handler.remote_reject_invite( | ||
invite_event_id, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
|
||
from twisted.internet.interfaces import IConsumer | ||
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.http.server import finish_request, respond_with_json | ||
|
@@ -49,18 +49,20 @@ | |
|
||
def parse_media_id(request: Request) -> Tuple[str, str, Optional[str]]: | ||
try: | ||
# The type on postpath seems incorrect in Twisted. | ||
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
postpath = request.postpath # type: List[bytes] # type: ignore | ||
assert postpath | ||
|
||
# This allows users to append e.g. /test.png to the URL. Useful for | ||
# clients that parse the URL to see content type. | ||
server_name, media_id = request.postpath[:2] | ||
|
||
if isinstance(server_name, bytes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is always bytes so I don't think we need this if-statement. |
||
server_name = server_name.decode("utf-8") | ||
media_id = media_id.decode("utf8") | ||
server_name_bytes, media_id_bytes = postpath[:2] | ||
server_name = server_name_bytes.decode("utf-8") | ||
media_id = media_id_bytes.decode("utf8") | ||
|
||
file_name = None | ||
if len(request.postpath) > 2: | ||
if len(postpath) > 2: | ||
try: | ||
file_name = urllib.parse.unquote(request.postpath[-1].decode("utf-8")) | ||
file_name = urllib.parse.unquote(postpath[-1].decode("utf-8")) | ||
except UnicodeDecodeError: | ||
pass | ||
return server_name, media_id, file_name | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,9 @@ | |
# limitations under the License. | ||
|
||
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.http.server import DirectServeJsonResource, respond_with_json | ||
|
@@ -79,7 +79,9 @@ async def _async_render_POST(self, request: Request) -> None: | |
headers = request.requestHeaders | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We know that the header will exist due to the above check, but mypy doesn't... |
||
media_type = content_type_headers[0].decode("ascii") | ||
else: | ||
raise SynapseError(msg="Upload request missing 'Content-Type'", code=400) | ||
|
||
|
@@ -88,8 +90,9 @@ async def _async_render_POST(self, request: Request) -> None: | |
# TODO(markjh): parse content-dispostion | ||
|
||
try: | ||
content = request.content # type: IO # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type of this is undefined right now in Twisted (I think)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It overrides the definition previously calculated. |
||
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: | ||
# For uploading of media we want to respond with a 400, instead of | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of
request.uri
is incorrectly specified in Twisted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well it isn't "incorrect", but the placeholder is a string and the value is bytes, which is annoying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Maybe make notes on the places where this happens, and submit that issue on
twisted/twisted
?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm already planning to.