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

Created on 2017-10-05 20:12 by freitafr, last changed 2017-10-06 07:06 by serhiy.storchaka.

Pull Requests
URL Status Linked Edit
PR 3901 open freitafr, 2017-10-05 20:39
Messages (2)
msg303784 - (view) Author: Fran├žois Freitag (freitafr) * 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).
History
Date User Action Args
2017-10-06 07:06:27serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg303806
versions: + Python 3.7
2017-10-05 20:44:23freitafrsetnosy: + orsenthil
2017-10-05 20:39:00freitafrsetkeywords: + patch
stage: patch review
pull_requests: + pull_request3872
2017-10-05 20:12:52freitafrcreate