add custom type

This commit is contained in:
Khaleel Al-Adhami 2024-10-21 12:36:41 -07:00
parent 89ea0de4c5
commit a7eae53309
2 changed files with 9 additions and 4 deletions

View File

@ -76,6 +76,7 @@ from reflex.utils import console, format, path_ops, prerequisites, types
from reflex.utils.exceptions import ( from reflex.utils.exceptions import (
ComputedVarShadowsBaseVars, ComputedVarShadowsBaseVars,
ComputedVarShadowsStateVar, ComputedVarShadowsStateVar,
DynamicComponentInvalidSignature,
DynamicRouteArgShadowsStateVar, DynamicRouteArgShadowsStateVar,
EventHandlerShadowsBuiltInStateMethod, EventHandlerShadowsBuiltInStateMethod,
ImmutableStateError, ImmutableStateError,
@ -2105,8 +2106,8 @@ def dynamic(func: Callable[[T], Component]):
The dynamically generated component. The dynamically generated component.
Raises: Raises:
ValueError: If the function does not have exactly one parameter. DynamicComponentInvalidSignature: If the function does not have exactly one parameter.
ValueError: If the function does not have a type hint for the state class. DynamicComponentInvalidSignature: If the function does not have a type hint for the state class.
""" """
number_of_parameters = len(inspect.signature(func).parameters) number_of_parameters = len(inspect.signature(func).parameters)
@ -2118,12 +2119,12 @@ def dynamic(func: Callable[[T], Component]):
values = list(func_signature.values()) values = list(func_signature.values())
if number_of_parameters != 1: if number_of_parameters != 1:
raise ValueError( raise DynamicComponentInvalidSignature(
"The function must have exactly one parameter, which is the state class." "The function must have exactly one parameter, which is the state class."
) )
if len(values) != 1: if len(values) != 1:
raise ValueError( raise DynamicComponentInvalidSignature(
"You must provide a type hint for the state class in the function." "You must provide a type hint for the state class in the function."
) )

View File

@ -135,3 +135,7 @@ class SetUndefinedStateVarError(ReflexError, AttributeError):
class StateSchemaMismatchError(ReflexError, TypeError): class StateSchemaMismatchError(ReflexError, TypeError):
"""Raised when the serialized schema of a state class does not match the current schema.""" """Raised when the serialized schema of a state class does not match the current schema."""
class DynamicComponentInvalidSignature(ReflexError, TypeError):
"""Raised when a dynamic component has an invalid signature."""