python-hyper / hyperlink

🔗 Immutable, Pythonic, correct URLs.
https://hyperlink.readthedocs.io/
Other
283 stars 41 forks source link

URL.add supporting a list of values #182

Open ddormer opened 1 year ago

ddormer commented 1 year ago

I occasionally find myself making multiple URL.add method calls in a loop to add a list of values to the same query parameter name. If I'm not alone, perhaps it would be nice to add support for passing a list of values to URL.add?

For example:

>>> # This currently returns a list of values.
>>> values = URL.from_text('?foo=1&foo=2').get('foo')
>>> values
['1', '2']
>>> # This is currently a TypeError
>>> URL.add('foo', values)
URL.from_text('?foo=1&foo=2')

While looping over a single list and making multiple add calls isn't much effort, when working with a dictionary of parameters that I'd like to build a URL out of, and the dictionary may or may not have a list of values for some of the keys, I find the code needed to check each dictionary value and loop over the lists a bit messy.

Edit: I'm willing to put work into a PR for the feature, if the feature is considered a good addition.