0

Is there a way to configure VS Code to only execute a python script if there are no linter errors? I am using Python 3.9.0 and VS Code 1.49.0. Linter is mypy (mypy-0.790, mypy-extensions-0.4.3, typed-ast-1.4.1, typing-extensions-3.7.4.3). OS is Windows 10, version 2004.

Currently, while linter reports problems correctly (incompatible type, based on type hints, for instance), pressing F5 still runs the code (which crashes, as the linter is correct). I would like VS Code to only proceed to the "Run" step if the linter output has no errors, as the time taken to execute a script with linter errors is basically a waste of time.

How can this be configured?

1 Answer 1

1

First, create a new task. If you don't already have a tasks.json file in the .vscode folder (or you don't have a .vscode folder), you can press F1 and run "Tasks: Configure Task", then select "Create tasks.json file from template". Select "Others" from the list of templates. It should generate a file that looks like this:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

Change the label to "lint" (or any other label that suits your fancy), and change the command to "mypy <directories or files>". For more information on the mypy command, refer to the docs.

If you do already have a tasks.json file, just copy the part inside the tasks array into your tasks array and make the same changes.

Then, open your launch.json and add the following line into whichever configuration(s) you're using.

"preLaunchTask": "lint",

If the task fails, you'll see a dialogue box pop up saying

The preLaunchTask 'lint' terminated with exit code 1.

and asking if you want to "Debug Anyway", Show Errors", or "Abort". There'll also be a checkbox to have VS Code remember your choice in your settings (if you'd like to set this before the first time you get an error, the setting is named debug.onTaskErrors).

Additional resources:
Tasks Documentation
Debugging Documentation
Description of debug.onTaskErrors

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.