3

I'm using Pycharm and Python 3.4 on different servers to execute scripts I've made. I'm using type hinting in my code. Now someone from my work have executed my code on a Python 3.5 installation and is getting a syntax error on my type hinting. I do not "knowingly" use any modules for type hinting in Python 3.4 IE I am not using the typing module.

def run(iargs) -> int:
               ^ syntax error

Could it be that Python 3.4 not even reading these tags where 3.5 does. ?

EDIT

I've done the following test

Python 3.4.3 (default, Oct 28 2016, 10:12:53)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def Add(r,l) -> int:
...   return int(r + l)
...
>>> Add(3,5)
8
>>>

Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def Add(r,l) -> int:
...   return int(r + l)
...
>>> Add(3,5)
8

So there are no problems regarding version of the python 3.4 or 3.5 however when I execute my script I get the following

  File "test.py", line 235
    def run(iargs) -> int:
                   ^
SyntaxError: invalid syntax

Regards

6
  • 1
    Yes, Python 3.4 does not understand a syntax of type hints. Commented Jun 19, 2017 at 8:34
  • So the is why it only "works" in 3.4? I am using it in pycharm. But what is wrong with the syntax and why does it not work in 3.5 because i guess it should? Commented Jun 19, 2017 at 8:54
  • You are not running it in 3.4 when it works. Triple check used interpreters. Commented Jun 19, 2017 at 9:07
  • I am. Im running 3.4.3 i known because ive only installed this version. The problem was one of our developers using my scripts on his own test server. Commented Jun 19, 2017 at 9:30
  • The syntax is supported in Python 3.4, but they are merely function annotations (PEP 3107) not type hints (PEP 484). Commented Dec 6, 2019 at 21:32

1 Answer 1

3

Python 3.4 does not support inline type annotations. The syntax you used was introduced in 3.5 and up

If you want type annotations to work in 3.4 you need to make them a comment like so:

def run(iargs) # type: (str) -> str

On top of that you will need to install typing module from pypi. Also worth noting is that the type hint inside of a comment is not forwards compatible to >=3.5

Sign up to request clarification or add additional context in comments.

4 Comments

I don't see any difference with this line of code compared to the same line of code that the OP says causes a syntax error.
Oh my bad, the comments dissapeared on copy paste.
This isn't true! Function annotations were added in 3.0: python.org/dev/peps/pep-3107
Aside from what falcojr said (which seems to be right - I just tested and no errors on Python 3.4), why do you say it's not compatible with Python >= 3.5? It's only a comment anyways. If it's not understood, it's a normal comment. And I think the type hints (comment or not) don't really do anything on the Python interpreter. I think they're only useful for an IDE (like PyCharm) and/or for someone trying to understand the program. Please correct me if I'm wrong though. I've still to take a complete course on Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.