From 4aae3a5ebf040b1a080dbe82200a9a8cf278ca57 Mon Sep 17 00:00:00 2001 From: Jan Segre Date: Fri, 26 Sep 2025 20:27:30 +0200 Subject: [PATCH] fix(nano): API was not processing initialize method correctly --- hathor/nanocontracts/resources/blueprint.py | 14 +++++++++----- tests/resources/nanocontracts/my_blueprint.py | 2 +- tests/resources/nanocontracts/test_blueprint.py | 5 ++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/hathor/nanocontracts/resources/blueprint.py b/hathor/nanocontracts/resources/blueprint.py index 242f3beab..307a1a950 100644 --- a/hathor/nanocontracts/resources/blueprint.py +++ b/hathor/nanocontracts/resources/blueprint.py @@ -112,17 +112,21 @@ def render_GET(self, request: 'Request') -> bytes: continue method_args = [] - argspec = inspect.getfullargspec(method) - for arg_name in argspec.args[1:]: - arg_type = argspec.annotations[arg_name] + signature = inspect.signature(method) + for parameter in signature.parameters.values(): + if parameter.name == 'self': + continue + arg_type = parameter.annotation if arg_type is Context: continue method_args.append(MethodArgInfo( - name=arg_name, + name=parameter.name, type=self.get_type_name(arg_type), )) - return_type = argspec.annotations.get('return', None) + return_type = signature.return_annotation + if return_type is inspect._empty: + return_type = None method_info = MethodInfo( args=method_args, diff --git a/tests/resources/nanocontracts/my_blueprint.py b/tests/resources/nanocontracts/my_blueprint.py index 59efb7578..25da3f92b 100644 --- a/tests/resources/nanocontracts/my_blueprint.py +++ b/tests/resources/nanocontracts/my_blueprint.py @@ -21,7 +21,7 @@ class MyBlueprint(Blueprint): a_optional_int: Optional[int] @public - def initialize(self, ctx: Context) -> None: + def initialize(self, ctx: Context, arg1: int) -> None: pass @public diff --git a/tests/resources/nanocontracts/test_blueprint.py b/tests/resources/nanocontracts/test_blueprint.py index aa2e34790..2960bb724 100644 --- a/tests/resources/nanocontracts/test_blueprint.py +++ b/tests/resources/nanocontracts/test_blueprint.py @@ -74,7 +74,10 @@ def test_success(self) -> Generator[Deferred[Any], Any, None]: }) self.assertEqual(data['public_methods'], { 'initialize': { - 'args': [], + 'args': [{ + 'name': 'arg1', + 'type': 'int', + }], 'return_type': 'null', 'docstring': None, },