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:
2
Answers
Methods one:
Add the following configuration in settings.json to enable linting and set linter to
pylint
You will get error:
No name 'fun2' in module 'utility' pylint(no-name-in-module)
Methods two:
Change python.analysis.typeCheckingMode to basic or strict in settings
You can also add the following directly to the set
This will give error hints by pylance
The underscore warning appears both ways
I see this when I use flake8 using as my linter, it’s great for pep8 but doesn’t catch this type of issue. To fix that I use mypy to compliment flake8 with static type checking which will flag if an import doesn’t exist. Once you’ve added mypy to your environment, add the following setting to VSCode: