Skip to content

Commit

Permalink
allow to update query string using mod operator
Browse files Browse the repository at this point in the history
  • Loading branch information
vanyakosmos committed Apr 4, 2020
1 parent 78f49dc commit f57d0eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/test_update_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,12 @@ def test_update_query_multiple_keys():
u2 = url.update_query([("a", "3"), ("a", "4")])

assert str(u2) == "http://example.com/path?a=3&a=4"


# mod

def test_update_query_with_mod_operator():
url = URL("http://example.com/")
assert str(url % {"a": "1"}) == "http://example.com/?a=1"
assert str(url % {"a": "1"} % {"b": "2"}) == "http://example.com/?a=1&b=2"
assert str(url % {"a": "1"} % {"a": "3", "b": "2"}) == "http://example.com/?a=3&b=2"
3 changes: 3 additions & 0 deletions yarl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ def __truediv__(self, name):
return URL(
self._val._replace(path=new_path, query="", fragment=""), encoded=True
)

def __mod__(self, query):
return self.update_query(query)

def __bool__(self) -> bool:
return bool(
Expand Down

0 comments on commit f57d0eb

Please sign in to comment.