Skip to content

Feat: Add len builtin function to URLSearchParams class #45

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

Merged
merged 3 commits into from
Nov 2, 2023

Conversation

plusiv
Copy link
Contributor

@plusiv plusiv commented Oct 23, 2023

This change aims to use the built-in function len() for getting the search params passed to the class URLSearchParams, instead of using the size property, len is a more pythonic and idiomatic way to retrieve a count of items.

Example of usage

>>> from ada_url import URLSearchParams
>>> obj = URLSearchParams('key1=value1&key2=value2&key2=value3')
>>> len(obj)
3

@bbayles
Copy link
Collaborator

bbayles commented Oct 23, 2023

I had this in the class initially, but took it out. I think it's fine to have a len, but then should we also have a __getattr__, __setattr__, __delattr__, etc? I decided to leave the interface roughly the same as the WHATWG one and let the Python functions be friendlier.

Or would you use __len__ without expecting attribute setting and getting to work?

@plusiv
Copy link
Contributor Author

plusiv commented Oct 23, 2023

@bbayles thanks for your comment.

I agree with you about keeping compatibility with the mainstream project, however, imho, adding magic methods could help to improve readability for Python developers, and make the API still being friendly. This PR does not remove the old API, but adds a more fancy way to use the Python API.

@plusiv plusiv marked this pull request as draft October 23, 2023 20:00
@plusiv
Copy link
Contributor Author

plusiv commented Oct 23, 2023

@bbayles btw, the purpose of my idea is not to override the classic methods, but provide a Pythonic way to use them.

@bbayles
Copy link
Collaborator

bbayles commented Oct 23, 2023

Right, but do you have an idea of a limiting principle? Would you want the get/set functions as well? I'm trying to decide whether to add those too, and if not, whether length makes sense.

@plusiv
Copy link
Contributor Author

plusiv commented Oct 23, 2023

If you mean adding __setattr__, __hasattr__ and __getattr__ as a support for the methods set(), has() and get(), I think that could be a good option too. Of course, this will expand the scope.

@plusiv plusiv marked this pull request as ready for review October 27, 2023 03:26
@plusiv
Copy link
Contributor Author

plusiv commented Oct 31, 2023

I don't get why is this falling, Do you guys have any clue?

@bbayles
Copy link
Collaborator

bbayles commented Oct 31, 2023

Looks like it was an intermittent failure with one of the wheel builds.

I've still not decided whether to make this a general purpose "multidict" data structure, so I'm holding off on merging.

@bbayles
Copy link
Collaborator

bbayles commented Nov 2, 2023

I looked into how other libraries and treat structures with repeated keys (e.g. request headers and query parameters). There doesn't seem to be a common approach:

  • CPython's urllib.parse.parse_qs returns a dictionary with string keys and lists of string values. You can modify with standard dict and list methods.
  • CPython's urllib.parse.parse_qsl returns a list of key, value pairs. You can modify with standard list methods.
  • requests.structures.CaseInsensitiveDict sidesteps the problem by returning comma separated lists of values (which is legal for headers; doesn't make sense for URL parameters)
  • MultiDict is reasonably popular, but it has an idiosyncratic API that doesn't seem any more or less intuitive than the one described in the WHAT WG spec.

Anyway, I'll merge in this __len__ change on the off chance that somebody expects it to work, but I'll decline to do any further dict-like extensions.

Thanks!

@bbayles bbayles merged commit 853a49b into ada-url:main Nov 2, 2023
@plusiv
Copy link
Contributor Author

plusiv commented Nov 2, 2023

Hi @bbayles ! I apologize for my delay and being late to respond.

I agree with you, however, I think we can create a discussion to address this in a general way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants