Very Import-ant (Fast, Customizable Auto-Imports)
Existing Python language server extensions (e.g. Pylance) implement
auto-importing fairly well, but with a few notable drawbacks:
The auto-import functionality requires the language server to
create a drop-down, which occasionally takes a while to load (frequently
it would have been quicker for a user to navigate to the top of the
file and just add the import line manually).
The user has to accept a selection from the drop-down, which is an interruption
to the typing flow (albeit a somewhat small one, but one nonetheless).
Most (all?) of the existing Python language server extensions for VS Code
(specifically Pylance) don't allow customizing auto-import aliases.
Instead they only support a fixed set of ones (e.g. pd for pandas, np for
numpy, etc.).
Existing Python language server extensions only allow you to auto-import
as you are typing. They currently do not automatically fix known undefined
variables elsewhere in the file. No existing formatters appear to fully
support this either.
This extension aims to solve all of the above problems by providing
functionality to automatically add imports while you are typing and
without any interruption to your development flow.
Examples
Add type annotation imports as you type
Add libraries as you type
Add imports on file save
Setup
Install this extension
Add the following to your settings.json :
{
// Setting to enable this formatter
"very-import-ant.format.enable": true,
// If editor.formatOnType is true, these characters will be all characters
// that trigger an import check/addition. It is recommended to make this
// whitespace characters + the last letters of all imported variables
// in the autoImports.variables setting (see below).
"very-import-ant.onTypeTriggerCharacters": "\n \tdprt",
// Python settings
"[python]": {
"editor.defaultFormatter": "groogle.very-import-ant",
// If you want the imports to be added as you type
"editor.formatOnType": true,
// If you want the imports to be added only when you save the file
"editor.formatOnSave": true,
// If you want the imports to be added whenever you paste something into your editor
"editor.formatOnPaste": true,
},
// Notebook settings
"notebook.defaultFormatter": "groogle.very-import-ant",
"notebook.formatOnCellExecution": true,
"notebook.formatOnSave.enabled": true,
// Add the following setting if you want to specify your own list
// of auto-import variables.
// The ones listed below are all included by default.
"very-import-ant.autoImports": [
{
"variable": "pd",
"import": "import pandas as pd"
},
{
"variable": "np",
"import": "import numpy as np"
},
{
"variable": "xr",
"import": "import xarray as xr"
},
{
"variable": "xrt",
"import": "from xarray import testing as xrt"
},
{
"variable": "Any",
"import": "from typing import Any"
},
{
"variable": "Callable",
"import": "from typing import Callable"
},
{
"variable": "Dict",
"import": "from typing import Dict"
},
{
"variable": "List",
"import": "from typing import List"
},
{
"variable": "Union",
"import": "from typing import Union"
},
{
"variable": "Optional",
"import": "from typing import Optional"
},
],
}
[Only relevant if editor.formatOnType is true]: Ensure Pylance doesn't hijack on-type formatting by following the instructions here.
While this extension is a technically a formatter extension, it really only does
one thing. In order to use this extension in conjunction with another formatter
use the Multiple Formatters Extension.
Troubleshooting
See the troubleshooting doc for info on known issues and fixes.
Contribute
Feel free to
open issues or
pull requests,
and I'll do my best to respond in a timely manner.
Appreciate
I find it very rewarding to know that my projects made someone's day or
developer life a little better. If you feel so inclined,
leave a review
or buy my a coffee so I know my project helped
you out!
| |