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 update_query(None) doesn't clear the query #737

Closed
wants to merge 1 commit into from

Conversation

slymit
Copy link

@slymit slymit commented Jun 11, 2022

What do these changes do?

This fix bug when method url.update_query(None) doesn't clear the query.

Are there changes in behavior for the user?

This changes clear the query if None is passed to method update_query().

Related issue number

#723

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> (e.g. 588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the PR
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: Fix issue with non-ascii contents in doctest text files.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jun 11, 2022
Comment on lines 984 to +990
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)
if args and args[0] is None:
query = new_query
else:
query = MultiDict(self.query)
query.update(new_query)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of re-testing args, check if s == "". _get_str_query() will only ever return "" if None was passed in. We can make use of this to avoid unnecessary round-tripping the query string to MultiDict and back:

Suggested change
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)
if args and args[0] is None:
query = new_query
else:
query = MultiDict(self.query)
query.update(new_query)
s, query = self._get_str_query(*args, **kwargs), None
if s:
new_query = MultiDict(parse_qsl(s, keep_blank_values=True))
query = MultiDict(self.query)
query.update(new_query)

By setting query = None at the start we save on parsing an empty string to an empty MultiDict() and back again; note that the last line uses self._get_str_query(query) again so defaulting query to None means that self._val._replace(query="") is called (where self._val is a urllib.parse.SplitResult() named tuple).

@asvetlov
Copy link
Member

Fixed by #792 I guess

@asvetlov asvetlov closed this Dec 13, 2022
@slymit slymit deleted the update_query_none branch February 17, 2023 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants