Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax highlighting not working for Python in VS Code with type annotations after reformatting

I'm trying to get Visual Studio Code to format (the colours not the layout) Python code with type annotations (hinting). It's failing to do so for the following code:

from typing import Iterator

# return math.factorial(x)
def fib(n: int) -> Iterator[int]:
    a, b = 0, 1
    while a < n:
        yield a
        a, b = b, a + b        

"""
This function checks whether a string is a palindrome.
s - The string to check.
"""
def is_palindrome(s: str) -> bool:
    return s == s[::-1]

"""
This function compares two strings of word letters and returns the percentage match.

p_string1 - The first letters to compare.
p_string2 - The second letters to compare.
"""
def compare_letters(p_string1: str, p_string2: str) -> float:
    return 1.0

I'm using "python.formatting.provider": "black" but I also tried autopep8 and yapf. They all seem to fail in the same way, by getting it all mixed up after the type annotations.

When I go to the black website and paste the code into the Black Playground it works fine though.

I have upgraded using python -m pip install --upgrade black and it is showing the same version (black-19.10b0) as the Black Playground, so not sure where this is Visual Studio Code issue or a me issue.

I am using WinPython 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32.

Not really sure what to log a bug against with all this linting, formatting (colour/layout), Python parsing, etc.

Has anyone had any success with formatting Python type annotations in Visual Studio Code and what settings are you using?

UPDATE: This does not happen when I run with code --disable-extensions. Does anyone know how I can disable extensions selectively in order to find out which one is causing the issue?

like image 733
Superdooperhero Avatar asked Dec 15 '19 08:12

Superdooperhero


People also ask

How do I enable syntax highlighting in Python?

You can change the syntax highlighting theme by clicking Options > Configure IDLE and switching to the Highlights tab.

How do I change syntax highlighting in VSCode?

To change the text and syntax colors in visual studio code follow the steps given below: Open VS Code editor to change the syntax colors. Go to Settings, which is on the bottom left corner of the VS Code window. In the search field type JSON, and click on the 'Edit in settings.

What does VSCode use for syntax highlighting?

VS Code uses TextMate grammars as the syntax tokenization engine. Invented for the TextMate editor, they have been adopted by many other editors and IDEs due to large number of language bundles created and maintained by the Open Source community.

How do you restructure code in VS code?

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.

Why is syntax highlighting not working for my Python file?

Sign in to your account Syntax highlighting no longer works for python files. This has happened before and was resolved after disabling the python extension. However, this time disabling the extension did not bring back highlighting. This could a conflict with python versions, as I just upgraded to python 3.7.

What is syntax highlight?

Syntax Highlight Guide Syntax highlighting determines the color and style of source code displayed in the Visual Studio Code editor. It is responsible for colorizing keywords like if or for in JavaScript differently than strings and comments and variable names. There are two components to syntax highlighting:

What is semantic highlighting in Visual Studio Code?

Semantic highlighting is an addition to syntax highlighting as described in the Syntax Highlight Guide. Visual Studio Code uses TextMate grammars as the main tokenization engine. TextMate grammars work on a single file as input and break it up based on lexical rules expressed in regular expressions.

How to highlight syntax in typescript?

There are two components to syntax highlighting: Before diving into the details, a good start is to play with the scope inspector tool and explore what tokens are present in a source file and what theme rules they match to. To see both semantic and syntax token, use a built-in theme (for example, Dark+) on a TypeScript file.


1 Answers

Uninstalling the Python for VSCode extension fixed the issue.

like image 144
Superdooperhero Avatar answered Oct 27 '22 11:10

Superdooperhero