Python Forum
Type hint produces Syntax error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Type hint produces Syntax error
#1
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 ?
Reply
#2
Please post full traceback, not just the last line
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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.
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to use pyinstaller results in a syntax error mark1969 2 58 Apr-15-2026, 01:59 PM
Last Post: snippsat
Question how to type hint a function in a dataclass? Calab 3 1,930 Feb-27-2025, 04:40 AM
Last Post: Calab
  POST Syntax error amplay 0 1,387 Aug-07-2024, 02:43 PM
Last Post: amplay
  How can I create this type hint. deanhystad 0 1,013 Aug-05-2024, 07:55 PM
Last Post: deanhystad
  is this really a syntax error? Skaperen 4 2,552 May-25-2024, 07:31 AM
Last Post: snippsat
  World Clock syntax error OscarBoots 1 1,759 May-03-2024, 05:20 AM
Last Post: snippsat
  Syntax error for "root = Tk()" dlwaddel 15 10,783 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 2,071 Jan-19-2024, 01:20 PM
Last Post: rob101
  Wrong type error rowan_bradley 6 3,868 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 8,050 Jul-22-2023, 11:33 PM
Last Post: jhancock

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020