Skip to content

Commit 5a87e31

Browse files
authored
fix issubclass calls (#5753)
* fix issubclass calls * cached property question mark
1 parent b28f873 commit 5a87e31

File tree

2 files changed

+14
-51
lines changed

2 files changed

+14
-51
lines changed

reflex/components/component.py

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,48 +1021,8 @@ def get_initial_props(cls) -> set[str]:
10211021
"""
10221022
return set()
10231023

1024-
@classmethod
1025-
def _are_fields_known(cls) -> bool:
1026-
"""Check if all fields are known at compile time. True for most components.
1027-
1028-
Returns:
1029-
Whether all fields are known at compile time.
1030-
"""
1031-
return True
1032-
1033-
@classmethod
1034-
@functools.cache
1035-
def _get_component_prop_names(cls) -> set[str]:
1036-
"""Get the names of the component props. NOTE: This assumes all fields are known.
1037-
1038-
Returns:
1039-
The names of the component props.
1040-
"""
1041-
return {
1042-
name
1043-
for name in cls.get_fields()
1044-
if name in cls.get_props()
1045-
and isinstance(
1046-
field_type := types.value_inside_optional(
1047-
types.get_field_type(cls, name)
1048-
),
1049-
type,
1050-
)
1051-
and issubclass(field_type, Component)
1052-
}
1053-
1054-
def _get_components_in_props(self) -> Sequence[BaseComponent]:
1055-
"""Get the components in the props.
1056-
1057-
Returns:
1058-
The components in the props
1059-
"""
1060-
if self._are_fields_known():
1061-
return [
1062-
component
1063-
for name in self._get_component_prop_names()
1064-
for component in _components_from(getattr(self, name))
1065-
]
1024+
@functools.cached_property
1025+
def _get_component_prop_property(self) -> Sequence[BaseComponent]:
10661026
return [
10671027
component
10681028
for prop in self.get_props()
@@ -1071,6 +1031,14 @@ def _get_components_in_props(self) -> Sequence[BaseComponent]:
10711031
for component in _components_from(value)
10721032
]
10731033

1034+
def _get_components_in_props(self) -> Sequence[BaseComponent]:
1035+
"""Get the components in the props.
1036+
1037+
Returns:
1038+
The components in the props
1039+
"""
1040+
return self._get_component_prop_property
1041+
10741042
@classmethod
10751043
def _validate_children(cls, children: tuple | list):
10761044
from reflex.utils.exceptions import ChildrenTypeError
@@ -2060,15 +2028,6 @@ def get_args_spec(key: str) -> types.ArgsSpec | Sequence[types.ArgsSpec]:
20602028
self.props[camel_cased_key] = value
20612029
setattr(self, camel_cased_key, value)
20622030

2063-
@classmethod
2064-
def _are_fields_known(cls) -> bool:
2065-
"""Check if the fields are known.
2066-
2067-
Returns:
2068-
Whether the fields are known.
2069-
"""
2070-
return False
2071-
20722031
def __eq__(self, other: Any) -> bool:
20732032
"""Check if the component is equal to another.
20742033

reflex/utils/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,10 @@ def is_valid_var_type(type_: type) -> bool:
857857
if is_union(type_):
858858
return all(is_valid_var_type(arg) for arg in get_args(type_))
859859

860+
if is_literal(type_):
861+
types = {type(value) for value in get_args(type_)}
862+
return all(is_valid_var_type(type_) for type_ in types)
863+
860864
type_ = origin if (origin := get_origin(type_)) is not None else type_
861865

862866
return (

0 commit comments

Comments
 (0)