osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

type hinting backward compatibility with python 3.0 to 3.4


On Fri, 19 May 2017 11:35 pm, Edward Ned Harvey (python) wrote:

> I think it's great that for built-in types such as int and str, backward
> compatibility of type hinting annotations is baked into python 3.0 to 3.4.
> In fact, I *thought* python 3.0 to 3.4 would *ignore* annotations, but it
> doesn't...

Why would you think that? Was there something in the documentation or PEP
for annotations that lead you to believe that they were ignored by the
interpreter? If so, that documentation needs improvement.

 
> I'm struggling to create something backward compatible that requires the
> 'typing' module. For example, the following program is good in python 3.5,
> but line 11 is a syntax error in python 3.4:

You say "syntax error". But it isn't a syntax error, the actual error is:


> When I run this program in python 3.4, I get this:
>     Traceback (most recent call last):
>       File "./ned.py", line 11, in <module>
>         def divider(x: int, y: int) -> Optional[float]:
>     NameError: name 'Optional' is not defined

NameError.

You solve this NameError the same way you solve any other NameError: by
making sure that the name "Optional" is defined.

You should actually read the error message you get, and pay attention to it.


(By the way, nobody should be using Python 3.0, it is so heavily bug-ridden
that it is not supported. Most Python 3 devs consider Python 3.3 the first
one worth supporting.)



-- 
Steve
Emoji: a small, fuzzy, indistinct picture used to replace a clear and
perfectly comprehensible word.