Typing check using signature, allowing to use object methods as RPC endpoints#27
Open
trollfot wants to merge 4 commits intoAnanto30:mainfrom
Open
Typing check using signature, allowing to use object methods as RPC endpoints#27trollfot wants to merge 4 commits intoAnanto30:mainfrom
trollfot wants to merge 4 commits intoAnanto30:mainfrom
Conversation
Ananto30
requested changes
Mar 19, 2023
Comment on lines
+25
to
+32
| extras_require={ | ||
| 'test': [ | ||
| 'pytest', | ||
| 'pytest-asyncio', | ||
| 'pyjwt' | ||
| ] | ||
| } | ||
| ) |
Owner
There was a problem hiding this comment.
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) |
Owner
There was a problem hiding this comment.
verify_function_typing name is not clear, it verifies and returns something, doing 2 things but the name suggests it only verifies
Comment on lines
+34
to
+64
| 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 |
Owner
Owner
|
@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
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

No description provided.