pythonvisual-studio-codevscode-python

VSCode Python Extension: Autocomplete doesn't detect the type of an hinted attribute when it's used in the same method


When an attribute is created and hinted, and then used in the same method, the autocomplete doesn't work (only shows python object stuff):

enter image description here

But if I try to use the attribute inside another method, the autocomplete works and show all the specific class stuff:

enter image description here


Solution

  • While inside the same method, Pylance can actually see that the variable is None because you set it in the last line so it ignores the type hint. Inside a different method, Pylance can no longer count on the variable being None so it goes with the type hint and treats it as a ndarray.

    However, if you define it as a proper ndarray in the function, Pylance will autofill correctly as you can see here in this screenshot