This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Unify HTTP query parameter type hints #12415
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b4a5824
Pull out query param types to `synapse.http.types`
2950867
Try using QueryParams everywhere
904d57f
I think `encode_query_args` is redundant.
3f2e236
Note that `doseq=True` is required
25c9127
MatrixFederationRequest.query: QueryParams
00f22d7
The helper function already deals with `None`
8e0267f
Propagate `QueryParams` upwards to make_query
85a8086
Add annotation which would have caught #12410
05b6d6c
Changelog
e793952
License header
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve type hints related to HTTP query parameters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ | |
read_body_with_max_size, | ||
) | ||
from synapse.http.federation.matrix_federation_agent import MatrixFederationAgent | ||
from synapse.http.types import QueryParams | ||
from synapse.logging import opentracing | ||
from synapse.logging.context import make_deferred_yieldable, run_in_background | ||
from synapse.logging.opentracing import set_tag, start_active_span, tags | ||
|
@@ -98,10 +99,6 @@ | |
|
||
_next_id = 1 | ||
|
||
|
||
QueryArgs = Dict[str, Union[str, List[str]]] | ||
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 pretty much just had two implementations of the same thing? 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. |
||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
|
@@ -144,7 +141,7 @@ class MatrixFederationRequest: | |
"""A callback to generate the JSON. | ||
""" | ||
|
||
query: Optional[dict] = None | ||
query: Optional[QueryParams] = None | ||
"""Query arguments. | ||
""" | ||
|
||
|
@@ -165,10 +162,7 @@ def __attrs_post_init__(self) -> None: | |
|
||
destination_bytes = self.destination.encode("ascii") | ||
path_bytes = self.path.encode("ascii") | ||
if self.query: | ||
query_bytes = encode_query_args(self.query) | ||
else: | ||
query_bytes = b"" | ||
Comment on lines
-168
to
-171
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. Does this also work properly if you pass an empty dictionary into 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 hadn't thought of this, but fortunately it will work:
|
||
query_bytes = encode_query_args(self.query) | ||
|
||
# The object is frozen so we can pre-compute this. | ||
uri = urllib.parse.urlunparse( | ||
|
@@ -485,10 +479,7 @@ async def _send_request( | |
method_bytes = request.method.encode("ascii") | ||
destination_bytes = request.destination.encode("ascii") | ||
path_bytes = request.path.encode("ascii") | ||
if request.query: | ||
query_bytes = encode_query_args(request.query) | ||
else: | ||
query_bytes = b"" | ||
query_bytes = encode_query_args(request.query) | ||
|
||
scope = start_active_span( | ||
"outgoing-federation-request", | ||
|
@@ -746,7 +737,7 @@ async def put_json( | |
self, | ||
destination: str, | ||
path: str, | ||
args: Optional[QueryArgs] = None, | ||
args: Optional[QueryParams] = None, | ||
data: Optional[JsonDict] = None, | ||
json_data_callback: Optional[Callable[[], JsonDict]] = None, | ||
long_retries: bool = False, | ||
|
@@ -764,7 +755,7 @@ async def put_json( | |
self, | ||
destination: str, | ||
path: str, | ||
args: Optional[QueryArgs] = None, | ||
args: Optional[QueryParams] = None, | ||
data: Optional[JsonDict] = None, | ||
json_data_callback: Optional[Callable[[], JsonDict]] = None, | ||
long_retries: bool = False, | ||
|
@@ -781,7 +772,7 @@ async def put_json( | |
self, | ||
destination: str, | ||
path: str, | ||
args: Optional[QueryArgs] = None, | ||
args: Optional[QueryParams] = None, | ||
data: Optional[JsonDict] = None, | ||
json_data_callback: Optional[Callable[[], JsonDict]] = None, | ||
long_retries: bool = False, | ||
|
@@ -891,7 +882,7 @@ async def post_json( | |
long_retries: bool = False, | ||
timeout: Optional[int] = None, | ||
ignore_backoff: bool = False, | ||
args: Optional[QueryArgs] = None, | ||
args: Optional[QueryParams] = None, | ||
) -> Union[JsonDict, list]: | ||
"""Sends the specified json data using POST | ||
|
||
|
@@ -961,7 +952,7 @@ async def get_json( | |
self, | ||
destination: str, | ||
path: str, | ||
args: Optional[QueryArgs] = None, | ||
args: Optional[QueryParams] = None, | ||
retry_on_dns_fail: bool = True, | ||
timeout: Optional[int] = None, | ||
ignore_backoff: bool = False, | ||
|
@@ -976,7 +967,7 @@ async def get_json( | |
self, | ||
destination: str, | ||
path: str, | ||
args: Optional[QueryArgs] = ..., | ||
args: Optional[QueryParams] = ..., | ||
retry_on_dns_fail: bool = ..., | ||
timeout: Optional[int] = ..., | ||
ignore_backoff: bool = ..., | ||
|
@@ -990,7 +981,7 @@ async def get_json( | |
self, | ||
destination: str, | ||
path: str, | ||
args: Optional[QueryArgs] = None, | ||
args: Optional[QueryParams] = None, | ||
retry_on_dns_fail: bool = True, | ||
timeout: Optional[int] = None, | ||
ignore_backoff: bool = False, | ||
|
@@ -1085,7 +1076,7 @@ async def delete_json( | |
long_retries: bool = False, | ||
timeout: Optional[int] = None, | ||
ignore_backoff: bool = False, | ||
args: Optional[QueryArgs] = None, | ||
args: Optional[QueryParams] = None, | ||
) -> Union[JsonDict, list]: | ||
"""Send a DELETE request to the remote expecting some json response | ||
|
||
|
@@ -1150,7 +1141,7 @@ async def get_file( | |
destination: str, | ||
path: str, | ||
output_stream, | ||
args: Optional[QueryArgs] = None, | ||
args: Optional[QueryParams] = None, | ||
retry_on_dns_fail: bool = True, | ||
max_size: Optional[int] = None, | ||
ignore_backoff: bool = False, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from typing import Iterable, Mapping, Union | ||
DMRobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# the type of the query params, to be passed into `urlencode` with `doseq=True`. | ||
QueryParamValue = Union[str, bytes, Iterable[Union[str, bytes]]] | ||
QueryParams = Union[Mapping[str, QueryParamValue], Mapping[bytes, QueryParamValue]] | ||
|
||
__all__ = ["QueryParams"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does it make sense to use
MutableMapping
forQueryParams
(or have aMutableQueryParams
)?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 think I prefer it keeping the (immutable) Mapping. The non-mutable version expresses that functions taking a
QueryParams
won't mutate those parameters. It's inconvenient here if you conditionally build up a parameter dictionary, but that's rare: we almost always build a parameter dictionaryargs
as a dictionary literal and pass it immediately to an HTTP function (without further mutatingargs
).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.
That seems fine! 👍