0

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

rioV8
  • 24,506
  • 3
  • 32
  • 49
Camilo
  • 35
  • 4
  • This is weird behavior, happens for me as well, but is probably as designed. If you think this should be changed you could submit a but report for PyLance (Help -> Show all Commands -> Type "Pylance: Report issue...") – M Virts May 11 '23 at 23:58
  • Looks like this only happens for attributes of `self`, maybe because PyLance is expecting them all to be declared in `__init__`? – M Virts May 11 '23 at 23:59
  • 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. – SimonUnderwood May 12 '23 at 01:08
  • Why do you think it has the methods belong to `ndarray` when you define it as **None** in the same method? – MingJie-MSFT May 12 '23 at 01:29

1 Answers1

2

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

SimonUnderwood
  • 469
  • 3
  • 12