Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing Backwards Compatability with Python 3.6 Variable Annotations

I am trying to create a python package (not anywhere at the moment) and I want to use 3.6 python variable annotations, that is

foo: int = 5

while still providing support for Python 3.5.

Is there any way to provide these style of variable annotations inside Python 3.5, either through a

from __future__ import variable_annotations

or similar. I know it is possible to use comment type annotations, but I would like to be able to use this style.

like image 391
IllustriousMagenta Avatar asked Jun 04 '18 07:06

IllustriousMagenta


1 Answers

TLDR: No, you can not use variable annotations in Python 3.5 and earlier.

First, Python 3.6.0 changelog reports variable annotations to be a new feature comparing to 3.5.

Second, PEP 526 is defined as backward compatible. According to PEP 387:

Unless it is going through the deprecation process below, the behavior of an API must not change between any two consecutive releases.

And this includes:

Syntax and behavior of these constructs as defined by the reference manual

So PEP 526's "fully backwards compatible" just means that Python 3.5 (or to be scrupulous, pre-PEP526) syntax will be working in 3.6.0 without changes: variable annotations are not mandatory.

At last, you mentioned "comment type annotations". So I will just give a link to PEP 484, which is available for Python 3.5 (it might be useful for some readers of this question).

like image 194
Eugene Primako Avatar answered Oct 27 '22 16:10

Eugene Primako