Plugin Silently Fails When Mypy `non_interactive` Option Is Set

by ADMIN 64 views

Introduction

The mypy plugin is designed to provide real-time type checking and error reporting in the editor. However, when the non_interactive option is set in the setup.cfg file, the plugin fails to report typing errors, even the most obvious ones. This article aims to reproduce the issue, analyze the logs, and provide a solution to this problem.

Diagnostic Data

Python Version and Distribution

  • Python version: 3.13.2
  • Distribution: venv (but probably any)

Operating System and Version

  • Operating system: Fedora Linux 41 (but again, probably any)

Version of Tool Extension

  • Version of tool extension: 2025.2.0

Behaviour

Expected Behavior

The plugin shows typing errors in the editor.

Actual Behavior

Even the most obvious typing errors do not show up in the editor.

Reproduction Steps

Step 1: Add a setup.cfg File

Add a setup.cfg file to the project with the following content:

[mypy]
install_types = True
non_interactive = True

Step 2: Create a Python File with Typing Errors

Create a Python file with typing errors:

def broken() -> bool:
    an_int: int = 42
    a_string = "string"
    kaputt = an_int + a_string
    return kaputt

Logs

Without the non_interactive Option

When everything works fine, the logs show the following:

2025-04-22 17:14:01.400 [info] /bin/python -m mypy --no-color-output --no-error-summary --show-absolute-path --show-column-numbers --show-error-codes --no-pretty --show-error-end /home/someone/DummyProject/mypy_kaputt.py
2025-04-22 17:14:01.401 [info] CWD Server: /home/someone/DummyProject
2025-04-22 17:14:01.677 [info] file:///home/someone/DummyProject/mypy_kaputt.py :
/home/someone/DummyProject/mypy_kaputt.py:4:23:4:30: error: Unsupported operand types for + ("int" and "str")  [operator]
/home/someone/DummyProject/mypy_kaputt.py:5:12:5:17: error: Incompatible return value type (got "int", expected "bool")  [return-value]

2025-04-22 17:14:01.677 [info] file:///home/someone/DummyProject/mypy_kaputt.py :
/home/someone/DummyProject/mypy_kaputt.py:4:23:4:30: error: Unsupported operand types for + ("int" and "str")  [operator]
/home/someone/DummyProject/mypy_kaputt.py:5:12:5:17: error: Incompatible return value type (got "int", expected "bool")  [return-value]

With the non_interactive Option Set

When the non_interactive option is set, the logs show the following:

2025-04-22 17:11:54.005 [info] /bin/python -m mypy --no-color-output --no-error-summary --show-absolute-path --show-column-numbers --show-error-codes --no-pretty --show-error-end /home/someone/DummyProject/mypy_kaputt.py
2025-04-22 17:11:54.005 [info] CWD Server: /home/someone/DummyProject
2025-04-22 17:11:54.290 [info] /home/someone/DummyProject/mypy_kaputt.py:4:23:4:30: error: Unsupported operand types for + ("int" and "str")  [operator]
/home/someone/DummyProject/mypy_kaputt.py:5:12:5:17: error: Incompatible return value type (got "int", expected "bool")  [return-value]

2025-04-22 17:11:54.291 [info] file:///home/someone/DummyProject/mypy_kaputt.py :

Note that even though the typing errors are still in the log, the preceding file:/// line is absent. It seems the plugin relies on this.

Conclusion

The mypy plugin silently fails when the non_interactive option is set in the setup.cfg file. The plugin relies on the presence of the file:/// line in the logs to report typing errors. However, when the non_interactive option is set, this line is absent, causing the plugin to fail. Ideally, the plugin should just work regardless of this setting. A solution to this problem would be to modify the plugin to handle the absence of the file:/// line in the logs when the non_interactive option is set.

Recommendations

  1. Modify the plugin to handle the absence of the file:/// line in the logs when the non_interactive option is set.
  2. Provide a warning or an error message when the non_interactive option is set, indicating that the plugin may not work as expected.
  3. Consider adding a feature to the plugin to allow users to configure the plugin to work with the non_interactive option set.

Q: What is the mypy plugin and what is its purpose?

A: The mypy plugin is a tool that provides real-time type checking and error reporting in the editor. Its purpose is to help developers catch type-related errors and improve code quality.

Q: What is the non_interactive option and how does it affect the plugin?

A: The non_interactive option is a configuration setting in the setup.cfg file that tells mypy to run in non-interactive mode. When this option is set, the plugin fails to report typing errors, even the most obvious ones.

Q: Why does the plugin rely on the presence of the file:/// line in the logs?

A: The plugin relies on the presence of the file:/// line in the logs to determine the file path and report typing errors. When the non_interactive option is set, this line is absent, causing the plugin to fail.

Q: What are the consequences of the plugin failing to report typing errors?

A: The consequences of the plugin failing to report typing errors are that developers may not catch type-related errors, which can lead to bugs and errors in the code. This can result in wasted time and resources, and can even lead to security vulnerabilities.

Q: How can developers troubleshoot the issue with the plugin?

A: Developers can troubleshoot the issue with the plugin by checking the logs for the absence of the file:/// line. They can also try setting the non_interactive option to False and see if the plugin works as expected.

Q: What are the recommended solutions to this issue?

A: The recommended solutions to this issue are:

  1. Modify the plugin to handle the absence of the file:/// line in the logs when the non_interactive option is set.
  2. Provide a warning or an error message when the non_interactive option is set, indicating that the plugin may not work as expected.
  3. Consider adding a feature to the plugin to allow users to configure the plugin to work with the non_interactive option set.

Q: How can developers contribute to the solution of this issue?

A: Developers can contribute to the solution of this issue by:

  1. Reporting the issue and providing detailed logs and configuration settings.
  2. Helping to modify the plugin to handle the absence of the file:/// line in the logs.
  3. Providing feedback and testing the plugin with the non_interactive option set.

By following these steps, developers can help to improve the mypy plugin and ensure that it works as expected, even when the non_interactive option is set.