[Python-ideas] Proposal to extend PEP 484 (gradual typing) to support Python 2.7

Brett Cannon brett at python.org
Fri Jan 22 13:37:48 EST 2016


On Thu, 21 Jan 2016 at 10:45 Guido van Rossum <guido at python.org> wrote:

> On Thu, Jan 21, 2016 at 10:14 AM, Agustín Herranz Cecilia <
> agustin.herranz at gmail.com> wrote:
>
>> El 2016/01/21 a las 1:11, Guido van Rossum escribió:
>>
> [...]
>>
> [SNIP]
>>
>> > - Also there must be addressed how it work on a python2 to python3
>>> environment as there are types with the same name, str for example, that
>>> works differently on each python version. If the code is for only one
>>> version uses the type names of that version.
>>>
>>> That's the same problem that exists at runtime, and people (and tools)
>>> already know how to deal with it: use bytes when you mean bytes, unicode
>>> when you mean unicode, and str when you mean whatever is "native" to the
>>> version you're running under and are willing to deal with it. So now you
>>> just have to do the same thing in type hints that you're already doing in
>>> constructors, isinstance checks, etc.
>>>
>>
>> This is actually still a real problem. But it has no bearing on the
>> choice of syntax for annotations in Python 2 or straddling code.
>>
>>
>> Yes, this is no related with the choice of syntax for annotations
>> directly. This is intended to help in the process of porting python2 code
>> to python3, and it's outside of the PEP scope but related to the original
>> problem. What I have in mind is some type aliases so you could annotate a
>> version specific type to avoid ambiguousness on code that it's used on
>> different versions. At the end what I originally try to said is that it's
>> good to have a convention way to name this type aliases.
>>
>
> Yes, this is a useful thing to discuss.
>
> Maybe we can standardize on the types defined by the 'six' package, which
> is commonly used for 2-3 straddling code:
>
> six.text_type (unicode in PY2, str in PY3)
> six.binary_type (str in PY2, bytes in PY3)
>
> Actually for the latter we might as well use bytes.
>

I agree that `bytes` should cover str/bytes in Python 2 and `bytes` in
Python 3.

As for the textual type, I say either `text` or `unicode` since they are
both unambiguous between Python 2 and 3 and get the point across.

And does `str` represent the type for the specific version of Python mypy
is running under, or is it pegged to a specific representation across
Python 2 and 3? If it's the former then fine, else those people who use the
"native string" concept might want a way to say "I want the `str` type as
defined on the version of Python I'm running under" (personally I don't
promote the "native string" concept, but I know it has been brought up in
the past).

-Brett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160122/1c19f574/attachment.html>


More information about the Python-ideas mailing list