How to get visual studio code to propose Class Attributes for code completion/Intellisense?
Asked Answered
U

1

8

If I edit a python file in Visual Studio Code with the ms-python.python extension enabled, I get proposals for member variables and methods, but not for class attributes on the class. In the Image 1 below you can see that it does propose something for the class -- but not the class attribute I was hoping for.

Is there some way to fix this (fix a setting, install another plugin, ...)?

As an example, consider the following code and see the comments that indicate where autocompletion works/does not work.

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

I'm running VSCode 1.43.2 with the Python extension in version 2020.3.71659 on Arch Linux with Python 3.8.

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

Edit: There is an old question that goes into a similar direction, but that did not solve my problem.

Ulterior answered 15/4, 2020 at 14:55 Comment(0)
K
1

What you are referring to works when you create an instance of your class.

In your case, instance is an object of the class MyClass. So calling instance. should give you the suggestions on class attributes.

Karykaryl answered 21/1, 2023 at 10:27 Comment(1)
thanks for the reply, Brian. This is what I wanted to describe with the print statements at the bottom -- for the instance all of them work, but that is not what I wanted to focus on in the question.Ulterior

© 2022 - 2024 — McMap. All rights reserved.