-
-
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
base: main
Are you sure you want to change the base?
Conversation
'test': [ | ||
'pytest', | ||
'pytest-asyncio', | ||
'pyjwt' | ||
] | ||
} | ||
) |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
verify_function_typing
name is not clear, it verifies and returns something, doing 2 things but the name suggests it only verifies
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 |
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.
@trollfot thank you for the PR! 🙌 Unfortunately, the tests are failing and I think "inspect" is not a good solution for thorough type checking. I have tried it before but forgot why didn't use it 😞 |
c05b03a
to
04e5369
Compare
No description provided.