Python Forum

Full Version: Type hint produces Syntax error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I use PyCharm and a venv based on Python 3.7
I want to use type hint :

def some_function(some_arg: str) -> bool:

	return "some_str" == some_arg
Code inspection doesn't reveal any suspicious code
But :

Error:
def some_function(some_arg: str) -> bool: ^ SyntaxError: invalid syntax
Does someone have any idea why please ?
Please post full traceback, not just the last line
Sorry, here is the full traceback :

Error:
(venv) [...]>type_hint_test.py File "[...]\type_hint_test.py", line 2 def some_function(some_arg: str) -> bool: ^ SyntaxError: invalid syntax
You are not running 3.7 rather 2.7.
You should check python and pip in virtual environment (venv) that you have created.
# Running 2.7 get this error
E:\1py_div
λ py -2.7 type_hint.py
  File "type_hint.py", line 1
    def some_function(some_arg: str) -> bool:
                              ^
SyntaxError: invalid syntax

E:\1py_div
λ python -V
Python 3.6.4

# Check pip in venv it should not point to OS Python
E:\1py_div
λ pip -V
pip 10.0.1 from c:\python36\lib\site-packages\pip (python 3.6)

# Check placement in venv it should not point to OS Python
E:\1py_div
λ python -c "import sys; print(sys.executable)"
C:\python36\python.exe

# Now running 3.6 there is no error
E:\1py_div
λ python type_hint.py
PyCharm Configuring Python Interpreter.
Thanks !

(venv) [...]>python -c "import sys; print(sys.executable)"
[...]\venv\Scripts\python.exe

(venv) [...]>pip -V
pip 9.0.1 from [...]\venv\lib\site-packages\pip-9.0.1-py3.7.egg (python 3.7)

(venv) [...]>python -V
Python 3.7.0b3

(venv) [...]>venv\Scripts\python.exe type_hint_test.py

(venv) [...]> Thanks !
The venv is really based on python 3.7 but I wasn't using it to start the script.