1

I am using Python 3.8.5 and mypy 0.790.

Running mypy on this code block will raise an error:

# mypy_and_the_walrus.py 

z = "asd"

if (p := z):
    print('hello')  # error: invalid syntax

Running the program is fine:

❯ python -i mypy_and_the_walrus.py         
hello
>>> p
'asd'

I have not been able to figure out what the issue is.

Note:

I have a mypy.ini with the python version set to 3.8:

# mypy.ini

[mypy]

python_version = 3.8

This is probably something silly on my end, but I have not been able to find a solution to this.

The only solutions I have found are:

The second one seems like it would have a solution, but the solution is tha

Algebra8
  • 1,115
  • 1
  • 10
  • 23

1 Answers1

1

The answer is in the second link:

Walrus operator fails with 'Syntax Error' #64

However, the answer was not very clear:

It seems we need to run mypy itself on Python 3.8 to support syntax introduced in Python 3.8. Currently, we're using Python 3.7 to run mypy in the sandbox. I'll update the Docker image to fix this.

The solution:

❯ python3.8 -m mypy prac.py 
Success: no issues found in 1 source file
Algebra8
  • 1,115
  • 1
  • 10
  • 23
  • I am using vscode and I make mypy run in the background, I am using python 3.8 and it still reports the Walrus operator as invalid syntax. – vianmixt Dec 19 '21 at 11:40