VSCode Extension of Type4Py
This extension provides machine learning-based type autocompletion for Python, which assists developers to gradually add type annotations to their existing codebases.
- Machine learning-based type prediction (powered by Type4Py)
- It can be used with a local model on your machine (No data sharing)
- Improved autocompletion for Python type annotations (based on Python Type Hint)
- Type autocompletion for parameters, return types of functions, and variables.
- Simple, fairly fast and easy to use.
- In VSCode, select a Python source file of your interest.
- Go to
View -> Command Palette -> Type4Py: Infer type annotations.
- Depending on the file size, after a couple of seconds, you can see the in-progress/completed requests at the bottom left of VSCode.
- After the completion of your request, you can now start adding predicted type annotations to your code, similar to the Quick Start example above.
Tip: At the bottom left of VSCode, click on the status bar of Type4py to see the extension's logs and errors.
Tip: To use the local model for type inference instead of our server, see settings.
Tip: You can also enable automatic type inference when opening Python files. To do so, see settings.
Tip: The same above steps are also applicable to the web version of the extension.
- Do not edit the selected source file after running the Type4Py extension. That is, add the relevant predicted types to the code, save the file and then edit as you wish. We will drop this consideration once the caching solution is implemented.
- Make sure that the selected source file does not have syntax or parse errors before running the extension. In VSCode, Python linters warn about such errors.
- Concerning variables' redeclaration, type predictions are available for the last redeclaration of variables.
NOTE: Update VSCode before installing the extension.
The latest version of the extension can be installed from the Visual Studio Marketplace. The extension is also compatible with the web version of VSCode.
||If enabled, it uses Type4Py's local model on your machine. NOTE THAT you need to pull and run Type4Py's Docker image first. See here for more info.
||If enabled, it automatically infers type annotations when opening a Python source file. Note that automatic inference works only once for a newly opened file.
||If enabled, based on the file's imported names, irrelevent type predictions will be filtered out. Disable this setting if you would like to get all the predicted types regardless of relevancy.
||If enabled, accepted type predictions will be shared with us for research purposes and improving our Type4Py model. Note that the value of VSCode Telemetry overrides this setting. Read our privacy statement here.
Privacy is very prominent for us. You can read our privacy statements here.
Issues and errors can be reported here.
External contributions are welcome such as bug fixes and improvements! Feel free to send a pull request.
Running the extension from source
- Build the package & install dependencies using
- Compile the extension using
npm run compile
src/extension.ts in VSCode, open "Select Environment", and run in "VS Code Extension Development"
- Open a Python source code file, open the Command Palette, and run the "Type4Py: Infer type annotations" command.
- After a success message pops up, fill function annotations as usual (e.g.
def ...) ->), and type completion items will show up (if available)
- Make sure that all instances of VSCode are closed on the machine.
npm run test-compile
To develop and test the web version of the extension, run the following commands at the root of the project:
npm install && npm run compile-web
npx vscode-test-web --browserType=chromium --extensionDevelopmentPath=.
This opens up the VSCode Web at
localhost:3000 with the extension.
Here are the desirable features for future releases.
- Implementing a caching solution to preserve type predictions when source files change.
- Enabling the type-checking process for the Type4Py's predictions using mypy, preferably at the client-side.
- Fine-tuning the (pre-trained) Type4Py model on users' projects to learn project-specific types.
- Amir M. Mir (@mir-am)
- Evaldas Latoškinas (@elatoskinas)
- Sebastian Proksch (@proksch)
- Georgios Gousios (@gousiosg)
The Type4Py model and its VSCode extension are designed and developed in SERG at the Delft University of Technology.