Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to query_update(None) #792

Merged
merged 5 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/792.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an issue with update_query() not getting rid of the query when argument is None.
6 changes: 6 additions & 0 deletions tests/test_update_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def test_update_query_with_multiple_args():
url.update_query("a", "b")


def test_update_query_with_none_arg():
url = URL("http://example.com/?foo=bar&baz=foo")
expected_url = URL("http://example.com/")
assert url.update_query(None) == expected_url


def test_with_query_list_of_pairs():
url = URL("http://example.com")
assert str(url.with_query([("a", "1")])) == "http://example.com/?a=1"
Expand Down
8 changes: 5 additions & 3 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,9 +982,11 @@ def with_query(self, *args, **kwargs):
def update_query(self, *args, **kwargs):
"""Return a new URL with query part updated."""
s = self._get_str_query(*args, **kwargs)
new_query = MultiDict(parse_qsl(s, keep_blank_values=True))
query = MultiDict(self.query)
query.update(new_query)
query = None
if s:
new_query = MultiDict(parse_qsl(s, keep_blank_values=True))
query = MultiDict(self.query)
query.update(new_query)

return URL(self._val._replace(query=self._get_str_query(query)), encoded=True)

Expand Down