Skip to content
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
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

trollfot
Copy link

No description provided.

Comment on lines +25 to +32
'test': [
'pytest',
'pytest-asyncio',
'pyjwt'
]
}
)
Copy link
Owner

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)
Copy link
Owner

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

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think "inspect" is a good solution. Most of the tests are failing, it cannot even compare types, as an example list is supported type but it fails to recognize it
image

@Ananto30
Copy link
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 😞

@Ananto30 Ananto30 force-pushed the main branch 3 times, most recently from c05b03a to 04e5369 Compare June 28, 2024 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants