skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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:

        "python.linting.enabled": true,
        "python.linting.flake8Enabled": true,     
        "python.linting.mypyEnabled": true,
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search