Home > Net >  Python type annotations breaking syntax highlighting in vscode
Python type annotations breaking syntax highlighting in vscode

Time:01-18

When I try to add type annotations (for return types) to my functions then the syntax highlighting in vscode is not working after that point. Do you have a hint on what setting to change to fix the issues?

VSCode Version 1.67.1
Python Version 3.10.8

Code with type annotations

def median_absolute_deviaton(data: list) -> float:
    """Returns the median absolute deivation (MAD) [float]."""
    med = median(data)
    deviations = [abs(i-med) for i in data]
    return statistics.median(deviations)

Image with working syntax highlighting

Working syntax highlighting

Image with broken syntax highlighting

Broken syntax highlighting

CodePudding user response:

You may need to install and/or select the right Python interpreter:

VSCode Interpreter Selection

There's a python version shown at the lower right corner of the VSCode window. Click it, then select a version of python at least as high as python 3.5 (the version of python where type annotations were added).

If you don't have a version of python that high listed, then you'll need to install one.

If you have one installed and it isn't listed, then you can manually enter the path to the version you installed.

CodePudding user response:

SOLVED: It was caused by the "Python for VSCode" extension. Uninstalling helped. Found it here: https://github.com/MagicStack/MagicPython/issues/138#issuecomment-403538358

  • Related