use a dictionary for query params
wbolster opened this issue · 6 comments
hyperlink does not support dictionaries for query. while i know that technically this is an orderedmultidict, this is just not nice from an api perspective:
>>> url = URL.from_text('https://example.org/api/v2')
>>> url.replace(query={'foo': 'bar'})
[...]
ValueError: too many values to unpack (expected 2)instead, this works:
>>> url.replace(query=[('foo', 'bar')])
URL.from_text('https://example.org/api/v2?foo=bar')or using .items():
>>> url.replace(query={'foo': 'bar'}.items())
URL.from_text('https://example.org/api/v2?foo=bar')but tbh none of this looks appealing to me. :)
any reason for the current behaviour?
It could probably be made to work for .replace(), by calling .items(), as you've done at the end there). We can't have the query attribute itself be a dictionary, not just because of the backwards compat with Twisted, but because of the immutable nature of the URL itself. FrozenOrderedMultiDicts are rare, and I think might complicate things beyond the API benefits.
accepting a plain dict would simplify the api for common cases.
should i cook up a pr?
Sure, allowing .replace()'s query parameter to work an object that has an .items() method on it, should the argument not be an iterable of tuples, seems like a fine change to me. Would be nice if the PR took that approach, that way OrderedDict and others would work fine, too. By all means! :)
ok, thanks! pls assign this issue to me, i'll have a look when i find some time.
You got it! Thanks!