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
  World Clock syntax error OscarBoots 0 1 25 minutes ago
Last Post: OscarBoots
  Syntax error for "root = Tk()" dlwaddel 15 1,243 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 411 Jan-19-2024, 01:20 PM
Last Post: rob101
  Wrong type error rowan_bradley 6 1,286 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 1,245 Jul-22-2023, 11:33 PM
Last Post: jhancock
  Syntax error while executing the Python code in Linux DivAsh 8 1,663 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,256 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,347 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,308 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 921 Dec-05-2022, 06:38 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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