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

Guido van Rossum guido at python.org
Fri Jan 22 14:08:14 EST 2016


On Fri, Jan 22, 2016 at 10:37 AM, Brett Cannon <brett at python.org> wrote:

>
>
> 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:
>> [...]
>> 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.
>

OK, that's settled.


> 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.
>

Then let's call it unicode. I suppose we can add this to typing.py. In PY2,
typing.unicode is just the built-in unicode. In PY3, it's the built-in str.


>
> 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).
>

In mypy (and in typeshed and in typing.py), 'str' refers to the type named
str in the Python version for which you are checking -- i.e. by default
mypy checks in PY3 mode and str will be the unicode type; but "mypy --py2"
checks in PY2 mode and str will be the Python 2 8-bit string type. (This is
actually the only thing that makes sense IMO.)

There's one more thing that I wonder might be useful. In PY2 we have
basestring as the supertype of str and unicode. As far as mypy is concerned
it's almost the same as Union[str, unicode]. Maybe we could add this to
typing.py as well so it's also available in PY3, in that case as a
shorthand for Union[str, unicode].

FWIW We are having a long discussion about this topic in the mypy tracker:
https://github.com/JukkaL/mypy/issues/1135 -- interested parties are
invited to participate there!

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160122/7bc75ef9/attachment.html>


More information about the Python-ideas mailing list