VS Code: add return type annotations automatically in Python function definitions

Viewed 493

What I have:

def get_candy():
    return "candy"

What I want VS Code to automatically do:

def get_candy() -> str:
    return "candy"

When I later call get_candy() the annotation box automatically includes -> str: anyhow, but it would be nice if it was included in the source code as well.

The annotation extensions I checked do not do this either, the just add the """ comments - ...""" stuff.

1 Answers

When you see the return type of functions that don't explicitly show a return type, that is most likely pylance. While pylance does guess at return types, (1) it's not perfect, especially for complicated functions, and (2) I have never seen (nor can I find) anything about making it automatically add return types.

Assuming you are using pylance, you can always make a feature request on pylance's issue repo.

Related