classification
Title: Misleading error message in urllib.parse.unquote
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: urllib.parse.unquote raises incorrect errormessage when string parameter is bytes
View: 32498
Assigned To: Nosy List: r.david.murray, thet, xtreak
Priority: normal Keywords:

Created on 2018-06-12 18:02 by thet, last changed 2018-06-17 13:03 by r.david.murray. This issue is now closed.

Messages (4)
msg319396 - (view) Author: Johannes Raggam (thet) Date: 2018-06-12 18:02
urllib.parse.unquote gives an misleading error message when:

>>> import urllib
>>> urllib.parse.unquote(b'bytesurl')
*** TypeError: a bytes-like object is required, not 'str'

while:

>>> urllib.parse.unquote('texturl')
texturl

The correct behavior is to pass a string/text object to unquote. But passing a bytes object gives a misleading error message.

A fix would be to add an assertion in
https://github.com/python/cpython/blob/0250de48199552cdaed5a4fe44b3f9cdb5325363/Lib/urllib/parse.py#L614 like:

>>> assert isinstance(string, str)
msg319397 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-06-12 18:11
We don't generally do type checks like that.  This is a specific case of a general issue with binary operators and mismatched string/bytes types.  I thought there was an open issue for it, but I haven't found it yet.
msg319805 - (view) Author: Karthikeyan Singaravelan (xtreak) * Date: 2018-06-17 06:03
@r.david.murray I googled the error message and I hope you are referring to the below issue which has a patch and test.

issue : https://bugs.python.org/issue32498
msg319813 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-06-17 13:03
I thought there was a more general issue for the 'in' operator itself, but perhaps the issue you found was indeed the one I was thinking of.  This one certainly seems to be a duplicate of that one ;)  If you've a mind to, you could turn the patch in that issue into a PR to move the issue forward.
History
Date User Action Args
2018-06-17 13:03:59r.david.murraysetstatus: open -> closed
superseder: urllib.parse.unquote raises incorrect errormessage when string parameter is bytes
messages: + msg319813

resolution: duplicate
stage: resolved
2018-06-17 06:03:06xtreaksetnosy: + xtreak
messages: + msg319805
2018-06-12 18:11:48r.david.murraysetnosy: + r.david.murray
messages: + msg319397
2018-06-12 18:02:33thetcreate