change form and improve type checking

This commit is contained in:
Khaleel Al-Adhami 2024-10-15 15:38:55 -07:00
parent 437848f5b9
commit eecc94f866
4 changed files with 16 additions and 10 deletions

View File

@ -102,7 +102,7 @@ class Fieldset(Element):
name: Var[Union[str, int, bool]]
def on_submit_event_spec() -> Tuple[Var[Dict[str, Any]]]:
def on_submit_event_spec() -> Tuple[Var[Dict[str, str]]]:
"""Event handler spec for the on_submit event.
Returns:

View File

@ -270,7 +270,7 @@ class Fieldset(Element):
"""
...
def on_submit_event_spec() -> Tuple[Var[Dict[str, Any]]]: ...
def on_submit_event_spec() -> Tuple[Var[Dict[str, str]]]: ...
class Form(BaseHTML):
@overload
@ -337,7 +337,7 @@ class Form(BaseHTML):
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_submit: Optional[EventType[Dict[str, Any]]] = None,
on_submit: Optional[EventType[Dict[str, str]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
) -> "Form":

View File

@ -129,7 +129,7 @@ class FormRoot(FormComponent, HTMLForm):
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_submit: Optional[EventType[Dict[str, Any]]] = None,
on_submit: Optional[EventType[Dict[str, str]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
) -> "FormRoot":
@ -594,7 +594,7 @@ class Form(FormRoot):
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_submit: Optional[EventType[Dict[str, Any]]] = None,
on_submit: Optional[EventType[Dict[str, str]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
) -> "Form":
@ -716,7 +716,7 @@ class FormNamespace(ComponentNamespace):
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_submit: Optional[EventType[Dict[str, Any]]] = None,
on_submit: Optional[EventType[Dict[str, str]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
) -> "Form":

View File

@ -1015,17 +1015,23 @@ def call_event_handler(
"See https://reflex.dev/docs/events/event-arguments/"
)
def compare_types(specific_type, accepted_type):
def compare_types(provided_type, accepted_type):
# Check if both are generic types (e.g., List)
if get_origin(specific_type) != get_origin(accepted_type):
if (get_origin(provided_type) or provided_type) != (
get_origin(accepted_type) or accepted_type
):
return False
# Get type arguments (e.g., int vs. Union[float, int])
specific_args = get_args(specific_type)
provided_args = get_args(provided_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))
return all(
issubclass(provided_arg, accepted_arg)
for provided_arg, accepted_arg in zip(provided_args, accepted_args)
if accepted_arg is not Any
)
event_spec_return_type = get_type_hints(arg_spec).get("return", None)