Issue 33690: urlib.parse.urlencode with empty list and doseq=True drops the parameter (original) (raw)

Issue33690

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:01 admin set github: 77871
2018-05-31 04:04:01 r.david.murray set resolution: not a bug
2018-05-30 23:26:43 maxking set status: open -> closedstage: resolved
2018-05-30 23:26:30 maxking set messages: +
2018-05-30 23:13:25 r.david.murray set messages: +
2018-05-30 23:08:39 maxking set messages: +
2018-05-30 06:27:45 r.david.murray set nosy: + r.david.murraymessages: +
2018-05-30 04:57:25 maxking create