Home > Software engineering >  Python type hinting for constructors in VSCode
Python type hinting for constructors in VSCode

Time:06-07

In VS Code, whenever I write a constructor for a Python class it appends the type hint "-> None" to it when autocompleting the method.

class MyClass:
    def __init__(self) -> None:
        pass

Is there a setting that configures that behaviour or is there a way to disable it? I would like it to look like the following for constructors:

class MyClass:
    def __init__(self):
        pass

CodePudding user response:

I don't think there is any way to just change the lsp's builtin snippets but VS Code makes it very easy to create your own snippets and you can create your own snippet for a constructor without an explicit return type hint if you want

This page shows hot to do so https://code.visualstudio.com/docs/editor/userdefinedsnippets

Example snippet for this case

{
  "init": {
    "prefix": ["init", "__init__"],
    "body": [
      "def __init__(self):", 
      "\t$0"
    ],
    "description": "An empty Constructor with no type hints"
  }
}
  • Related