An extension called Pylance that can be used with VS Code etc. was released, so I tried using it immediately.
Reference: Accelerate Python development with type hints-Microsoft announces extension "Pylance" for VS Code
--It is an extension of Python made by Microsoft. --Python input completion, which is included in Python extensions, is enabled. This doesn't change much because most people who write Python code using VS Code will have Python extensions. --Similar to the Python type check Pyright I wrote earlier, the completion will work by writing the code and annotating the type in the place where the type mistake or completion is difficult to work (Pyright internally). It is used). --Pyright's article: Type annotations for richer input completion and more robust Python code and an introduction to Pyright --It will check for errors where imports are missing and give you the option to insert the missing imports into your code. --As of 2020-07-02, when this article is written, it is still in the Preview version.
Just search for pylance etc. on the VS Code extension page and install it.
However, in my case, I had the Pyright extension installed in advance, but I got an error when I installed Pylance, probably because of a conflict. After disabling the Pyright extension and restarting VS Code, the error went away.
By default, the type checking feature is disabled. To enable it, open the VS Code settings screen, search for "python.analysis.typeCheckingMode" and switch the settings to basic or strict.
basic seems to be a slightly loose check, such as checking whether it is correct at the place where the type annotation is done.
In strict, it seems that the part itself that does not have type annotation etc. also causes an error. If it is strict from the beginning, there is no problem, but if you start type annotation from the middle and set strict, a lot of errors will appear in the existing code and it will be a bit painful: sweat:
Wasn't it in Pyright? As a function, a function that imports when you forget to import in Python code has been added to Pylance.
When I was working with this feature in a language other than Python, I used to use something similar, so I'm happy to be able to use it!
In cases where import is insufficient, an error will be displayed underlined in red on VS Code as shown below.
In that state, if you move the cursor position to the part where the error occurs (np
in the image), an icon that looks like a light bulb will appear.
In this state, press Ctrl + .
or click the light bulb icon to display a list of imports to insert. It's wise to recognize np without doing numpy ...!
If you select the appropriate one, the import statement will be inserted.
You can also use this function by clicking "Quick Fix ..." on the pop-up that appears when you mouse over.
Recommended Posts