From 3b42fdd12fd7748a660096dacc0a296ad7690a36 Mon Sep 17 00:00:00 2001 From: bensynapse <118375461+bensynapse@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:38:53 +0300 Subject: [PATCH] Preserve empty method names --- CHANGELOG.md | 2 ++ jsonrpcserver/methods.py | 2 +- tests/test_methods.py | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85087fd..5ff86ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -148,6 +148,8 @@ work in 5.x but give a `DeprecationWarning`, and will be removed in 6.0. ### Fixes +- `@method(name="")` now registers the empty name. It used to ignore that + name and register the function's name instead. - The development server, `serve()`, dropped every connection without a response when `sys.stderr` was `None`. That's the case in a PyInstaller app built with `--noconsole` (#269). It now logs requests through the logging diff --git a/jsonrpcserver/methods.py b/jsonrpcserver/methods.py index 4254ab2..60464c5 100644 --- a/jsonrpcserver/methods.py +++ b/jsonrpcserver/methods.py @@ -78,7 +78,7 @@ def add(a: int, b: int) -> Result: """ def decorator(func: F) -> F: - method_name = name or func.__name__ + method_name = func.__name__ if name is None else name if method_name.startswith("rpc."): warnings.warn( f"Method names starting with 'rpc.' are reserved by the JSON-RPC spec " diff --git a/tests/test_methods.py b/tests/test_methods.py index c38010a..1a64d4a 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -1,5 +1,8 @@ """Test methods.py""" +import pytest + +from jsonrpcserver import Result, Success, dispatch from jsonrpcserver.methods import global_methods, method # pylint: disable=missing-function-docstring @@ -19,3 +22,16 @@ def name() -> None: pass assert callable(global_methods["new_name"]) + + +def test_decorator_empty_name(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setitem(global_methods, "", lambda: Success("previous")) + + @method(name="") + def empty_name() -> Result: + return Success("pong") + + assert dispatch('{"jsonrpc": "2.0", "method": "", "id": 1}') == ( + '{"jsonrpc": "2.0", "result": "pong", "id": 1}' + ) + assert global_methods[""] is empty_name