fix the typecheck logic

This commit is contained in:
Lendemor 2024-10-03 17:00:04 +02:00
parent 0be141fd30
commit 26eb35899d

View File

@ -917,12 +917,24 @@ def call_event_handler(
"See https://reflex.dev/docs/events/event-arguments/" "See https://reflex.dev/docs/events/event-arguments/"
) )
def compare_types(specific_type, accepted_type):
# Check if both are generic types (e.g., List)
if get_origin(specific_type) != get_origin(accepted_type):
return False
# Get type arguments (e.g., int vs. Union[float, int])
specific_args = get_args(specific_type)
accepted_args = get_args(accepted_type)
# Ensure all specific types are compatible with accepted types
return all(issubclass(s, a) for s, a in zip(specific_args, accepted_args))
# check that args of event handler are matching the spec if type hints are provided # check that args of event handler are matching the spec if type hints are provided
for arg, arg_type in inspect.getfullargspec(arg_spec).annotations.items(): for arg, arg_type in inspect.getfullargspec(arg_spec).annotations.items():
if arg not in fullspec.annotations: if arg not in fullspec.annotations:
continue continue
if arg_type == fullspec.annotations[arg]:
print(f"continue for {arg}: {arg_type} == {fullspec.annotations[arg]}") if compare_types(fullspec.annotations[arg], arg_type):
continue continue
else: else:
raise EventHandlerArgTypeMismatch( raise EventHandlerArgTypeMismatch(