Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 98ad8ae

Browse files
committed
Use the passed-in name for constructing repr() of JsonMethod.
This lets users pass in callables that don't have a `__name__` (like instances of functools.partial). Signed-off-by: Zsolt Dollenstein <[email protected]>
1 parent b9d375c commit 98ad8ae

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

aiohttp_json_rpc/auth/django.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ async def prepare_request(self, request, user=None):
213213

214214
if action in ('view', 'add', 'change', 'delete', ):
215215
request.methods[method_name] = JsonRpcMethod(
216-
self.handle_orm_call)
216+
self.handle_orm_call, method_name)
217217

218218
# rpc defined methods
219219
for name, method in request.rpc.methods.items():

aiohttp_json_rpc/rpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
class JsonRpcMethod:
3232
CREDENTIAL_KEYS = ['request', 'worker_pool']
3333

34-
def __init__(self, method):
34+
def __init__(self, method, name=None):
3535
self.method = method
36+
self.name = name
3637

3738
# method introspection
3839
try:
@@ -80,7 +81,7 @@ def __init__(self, method):
8081
]
8182

8283
self._repr_str = 'JsonRpcMethod({}({}))'.format(
83-
self.method.__name__,
84+
name or self.method.__name__,
8485
', '.join(args),
8586
)
8687

@@ -179,7 +180,7 @@ def _add_method(self, method, name='', prefix=''):
179180
if prefix:
180181
name = '{}__{}'.format(prefix, name)
181182

182-
self.methods[name] = JsonRpcMethod(method)
183+
self.methods[name] = JsonRpcMethod(method, name)
183184

184185
def _add_methods_from_object(self, obj, prefix='', ignore=[]):
185186
for attr_name in dir(obj):

tests/test_methods.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,19 @@ async def unnamed_method(request):
101101

102102
assert await client.call('method1') == 'method1'
103103
assert await client.call('method2') == 'method2'
104+
105+
assert 'method1' in repr(rpc_context.rpc.methods['method1'])
106+
107+
108+
@pytest.mark.asyncio
109+
async def test_partial_method(rpc_context, caplog):
110+
from functools import partial
111+
112+
async def pong(request, msg):
113+
return msg
114+
115+
rpc_context.rpc.add_methods(
116+
('', partial(pong, msg="pong"), "pong"),
117+
)
118+
119+
assert 'pong' in repr(rpc_context.rpc.methods['pong'])

0 commit comments

Comments
 (0)