重新格式化后,带有类型注释的 VS Code 中的语法突出显示不适用于 Python [英] Syntax highlighting not working for Python in VS Code with type annotations after reformatting

查看:39
本文介绍了重新格式化后,带有类型注释的 VS Code 中的语法突出显示不适用于 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 Visual Studio Code 使用类型注释(提示)格式化(颜色而不是布局)Python 代码.以下代码没有这样做:

from 输入 import Iterator# 返回 math.factorial(x)def fib(n: int) ->迭代器[int]:a, b = 0, 1而 <ñ:产生一个a, b = b, a + b"""此函数检查字符串是否为回文.s - 要检查的字符串."""def is_palindrome(s: str) ->布尔:返回 s == s[::-1]"""此函数比较两个单词字母字符串并返回匹配百分比.p_string1 - 要比较的第一个字母.p_string2 - 要比较的第二个字母."""def compare_letters(p_string1: str, p_string2: str) ->漂浮:返回 1.0

我正在使用 "python.formatting.provider": "black" 但我也尝试了 autopep8yapf.它们似乎都以同样的方式失败,在类型注释之后将其全部混淆.

当我访问 black 网站并将代码粘贴到 Black Playground 时,它运行良好.

我已经使用 python -m pip install --upgrade black 进行了升级,它显示与 Black Playground 相同的版本 (black-19.10b0),所以不确定这是 Visual Studio Code问题或我的问题.

我在 win32 上使用 WinPython 3.7.1(v3.7.1:260ec2c36a,2018 年 10 月 20 日,14:57:15)[MSC v.1915 64 位 (AMD64)].>

不太确定要针对所有这些 linting、格式(颜色/布局)、Python 解析等记录错误.

有没有人在 Visual Studio Code 中格式化 Python 类型注释以及您使用的设置方面取得成功?

更新:当我使用 code --disable-extensions 运行时不会发生这种情况.有谁知道我如何有选择地禁用扩展程序以找出导致问题的原因?

解决方案

卸载 Python for VSCode 扩展修复了该问题.

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?

解决方案

Uninstalling the Python for VSCode extension fixed the issue.

这篇关于重新格式化后,带有类型注释的 VS Code 中的语法突出显示不适用于 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆