Skip to content

Commit a7622de

Browse files
Finish annotating query_items parameters in builder.py.
- These are passed into `urllib.parse.urlencode()`, so the annotation for that was copied from typeshed and modified slightly. - `_QueryType` in typeshed uses `typing.Sequence` to account for different types of sequence values being passed in, but `URLBuilder.extend_query_with()` in builder.py uses `isinstance(query_items, list)` as flow control, so Sequence is too wide to account for what that method allows. Thus, the type was modified to use `typing.List` in place of `typing.Sequence` where necessary. - Arguably, that isinstance check should be changed to check against `Sequence` instead, but I'd prefer having that double-checked, and this PR's scope is currently mostly limited to annotations anyway. This can be revisited later if necessary. - TODO: Ask if `username is None` check is still needed in `URLBuilder.add_credentials()` if the `username` parameter is annotated with `str`.
1 parent 6c3d109 commit a7622de

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/rfc3986/builder.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
from . import uri
2121
from . import uri_reference
2222

23+
# Copied from urllib.parse in typeshed.
24+
_QueryType = t.Union[
25+
t.Mapping[t.Any, t.Any],
26+
t.Mapping[t.Any, t.Sequence[t.Any]],
27+
t.List[t.Tuple[t.Any, t.Any]],
28+
t.List[t.Tuple[t.Any, t.Sequence[t.Any]]],
29+
]
30+
2331

2432
class URIBuilder:
2533
"""Object to aid in building up a URI Reference from parts.
@@ -268,7 +276,7 @@ def extend_path(self, path: str):
268276

269277
return self.add_path(path)
270278

271-
def add_query_from(self, query_items):
279+
def add_query_from(self, query_items: _QueryType):
272280
"""Generate and add a query a dictionary or list of tuples.
273281
274282
.. code-block:: python
@@ -294,7 +302,7 @@ def add_query_from(self, query_items):
294302
fragment=self.fragment,
295303
)
296304

297-
def extend_query_with(self, query_items):
305+
def extend_query_with(self, query_items: _QueryType):
298306
"""Extend the existing query string with the new query items.
299307
300308
.. versionadded:: 1.5.0

0 commit comments

Comments
 (0)