This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: urlencode should accept generator as values for mappings when doseq=True
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: francois.freitag, orsenthil, serhiy.storchaka, taleinat
Priority: normal Keywords: patch

Created on 2017-10-05 20:12 by francois.freitag, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 3901 open francois.freitag, 2017-10-05 20:39
Messages (4)
msg303784 - (view) Author: François Freitag (francois.freitag) * Date: 2017-10-05 20:12
The urlencode documentation states that:
The value element in itself can be a sequence and in that case, if the optional parameter doseq is evaluates to True, individual key=value pairs separated by '&' are generated for each element of the value sequence for the key.

Passing a generator as the value gives unexpected results:
>>> from urllib.parse import urlencode
>>> def gen():
...     yield from range(2)
>>> urlencode({'a': gen()}, doseq=True)
'a=%3Cgenerator+object+gen+at+0x7f35ff78db48%3E'

A list gives:
>>> urlencode({'a': [0, 1]}, doseq=True)
'a=0&a=1'
msg303806 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-06 07:06
Extending urlencode() to accept iterables instead of just sequences of values makes sense to me. There is no principal reason of requiring sequences. But you need to update the documentation, docstrings and comments.

While we are here, we can generalize urlencode() in other aspect. Currently it accepts either mapping or a sequence of 2-tuples. It could be accept an iterable of 2-tuples (or even an iterable of 2-element sequences or 2-element iterables).
msg306908 - (view) Author: François Freitag (francois.freitag) * Date: 2017-11-24 17:20
I updated the PR to allow any iterable of two-elements iterables to be passed to urlencode.

If anyone has the time and interest, the PR is ready for review.
msg350449 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2019-08-25 11:04
IMO the benefit here is very small and not worth the added complexity (implementation + tests + docs + maintenance).
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 75887
2019-08-25 11:04:53taleinatsetnosy: + taleinat
messages: + msg350449
2018-05-03 00:03:25francois.freitagsetversions: + Python 3.8, - Python 3.7
2017-11-24 17:20:46francois.freitagsetmessages: + msg306908
2017-10-06 07:06:27serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg303806
versions: + Python 3.7
2017-10-05 20:44:23francois.freitagsetnosy: + orsenthil
2017-10-05 20:39:00francois.freitagsetkeywords: + patch
stage: patch review
pull_requests: + pull_request3872
2017-10-05 20:12:52francois.freitagcreate