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

Created on 2017-10-05 20:12 by francois.freitag, last changed 2018-05-03 00:03 by francois.freitag.

Pull Requests
URL Status Linked Edit
PR 3901 open francois.freitag, 2017-10-05 20:39
Messages (3)
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 list gives:
>>> urlencode({'a': [0, 1]}, doseq=True)
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.
Date User Action Args
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