diff --git a/CHANGES/845.bugfix.rst b/CHANGES/845.bugfix.rst new file mode 100644 index 000000000..fbc7a70af --- /dev/null +++ b/CHANGES/845.bugfix.rst @@ -0,0 +1,2 @@ +Fixed an issue with ``update_query()`` getting rid of the query when the argument +is empty but not ``None``. diff --git a/yarl/_url.py b/yarl/_url.py index e21c5e96e..3ddb1fa8f 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -956,7 +956,7 @@ def _get_str_query(self, *args, **kwargs): raise ValueError("Either kwargs or single query parameter must be present") if query is None: - query = "" + query = None elif isinstance(query, Mapping): quoter = self._QUERY_PART_QUOTER query = "&".join(self._query_seq_pairs(quoter, query.items())) @@ -998,7 +998,7 @@ def with_query(self, *args, **kwargs): """ # N.B. doesn't cleanup query/fragment - new_query = self._get_str_query(*args, **kwargs) + new_query = self._get_str_query(*args, **kwargs) or "" return URL( self._val._replace(path=self._val.path, query=new_query), encoded=True ) @@ -1007,12 +1007,14 @@ def update_query(self, *args, **kwargs): """Return a new URL with query part updated.""" s = self._get_str_query(*args, **kwargs) query = None - if s: + if s is not None: 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) + return URL( + self._val._replace(query=self._get_str_query(query) or ""), encoded=True + ) def with_fragment(self, fragment): """Return a new URL with fragment replaced.