如何让Visual Studio Code为代码补全/Intellisense提供类属性提示?

7
如果我在启用 ms-python.python扩展的Visual Studio Code中编辑Python文件,则会获得成员变量和方法的建议,但不包括类属性。
在下图1中,您可以看到它确实为该类提出了一些建议,但并未包含我所希望的类属性。
有没有什么方法来解决这个问题(调整设置、安装其他插件等)?
以以下代码为例,看看注释指出的自动完成的工作/不工作的地方。
class MyClass:
    """
    A class with a single class attribute ``value`` and an instance attribute ``member``.
    """

    value: int = 3

    def __init__(self):
        self.member = 5

    def method(self, i: int) -> int:
        """return ``i`` + ``value`` + ``self.member``"""
        return i + MyClass.value + self.member


instance = MyClass()

print(instance.method(5))   # autocompletion for method works
print(instance.member)      # autocompletion for member works
print(MyClass.value)        # autocompletion for value DOES NOT work
print(instance.value)       # autocompletion for value works

我正在运行VSCode 1.43.2,Python扩展版本为2020.3.71659,在Python 3.8上的Arch Linux系统中。

Intellisense does make some proposals for class attributes, but not for _my_ attribute.

编辑:有一个旧问题与此类似,但没有解决我的问题。

1个回答

1

当你创建类的实例时,你所提到的内容是有效的。

在你的情况下,"instance" 是 "MyClass" 类的一个对象。因此调用 "instance." 应该会给你关于类属性的建议。


谢谢回复,Brian。这就是我想通过底部的打印语句描述的内容——所有实例都可以工作,但这不是我在问题中想要关注的重点。 - Michael Kopp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接