From e93d456f86e7c74e83a9662c3492d17444fdbf6b Mon Sep 17 00:00:00 2001 From: Zhaoqi Xu Date: Sun, 4 Oct 2026 16:57:32 +0800 Subject: [PATCH] fix: preserve statsd_prefix from config when CLI omits it --statsd-prefix used default="", so argparse always supplied an empty string and overwrote any value loaded from a config file. Use the same sentinel default as the other optional flags so file/object/toml config keeps its prefix unless the flag is passed explicitly. Fixes #382. --- src/hypercorn/__main__.py | 2 +- tests/test___main__.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/hypercorn/__main__.py b/src/hypercorn/__main__.py index b3980941..b36cf69a 100644 --- a/src/hypercorn/__main__.py +++ b/src/hypercorn/__main__.py @@ -182,7 +182,7 @@ def main(sys_args: list[str] | None = None) -> int: parser.add_argument( "--statsd-host", help="The host:port of the statsd server", default=sentinel ) - parser.add_argument("--statsd-prefix", help="Prefix for all statsd messages", default="") + parser.add_argument("--statsd-prefix", help="Prefix for all statsd messages", default=sentinel) parser.add_argument( "-m", "--umask", diff --git a/tests/test___main__.py b/tests/test___main__.py index f24b3462..e114b2eb 100644 --- a/tests/test___main__.py +++ b/tests/test___main__.py @@ -83,3 +83,28 @@ def test_verify_mode_conversion(monkeypatch: MonkeyPatch) -> None: hypercorn.__main__.main(["--verify-mode", "CERT_REQUIRED", "asgi:App"]) run_multiple.assert_called() + + +def test_statsd_prefix_preserves_config_file(tmp_path, monkeypatch: MonkeyPatch) -> None: + """CLI default must not wipe statsd_prefix loaded from a config file (#382).""" + run_multiple = Mock() + monkeypatch.setattr(hypercorn.__main__, "run", run_multiple) + cfg = tmp_path / "statsd_config.py" + cfg.write_text('statsd_prefix = "myapp."\n') + hypercorn.__main__.main(["--config", f"file:{cfg}", "asgi:App"]) + run_multiple.assert_called() + config = run_multiple.call_args_list[0][0][0] + assert config.statsd_prefix == "myapp." + + +def test_statsd_prefix_cli_overrides_config(tmp_path, monkeypatch: MonkeyPatch) -> None: + run_multiple = Mock() + monkeypatch.setattr(hypercorn.__main__, "run", run_multiple) + cfg = tmp_path / "statsd_config.py" + cfg.write_text('statsd_prefix = "myapp."\n') + hypercorn.__main__.main( + ["--config", f"file:{cfg}", "--statsd-prefix", "cli.", "asgi:App"] + ) + run_multiple.assert_called() + config = run_multiple.call_args_list[0][0][0] + assert config.statsd_prefix == "cli."