Python variable annotation and syntax error on version < 3.6

57 Views Asked by At

I have next line

status_dict : dict =  result.get('status', {})

I'm using the variable annotation, to make my codding easier after that (autocompletion). It works perfect on python >= 3.6, but on older versions I get

File "/home/marcel/work/py/svn_utils.py", line 261
    status_dict : dict =  svn_result.get('status', {})
                ^
SyntaxError: invalid syntax

Any idea how can I make the code to throw a meaningful error message, e.g. "Python 3.6 or newer is required".

What I've try until now: at the beginning of the script I've added next lines

import sys
MIN_PYTHON = (2, 6)
if sys.version_info < MIN_PYTHON:
    sys.exit("Python %s.%s or newer is required.\n" % MIN_PYTHON)

but it doesn't work because first is the parser complaining by the syntax.

The only solution that I see a wrapper script to check the minimum version and decide if the main script will be executed or not. But maybe there is some other solution, to avoid the wrapper.

I've got some hints/questions if Is there a strategy for making Python 3.5 code with type-annotations backwards compatible? is not the answer to my question.

The answer is "No", reason: it is refereeing to type annotation for functions (parameters and return value). I'm looking for the syntax for variable type annotation is newer peps.python.org/pep-0526 is refereeing to type annotation for functions (parameters and return value). The syntax for variable type annotation is newer peps.python.org/pep-0526

0

There are 0 best solutions below