[Python-ideas] Type hints for text/binary data in Python 2+3 code

Chris Angelico rosuav at gmail.com
Wed Mar 23 00:37:55 EDT 2016


On Wed, Mar 23, 2016 at 2:39 PM, Guido van Rossum <guido at python.org> wrote:
>> I was concerned with UnicodeEncodeErrors in Python 2 during implicit conversions from unicode to bytes:
>>
>>     getattr(obj, u'Non-ASCII-name')
>>
>> There are several places in the Python 2 API where these ASCII-based unicode->bytes conversions take place, so the _AsciiUnicode type comes to mind.
>
> OK, so you want the type of u'hello' to be _AsciiUnicode but the type
> of u'Здравствуйте' to be just unicode, right? And getattr()'s second
> argument would be typed as... What?

AIUI, getattr's second argument is simply 'str'; but in Python 2,
_AsciiUnicode (presumably itself a subclass of unicode) can be
implicitly promoted to str. A non-ASCII attribute name works fine, but
getattr converts unicode to str using the 'ascii' codec.

ChrisA


More information about the Python-ideas mailing list