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

Commit

Permalink
Use the passed-in name for constructing repr() of JsonMethod.
Browse files Browse the repository at this point in the history
This lets users pass in callables that don't have a `__name__` (like instances of functools.partial).

Signed-off-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
  • Loading branch information
zsol committed Sep 26, 2021
1 parent b9d375c commit 98ad8ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aiohttp_json_rpc/auth/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async def prepare_request(self, request, user=None):

if action in ('view', 'add', 'change', 'delete', ):
request.methods[method_name] = JsonRpcMethod(
self.handle_orm_call)
self.handle_orm_call, method_name)

# rpc defined methods
for name, method in request.rpc.methods.items():
Expand Down
7 changes: 4 additions & 3 deletions aiohttp_json_rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
class JsonRpcMethod:
CREDENTIAL_KEYS = ['request', 'worker_pool']

def __init__(self, method):
def __init__(self, method, name=None):
self.method = method
self.name = name

# method introspection
try:
Expand Down Expand Up @@ -80,7 +81,7 @@ def __init__(self, method):
]

self._repr_str = 'JsonRpcMethod({}({}))'.format(
self.method.__name__,
name or self.method.__name__,
', '.join(args),
)

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

self.methods[name] = JsonRpcMethod(method)
self.methods[name] = JsonRpcMethod(method, name)

def _add_methods_from_object(self, obj, prefix='', ignore=[]):
for attr_name in dir(obj):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,19 @@ async def unnamed_method(request):

assert await client.call('method1') == 'method1'
assert await client.call('method2') == 'method2'

assert 'method1' in repr(rpc_context.rpc.methods['method1'])


@pytest.mark.asyncio
async def test_partial_method(rpc_context, caplog):
from functools import partial

async def pong(request, msg):
return msg

rpc_context.rpc.add_methods(
('', partial(pong, msg="pong"), "pong"),
)

assert 'pong' in repr(rpc_context.rpc.methods['pong'])

0 comments on commit 98ad8ae

Please sign in to comment.