From b61e3d30c6452ff280cb19714fedaa8220340d04 Mon Sep 17 00:00:00 2001 From: zerlok Date: Sat, 30 Nov 2024 16:33:09 +0100 Subject: [PATCH] fix Client.publisher * make this method synchronous to use in `async with` * add Server & Client mypy test cases --- pyproject.toml | 2 +- src/brokrpc/rpc/client.py | 2 +- tests/test_mypy.yml | 96 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e9072a5..f2b4729 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "BrokRPC" -version = "0.2.1" +version = "0.2.2" description = "framework for gRPC like server-client communication over message brokers" authors = ["zerlok "] readme = "README.md" diff --git a/src/brokrpc/rpc/client.py b/src/brokrpc/rpc/client.py index 013972e..aaa5b31 100644 --- a/src/brokrpc/rpc/client.py +++ b/src/brokrpc/rpc/client.py @@ -20,7 +20,7 @@ class Client: def __init__(self, broker: Broker) -> None: self.__broker = broker - async def publisher[U]( + def publisher[U]( self, *, routing_key: str, diff --git a/tests/test_mypy.yml b/tests/test_mypy.yml index bf20a3e..6434c75 100644 --- a/tests/test_mypy.yml +++ b/tests/test_mypy.yml @@ -1,3 +1,99 @@ +- case: server + main: |- + import typing + + from brokrpc.abc import Serializer + from brokrpc.message import Message, BinaryMessage + from brokrpc.rpc.abc import RPCSerializer + from brokrpc.rpc.model import Request + from brokrpc.rpc.server import Server + from brokrpc.serializer.json import JSONSerializer + + from stubs import * + + def main(server: Server, func: {{ func }}, ser: {{ ser }}) -> None: + server.{{ method }}(func=func, routing_key="test", serializer=ser) # {{ expected }} + + parametrized: + # positive + - method: register_consumer + func: typing.Callable[[Message[Foo]], None] + ser: Serializer[Message[Foo], BinaryMessage] + expected: Y + - method: register_consumer + func: typing.Callable[[Message[object]], typing.Coroutine[typing.Any, typing.Any, bool]] + ser: JSONSerializer + expected: Y + - method: register_unary_unary_handler + func: typing.Callable[[Request[Foo]], typing.Coroutine[typing.Any, typing.Any, Bar]] + ser: RPCSerializer[Foo, Bar] + expected: Y + - method: register_unary_unary_handler + func: typing.Callable[[Request[object]], object] + ser: JSONSerializer + expected: Y + files: + - path: stubs.py + content: |- + class Foo: + pass + + class Bar: + pass + + mypy_config: |- + [mypy-aiormq.*,pamqp.*,pkg_resources.*] + ignore_missing_imports = True + ignore_errors = True + + +- case: client + main: |- + from brokrpc.abc import Serializer + from brokrpc.message import Message, BinaryMessage + from brokrpc.rpc.abc import RPCSerializer + from brokrpc.rpc.client import Client + from brokrpc.serializer.json import JSONSerializer + + from stubs import * + + async def main(client: Client, ser: {{ ser }}) -> None: + async with client.{{ method }}(routing_key="test", serializer=ser) as caller: + reveal_type(caller) # {{ expected }} + + parametrized: + # positive + - method: publisher + ser: Serializer[Message[Foo], BinaryMessage] + expected: |- + N: Revealed type is "brokrpc.abc.Publisher[stubs.Foo, Union[builtins.bool, None]]" + - method: publisher + ser: JSONSerializer + expected: |- + N: Revealed type is "brokrpc.abc.Publisher[builtins.object, Union[builtins.bool, None]]" + - method: unary_unary_caller + ser: RPCSerializer[Foo, Bar] + expected: |- + N: Revealed type is "brokrpc.rpc.abc.Caller[stubs.Foo, stubs.Bar]" + - method: unary_unary_caller + ser: JSONSerializer + expected: |- + N: Revealed type is "brokrpc.rpc.abc.Caller[builtins.object, builtins.object]" + files: + - path: stubs.py + content: |- + class Foo: + pass + + class Bar: + pass + + mypy_config: |- + [mypy-aiormq.*,pamqp.*,pkg_resources.*] + ignore_missing_imports = True + ignore_errors = True + + - case: message_body_covariance main: |- from stubs import *