Skip to content

Commit

Permalink
Fix to query_update(None) (#792)
Browse files Browse the repository at this point in the history
* Handled case where update_query() is handed None as an argument. Added corresponding test file as well.

* Added Changes file

* Rename 790.bugfix.rst to 792.bugfix.rst

Co-authored-by: Andrew Svetlov <[email protected]>
  • Loading branch information
dereckt and asvetlov authored Dec 12, 2022
1 parent 94b8679 commit dd86b34
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
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 @@ -985,9 +985,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

0 comments on commit dd86b34

Please sign in to comment.