Why VS Code IntelliSense for Python suggest autocomplete not for all variables of the same types?

Viewed 1058

Working with the library pandas ran into the fact that the VS Code could not always cope with the display of suggestions. More specifically, when creating a DataFrame object directly from its class, IntelliSense suggests autocompletion without problems. Screenshot. But when creating an object with a method such as read_excel IntelliSense does not show autocompletion suggestions for the object. Screenshot. There is no such problem working in PyCharm. Screenshot. Is it possible to configure VSCode to display autocompletion in this case?

1 Answers

What you are asking about has to do with 'type inference'. An IDE can infer the type of a function result or variable from the workings of the function, or what was previously assigned to the variable. However, since Python is not a statically typed language, this isn't always easy and requires some work on part of the IDE for easy cases and a lot of work for hard ones, which explains the differences you see. Since PyCharm is very mature, dedicated Python IDE, it does a better job of this than VSCode (which has its own, different strengths).

If this was about your own code, a solution would be to include type hints in your code, helping the IDE to determine what type is expected to go in or out of something. But since this is about a third party library, you can either submit a request they add type hints and wait, or try to figure out what it is about the code that's causing VSCode to lose its marbles on this code.

If you find out what the specific issue is, it's not impossible there is a way to improve the behaviour of VSCode here, but it's more likely a change to the code is required. It's also possible that the way the solution was written makes it simply impossible to infer the type with certainty (which is what IDEs deal in). Consider adding a type declaration to the variable holding the result, to get autocompletion (and some warnings and error checks) to work better.

Related