From 9efa70534506044a4ffdbb33c5cc79976515a6f9 Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Tue, 29 Jul 2025 09:40:37 +0200 Subject: [PATCH] Shutdown dmypy after a configurable timeout We call `dmypy run` with the `--timeout` option so that the daemon will shut down automatically after specified delay of inactivity. The timeout value is configurable through `dmypy_timeout` option, it defaults to 60s. When calling `dmypy restart` the `--timeout` option is also passed. Fix #88. --- README.rst | 5 +++++ pylsp_mypy/plugin.py | 23 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 37451c3..18c3803 100644 --- a/README.rst +++ b/README.rst @@ -60,6 +60,11 @@ Configuration - ``string`` - **Specifies which status file dmypy should use**. This modifies the ``--status-file`` option passed to ``dmypy`` given ``dmypy`` is active. - ``.dmypy.json`` + * - ``dmypy_timeout`` + - ``pylsp.plugins.pylsp_mypy.dmypy_timeout`` + - ``string`` + - **Specifies the inactivity timeout after which the mypy daemon will automatically shut down**. + - ``60`` * - ``config_sub_paths`` - ``pylsp.plugins.pylsp_mypy.config_sub_paths`` - ``array`` of ``string`` items diff --git a/pylsp_mypy/plugin.py b/pylsp_mypy/plugin.py index 8f954af..254c7de 100644 --- a/pylsp_mypy/plugin.py +++ b/pylsp_mypy/plugin.py @@ -293,6 +293,7 @@ def get_diagnostics( if dmypy: dmypy_status_file = settings.get("dmypy_status_file", ".dmypy.json") + dmypy_timeout = int(settings.get("dmypy_timeout", 60)) args = ["--show-error-end", "--no-error-summary", "--no-pretty"] @@ -377,7 +378,14 @@ def get_diagnostics( errors.strip(), ) subprocess.run( - ["dmypy", "--status-file", dmypy_status_file, "restart"], + [ + "dmypy", + "--status-file", + dmypy_status_file, + "restart", + "--timeout", + str(dmypy_timeout), + ], capture_output=True, **windows_flag, encoding="utf-8", @@ -395,10 +403,19 @@ def get_diagnostics( exit_status, errors.strip(), ) - mypy_api.run_dmypy(["--status-file", dmypy_status_file, "restart"]) + mypy_api.run_dmypy( + ["--status-file", dmypy_status_file, "restart", "--timeout", str(dmypy_timeout)] + ) # run to use existing daemon or restart if required - args = ["--status-file", dmypy_status_file, "run", "--"] + apply_overrides(args, overrides) + args = [ + "--status-file", + dmypy_status_file, + "run", + "--timeout", + str(dmypy_timeout), + "--", + ] + apply_overrides(args, overrides) if dmypy_command: # dmypy exists on PATH or was provided by settings # -> use this dmypy