Skip to content

Commit

Permalink
Refactor _get_str_query to avoid intermediate variable (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 8, 2024
1 parent 92dda6d commit 7d61055
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,29 +1204,27 @@ def _get_str_query(self, *args: Any, **kwargs: Any) -> Union[str, None]:
raise ValueError("Either kwargs or single query parameter must be present")

if query is None:
query = None
elif isinstance(query, Mapping):
return None
if isinstance(query, Mapping):
quoter = self._QUERY_PART_QUOTER
query = "&".join(self._query_seq_pairs(quoter, query.items()))
elif isinstance(query, str):
query = self._QUERY_QUOTER(query)
elif isinstance(query, (bytes, bytearray, memoryview)):
return "&".join(self._query_seq_pairs(quoter, query.items()))
if isinstance(query, str):
return self._QUERY_QUOTER(query)
if isinstance(query, (bytes, bytearray, memoryview)):
raise TypeError(
"Invalid query type: bytes, bytearray and memoryview are forbidden"
)
elif isinstance(query, Sequence):
if isinstance(query, Sequence):
# We don't expect sequence values if we're given a list of pairs
# already; only mappings like builtin `dict` which can't have the
# same key pointing to multiple values are allowed to use
# `_query_seq_pairs`.
return self._get_str_query_from_iterable(query)
else:
raise TypeError(
"Invalid query type: only str, mapping or "
"sequence of (key, value) pairs is allowed"
)

return query
raise TypeError(
"Invalid query type: only str, mapping or "
"sequence of (key, value) pairs is allowed"
)

@overload
def with_query(self, query: Query) -> "URL": ...
Expand Down

0 comments on commit 7d61055

Please sign in to comment.