From 5c85bbd49f4478278ed20c93e6d2cd7df88f4ca3 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Tue, 7 May 2024 10:23:20 -0400 Subject: [PATCH 1/2] Enable local Python debugging with DebugPy. Warn PTVSD deprecation. --- lean/commands/backtest.py | 5 +++++ lean/components/docker/lean_runner.py | 4 ++-- lean/models/utils.py | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lean/commands/backtest.py b/lean/commands/backtest.py index 832fe228..b0280878 100644 --- a/lean/commands/backtest.py +++ b/lean/commands/backtest.py @@ -331,6 +331,11 @@ def backtest(project: Path, elif debug == "ptvsd": debugging_method = DebuggingMethod.PTVSD _migrate_python_vscode(algorithm_file.parent) + logger.warn("The PTVSD debugging method is deprecated and might be removed in a future version of LEAN. " + "Consider using DebugPy instead.") + elif debug == "debugpy": + debugging_method = DebuggingMethod.DebugPy + _migrate_python_vscode(algorithm_file.parent) elif debug == "vsdbg": debugging_method = DebuggingMethod.VSDBG _migrate_csharp_vscode(algorithm_file.parent) diff --git a/lean/components/docker/lean_runner.py b/lean/components/docker/lean_runner.py index 4bbe3849..7c08ba1d 100644 --- a/lean/components/docker/lean_runner.py +++ b/lean/components/docker/lean_runner.py @@ -107,8 +107,8 @@ def run_lean(self, # Add known additional run options from the extra docker config self.parse_extra_docker_config(run_options, extra_docker_config) - # Set up PTVSD debugging - if debugging_method == DebuggingMethod.PTVSD: + # Set up PTVSD or DebugPy debugging + if debugging_method == DebuggingMethod.PTVSD or debugging_method == DebuggingMethod.DebugPy: run_options["ports"]["5678"] = "5678" # Set up VSDBG debugging diff --git a/lean/models/utils.py b/lean/models/utils.py index fd53afcc..7c868d01 100644 --- a/lean/models/utils.py +++ b/lean/models/utils.py @@ -22,7 +22,8 @@ class DebuggingMethod(Enum): PTVSD = 2 VSDBG = 3 Rider = 4 - LocalPlatform = 5 + LocalPlatform = 5 + DebugPy = 6 def get_internal_name(self) -> str: """Returns the LEAN debugging method that should be used for the current enum member. @@ -32,6 +33,7 @@ def get_internal_name(self) -> str: return { DebuggingMethod.PyCharm: "PyCharm", DebuggingMethod.PTVSD: "PTVSD", + DebuggingMethod.DebugPy: "DebugPy", DebuggingMethod.LocalPlatform: "DebugPy" # QC -> DebugPy, If its Python it uses DebugPy, if its C# LEAN safely ignores DebugPy }.get(self, "LocalCmdline") From 8f9333e7448ede120c8b3cc3c30500e62c2d7b20 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Tue, 7 May 2024 10:47:25 -0400 Subject: [PATCH 2/2] Extend unit tests and update readme --- README.md | 2 +- lean/commands/backtest.py | 2 +- tests/commands/test_backtest.py | 7 +++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index df71f3e4..88459e28 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ Usage: lean backtest [OPTIONS] PROJECT Options: --output DIRECTORY Directory to store results in (defaults to PROJECT/backtests/TIMESTAMP) -d, --detach Run the backtest in a detached Docker container and return immediately - --debug [pycharm|ptvsd|vsdbg|rider|local-platform] + --debug [pycharm|ptvsd|debugpy|vsdbg|rider|local-platform] Enable a certain debugging method (see --help for more information) --data-provider-historical [Interactive Brokers|Oanda|Bitfinex|Coinbase Advanced Trade|Binance|Kraken|IQFeed|Polygon|FactSet|IEX|AlphaVantage|CoinApi|ThetaData|QuantConnect|Local|Terminal Link|Bybit] Update the Lean configuration file to retrieve data from the given historical provider diff --git a/lean/commands/backtest.py b/lean/commands/backtest.py index b0280878..9c0836d2 100644 --- a/lean/commands/backtest.py +++ b/lean/commands/backtest.py @@ -234,7 +234,7 @@ def _migrate_csharp_csproj(project_dir: Path) -> None: default=False, help="Run the backtest in a detached Docker container and return immediately") @option("--debug", - type=Choice(["pycharm", "ptvsd", "vsdbg", "rider", "local-platform"], case_sensitive=False), + type=Choice(["pycharm", "ptvsd", "debugpy", "vsdbg", "rider", "local-platform"], case_sensitive=False), help="Enable a certain debugging method (see --help for more information)") @option("--data-provider-historical", type=Choice([dp.get_name() for dp in cli_data_downloaders], case_sensitive=False), diff --git a/tests/commands/test_backtest.py b/tests/commands/test_backtest.py index 9a3a373d..80d48260 100644 --- a/tests/commands/test_backtest.py +++ b/tests/commands/test_backtest.py @@ -283,6 +283,8 @@ def _ensure_rider_debugger_config_files_exist(project_dir: Path) -> None: ("PTVSD", DebuggingMethod.PTVSD), ("vsdbg", DebuggingMethod.VSDBG), ("VSDBG", DebuggingMethod.VSDBG), + ("debugpy", DebuggingMethod.DebugPy), + ("DebugPy", DebuggingMethod.DebugPy), ("rider", DebuggingMethod.Rider), ("Rider", DebuggingMethod.Rider)]) def test_backtest_passes_correct_debugging_method_to_lean_runner(value: str, debugging_method: DebuggingMethod) -> None: @@ -348,7 +350,8 @@ def test_backtest_auto_updates_outdated_python_pycharm_debug_config() -> None: assert workspace_xml.find(".//mapping[@remote-root='/Lean/Launcher/bin/Debug']") is None -def test_backtest_auto_updates_outdated_python_vscode_debug_config() -> None: +@pytest.mark.parametrize("value", ["ptvsd", "debugpy"]) +def test_backtest_auto_updates_outdated_python_vscode_debug_config(value) -> None: create_fake_lean_cli_directory() lean_config_manager = container.lean_config_manager @@ -380,7 +383,7 @@ def test_backtest_auto_updates_outdated_python_vscode_debug_config() -> None: ] })) - result = CliRunner().invoke(lean, ["backtest", "Python Project", "--debug", "ptvsd"]) + result = CliRunner().invoke(lean, ["backtest", "Python Project", "--debug", value]) assert result.exit_code == 0