Home > Back-end >  Visual Studio Code: Python Linting: Warning if import method from file that does not exist
Visual Studio Code: Python Linting: Warning if import method from file that does not exist

Time:09-30

Is it possible to get Visual Studio Code to give a warning if a method which does not exist has been imported from a file?

So in the example below, I would like if the editor would warn that there is no fun2 method in the file utilities.py

# File called `utilities.py`
def fun1(a, b):
    return a b


from utility import fun1, fun2

temp1 = fun1(1,2)
temp2 = fun2(1,2)

In VS Code, I can see a difference in colours in the two functions, so it seems to know that it is not available:

enter image description here

CodePudding user response:

Methods one:

Add the following configuration in settings.json to enable linting and set linter to pylint

    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,

You will get error: No name 'fun2' in module 'utility' pylint(no-name-in-module) enter image description here

Methods two:

Change python.analysis.typeCheckingMode to basic or strict in settings enter image description here

You can also add the following directly to the set

    "python.analysis.typeCheckingMode": "strict",

This will give error hints by pylance enter image description here

The underscore warning appears both ways enter image description here

  • Related