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

Andrey Vlasovskikh andrey.vlasovskikh at gmail.com
Wed Mar 23 03:45:51 EDT 2016


> 2016-03-23, в 7:37, Chris Angelico <rosuav at gmail.com> написал(а):
> 
> 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.

Right. I'm not sure that a non-ASCII attribute name is fine in Python 2 though.

-- 
Andrey Vlasovskikh

Web: http://pirx.ru/



More information about the Python-ideas mailing list