-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing check using signature, allowing to use object methods as RPC endpoints #27
Open
trollfot
wants to merge
4
commits into
Ananto30:main
Choose a base branch
from
HorsemanWSGI:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pytest | ||
from zero import ZeroServer | ||
from zero.errors import ZeroException | ||
|
||
|
||
class DummyService: | ||
|
||
def hello(self): | ||
return 'world' | ||
|
||
def say_no(self) -> str: | ||
return 'no' | ||
|
||
def say_yes(self, please: bool = False) -> str: | ||
if not please: | ||
return "ask nicely." | ||
return "yes" | ||
|
||
@staticmethod | ||
def ping() -> str: | ||
return pong | ||
|
||
@classmethod | ||
def name(cls) -> str: | ||
return cls.__name__ | ||
|
||
|
||
def test_methods(): | ||
app = ZeroServer() | ||
service = DummyService() | ||
app.register_rpc(service.say_no) | ||
app.register_rpc(service.say_yes) | ||
app.register_rpc(service.ping) | ||
app.register_rpc(service.name) | ||
|
||
|
||
def test_methods_no_args(): | ||
app = ZeroServer() | ||
service = DummyService() | ||
with pytest.raises(ZeroException) as e: | ||
app.register_rpc(service.hello) | ||
assert ( | ||
str(e.value) | ||
== "`hello` has no return type hinting; RPC functions must have type hints" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,7 @@ | |
|
||
from .codegen import CodeGen | ||
from .common import get_next_available_port | ||
from .type_util import ( | ||
get_function_input_class, | ||
get_function_return_class, | ||
verify_allowed_type, | ||
verify_function_args, | ||
verify_function_input_type, | ||
verify_function_return, | ||
) | ||
from .type_util import verify_function_typing | ||
from .zero_mq import ZeroMQ | ||
|
||
# import uvloop | ||
|
@@ -83,13 +76,16 @@ def register_rpc(self, func: typing.Callable): | |
if func.__name__ == "get_rpc_contract": | ||
raise Exception("get_rpc_contract is a reserved function; cannot have `get_rpc_contract` as a RPC function") | ||
|
||
verify_function_args(func) | ||
verify_function_input_type(func) | ||
verify_function_return(func) | ||
|
||
signature = verify_function_typing(func) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if signature.parameters: | ||
input_type = tuple(signature.parameters.values())[0].annotation | ||
else: | ||
input_type = None | ||
|
||
self._rpc_router[func.__name__] = func | ||
self._rpc_input_type_map[func.__name__] = get_function_input_class(func) | ||
self._rpc_return_type_map[func.__name__] = get_function_return_class(func) | ||
self._rpc_input_type_map[func.__name__] = input_type | ||
self._rpc_return_type_map[func.__name__] = signature.return_annotation | ||
|
||
def run(self): | ||
try: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import typing | ||
import inspect | ||
from zero.errors import ZeroException | ||
|
||
# from pydantic import BaseModel | ||
|
@@ -30,32 +31,49 @@ | |
allowed_types = basic_types + typing_types + special_types + pydantic_types | ||
|
||
|
||
def verify_function_args(func: typing.Callable): | ||
arg_count = func.__code__.co_argcount | ||
def verify_function_typing(func: typing.Callable): | ||
signature = inspect.signature(func) | ||
arg_count = len(signature.parameters) | ||
|
||
if arg_count > 1: | ||
raise ZeroException( | ||
f"`{func.__name__}` has more than 1 args; RPC functions can have only one arg - msg, or no arg" | ||
f"`{func.__name__}` has more than 1 args; " | ||
"RPC functions can have only one arg - msg, or no arg" | ||
) | ||
|
||
if arg_count == 1: | ||
arg_name = func.__code__.co_varnames[0] | ||
func_arg_type = typing.get_type_hints(func) | ||
if arg_name not in func_arg_type: | ||
raise ZeroException(f"`{func.__name__}` has no type hinting; RPC functions must have type hints") | ||
|
||
for name, param in signature.parameters.items(): | ||
if param.annotation is inspect._empty: | ||
raise ZeroException( | ||
f"`{func.__name__}` argument `{name}` is not typed." | ||
) | ||
if not param.annotation in allowed_types: | ||
raise ZeroException( | ||
f"`{func.__name__}` argument `{name}` type is not supported." | ||
) | ||
|
||
if signature.return_annotation is inspect._empty: | ||
raise ZeroException( | ||
f"`{func.__name__}` has no return type hinting; " | ||
"RPC functions must have type hints" | ||
) | ||
elif not signature.return_annotation in allowed_types: | ||
raise ZeroException( | ||
f"`{func.__name__}` return type is not supported." | ||
) | ||
|
||
def verify_function_return(func: typing.Callable): | ||
types = typing.get_type_hints(func) | ||
if not types.get("return"): | ||
raise ZeroException(f"`{func.__name__}` has no return type hinting; RPC functions must have type hints") | ||
return signature | ||
Comment on lines
+34
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
|
||
def get_function_input_class(func: typing.Callable): | ||
arg_count = func.__code__.co_argcount | ||
if arg_count == 0: | ||
if inspect.ismethod(func): | ||
max_argcount = 2 | ||
else: | ||
max_argcount = 1 | ||
if arg_count == max_argcount - 1: | ||
return None | ||
if arg_count == 1: | ||
arg_name = func.__code__.co_varnames[0] | ||
if arg_count == max_argcount: | ||
arg_name = func.__code__.co_varnames[max_argcount - 1] | ||
func_arg_type = typing.get_type_hints(func) | ||
return func_arg_type[arg_name] | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are "must be" installs, which is not true for zero. It is only needed for the test and not needed for package distribution. Please remove this.