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: urlib.parse.urlencode with empty list and doseq=True drops the parameter
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: maxking, r.david.murray
Priority: normal Keywords:

Created on 2018-05-30 04:57 by maxking, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg318153 - (view) Author: Abhilash Raj (maxking) * (Python committer) Date: 2018-05-30 04:57
When using `urllib.parse.urlencode` it is impossible to pass an empty list when doseq=True.

So, I see the following behavior when using urlencode:

In [3]: urlencode({'key': ['value']}, doseq=True)
Out[3]: 'key=value'

In [4]: urlencode({'key': []}, doseq=True)
Out[4]: ''

So, in our source code, we do urlecode(params, doseq=True), which can have keys with empty list as values and it gets dropped completely. To get it to work properly, I instead pass around an empty string as a value when the length of list is 0, which conveys the server that the value of the key is empty.

I wonder if it would make sense for urlencode to do that by default given doseq=True is meant to encode lists properly.
msg318156 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-05-30 06:27
If a sequence as a value means repeated instances of a key with each value from the list, then logically an empty list means no instances of the key, as documented.  Blank values aren't really part of the standard (such as it is): the absence of a parameter is supposed to be equivalent to the value being empty.  Because of this, you have to pass keep_blank_values=True to parse_qs to retain keys with blank values.  I think it is reasonable that you have to take extra action if you want an empty list of values to instead result in a single key with a blank value.

So, this is working as designed and desired, I think.
msg318219 - (view) Author: Abhilash Raj (maxking) * (Python committer) Date: 2018-05-30 23:08
Would it then make sense to add a similar flag, keep_blank_values, in urlencode to achieve similar behavior as parse_qas?
msg318220 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-05-30 23:13
It would be odd to do that, since blank values are kept by default.
msg318224 - (view) Author: Abhilash Raj (maxking) * (Python committer) Date: 2018-05-30 23:26
Ah! But blank lists aren't.

That makes sense! Thank you!
History
Date User Action Args
2022-04-11 14:59:01adminsetgithub: 77871
2018-05-31 04:04:01r.david.murraysetresolution: not a bug
2018-05-30 23:26:43maxkingsetstatus: open -> closed
stage: resolved
2018-05-30 23:26:30maxkingsetmessages: + msg318224
2018-05-30 23:13:25r.david.murraysetmessages: + msg318220
2018-05-30 23:08:39maxkingsetmessages: + msg318219
2018-05-30 06:27:45r.david.murraysetnosy: + r.david.murray
messages: + msg318156
2018-05-30 04:57:25maxkingcreate