Skip to content

Commit

Permalink
Merge pull request #456 from jhonabreul/bug-451-python-logcal-debugging
Browse files Browse the repository at this point in the history
Enable local Python debugging with DebugPy
  • Loading branch information
jhonabreul authored May 7, 2024
2 parents 746e76d + 8f9333e commit 18642f6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lean/commands/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lean/components/docker/lean_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lean/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")

Expand Down
7 changes: 5 additions & 2 deletions tests/commands/test_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 18642f6

Please sign in to comment.