Skip to content

Commit 18642f6

Browse files
authored
Merge pull request #456 from jhonabreul/bug-451-python-logcal-debugging
Enable local Python debugging with DebugPy
2 parents 746e76d + 8f9333e commit 18642f6

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Usage: lean backtest [OPTIONS] PROJECT
143143
Options:
144144
--output DIRECTORY Directory to store results in (defaults to PROJECT/backtests/TIMESTAMP)
145145
-d, --detach Run the backtest in a detached Docker container and return immediately
146-
--debug [pycharm|ptvsd|vsdbg|rider|local-platform]
146+
--debug [pycharm|ptvsd|debugpy|vsdbg|rider|local-platform]
147147
Enable a certain debugging method (see --help for more information)
148148
--data-provider-historical [Interactive Brokers|Oanda|Bitfinex|Coinbase Advanced Trade|Binance|Kraken|IQFeed|Polygon|FactSet|IEX|AlphaVantage|CoinApi|ThetaData|QuantConnect|Local|Terminal Link|Bybit]
149149
Update the Lean configuration file to retrieve data from the given historical provider

lean/commands/backtest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def _migrate_csharp_csproj(project_dir: Path) -> None:
234234
default=False,
235235
help="Run the backtest in a detached Docker container and return immediately")
236236
@option("--debug",
237-
type=Choice(["pycharm", "ptvsd", "vsdbg", "rider", "local-platform"], case_sensitive=False),
237+
type=Choice(["pycharm", "ptvsd", "debugpy", "vsdbg", "rider", "local-platform"], case_sensitive=False),
238238
help="Enable a certain debugging method (see --help for more information)")
239239
@option("--data-provider-historical",
240240
type=Choice([dp.get_name() for dp in cli_data_downloaders], case_sensitive=False),
@@ -331,6 +331,11 @@ def backtest(project: Path,
331331
elif debug == "ptvsd":
332332
debugging_method = DebuggingMethod.PTVSD
333333
_migrate_python_vscode(algorithm_file.parent)
334+
logger.warn("The PTVSD debugging method is deprecated and might be removed in a future version of LEAN. "
335+
"Consider using DebugPy instead.")
336+
elif debug == "debugpy":
337+
debugging_method = DebuggingMethod.DebugPy
338+
_migrate_python_vscode(algorithm_file.parent)
334339
elif debug == "vsdbg":
335340
debugging_method = DebuggingMethod.VSDBG
336341
_migrate_csharp_vscode(algorithm_file.parent)

lean/components/docker/lean_runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def run_lean(self,
107107
# Add known additional run options from the extra docker config
108108
self.parse_extra_docker_config(run_options, extra_docker_config)
109109

110-
# Set up PTVSD debugging
111-
if debugging_method == DebuggingMethod.PTVSD:
110+
# Set up PTVSD or DebugPy debugging
111+
if debugging_method == DebuggingMethod.PTVSD or debugging_method == DebuggingMethod.DebugPy:
112112
run_options["ports"]["5678"] = "5678"
113113

114114
# Set up VSDBG debugging

lean/models/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class DebuggingMethod(Enum):
2222
PTVSD = 2
2323
VSDBG = 3
2424
Rider = 4
25-
LocalPlatform = 5
25+
LocalPlatform = 5
26+
DebugPy = 6
2627

2728
def get_internal_name(self) -> str:
2829
"""Returns the LEAN debugging method that should be used for the current enum member.
@@ -32,6 +33,7 @@ def get_internal_name(self) -> str:
3233
return {
3334
DebuggingMethod.PyCharm: "PyCharm",
3435
DebuggingMethod.PTVSD: "PTVSD",
36+
DebuggingMethod.DebugPy: "DebugPy",
3537
DebuggingMethod.LocalPlatform: "DebugPy" # QC -> DebugPy, If its Python it uses DebugPy, if its C# LEAN safely ignores DebugPy
3638
}.get(self, "LocalCmdline")
3739

tests/commands/test_backtest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ def _ensure_rider_debugger_config_files_exist(project_dir: Path) -> None:
283283
("PTVSD", DebuggingMethod.PTVSD),
284284
("vsdbg", DebuggingMethod.VSDBG),
285285
("VSDBG", DebuggingMethod.VSDBG),
286+
("debugpy", DebuggingMethod.DebugPy),
287+
("DebugPy", DebuggingMethod.DebugPy),
286288
("rider", DebuggingMethod.Rider),
287289
("Rider", DebuggingMethod.Rider)])
288290
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:
348350
assert workspace_xml.find(".//mapping[@remote-root='/Lean/Launcher/bin/Debug']") is None
349351

350352

351-
def test_backtest_auto_updates_outdated_python_vscode_debug_config() -> None:
353+
@pytest.mark.parametrize("value", ["ptvsd", "debugpy"])
354+
def test_backtest_auto_updates_outdated_python_vscode_debug_config(value) -> None:
352355
create_fake_lean_cli_directory()
353356

354357
lean_config_manager = container.lean_config_manager
@@ -380,7 +383,7 @@ def test_backtest_auto_updates_outdated_python_vscode_debug_config() -> None:
380383
]
381384
}))
382385

383-
result = CliRunner().invoke(lean, ["backtest", "Python Project", "--debug", "ptvsd"])
386+
result = CliRunner().invoke(lean, ["backtest", "Python Project", "--debug", value])
384387

385388
assert result.exit_code == 0
386389

0 commit comments

Comments
 (0)