Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 98564

How to get Visual Studio Code to format type annotations

$
0
0

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?


Viewing all articles
Browse latest Browse all 98564

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>