You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/tmp/pygrate2/Lib/codecs.py:506: Py3xWarning: The first string is 'str' while the second is 'unicode': mixed bytes, str and unicode operands cannot be used in string concatenation in Python 3.x: convert the operand(s) so that they are the same type.
self.charbuffer += newchars
Since Python3 doesn't have the unicode type per se, I'm wondering if this warning is quite right? As I understand the situation (and my memory is a bit fuzzy...):
Python2 has the types str and unicode but not a type bytes. The bytes() function returns strs.
Python3 has the types bytes and str but not unicode.
If I've got that (roughly) correct, I wonder whether the warning at the top of this issue is correct and/or as accurate as it could be? I think that str + unicode in Python2 will always map to str + str in Python3? Or are there certain types of strings/unicode strings in Python2 that will map to bytes in Python3?
Yes, you are correct, the u prefix just exists for backward compatibility, i.e, no-op per se, its not supported in semantics to say unicode = str, that is to say no Python 3 code uses the u prefix or is expected to. Any code still using the u prefix is most likely assuming the genuine existence of the unicode type as was the case in Python 2. I think in short supporting u in python 3 is not a feature, just exists for convenience. If we look at it as a supported solely for compatibility, the warning is correct, if we assume it is a feature the warning needs fixing.
I'm not sure this is entirely about u"blah" strings, but I might be wrong. The warning we're giving is really saying the + operator won't work as expected in Python3. In one sense, I suppose that's true, but I think it's not very useful, because Python3 won't have let a user construct a unicode string by definition.
Now that I think about it, I'm not even sure if the user can do much about this warning? Can they convert their string type(s) in a way that will make their code forward compatible?
By making the strings involved in the + operation of the same type. str + str or bytes + bytes , if we took u to be a feature u'str' + 'str' shouldnt lead to a warning.
Activity
nanjekyejoannah commentedon May 22, 2023
Yes, you are correct, the
uprefix just exists for backward compatibility, i.e, no-op per se, its not supported in semantics to say unicode = str, that is to say no Python 3 code uses theuprefix or is expected to. Any code still using theuprefix is most likely assuming the genuine existence of the unicode type as was the case in Python 2. I think in short supportinguin python 3 is not a feature, just exists for convenience. If we look at it as a supported solely for compatibility, the warning is correct, if we assume it is a feature the warning needs fixing.ltratt commentedon May 22, 2023
I'm not sure this is entirely about
u"blah"strings, but I might be wrong. The warning we're giving is really saying the+operator won't work as expected in Python3. In one sense, I suppose that's true, but I think it's not very useful, because Python3 won't have let a user construct aunicodestring by definition.Now that I think about it, I'm not even sure if the user can do much about this warning? Can they convert their string type(s) in a way that will make their code forward compatible?
nanjekyejoannah commentedon May 22, 2023
By making the strings involved in the
+operation of the same type. str + str or bytes + bytes , if we tookuto be a feature u'str' + 'str' shouldnt lead to a warning.