From 86197cebe168fa59dd7c5ebc1b2fe9ae9dec038e Mon Sep 17 00:00:00 2001 From: Martijn Pieters Date: Wed, 19 Apr 2023 16:18:00 +0100 Subject: [PATCH] update_query({}) should be a no-op. --- CHANGES/845.bugfix.rst | 2 ++ yarl/_url.py | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 CHANGES/845.bugfix.rst 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.