Skip to content

Commit

Permalink
use even better logic for finding state wrt computedvar
Browse files Browse the repository at this point in the history
  • Loading branch information
adhami3310 committed Aug 17, 2024
1 parent dd13dba commit 397683a
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions reflex/ivars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
List,
Literal,
Optional,
Sequence,
Set,
Tuple,
Type,
Expand Down Expand Up @@ -1485,22 +1486,38 @@ def __get__(self, instance: BaseState | None, owner):
Returns:
The value of the var for the given instance.
"""
from reflex.state import BaseState

if instance is None:
list_of_modules = self.fget.__qualname__.split(".")
from reflex.state import BaseState

path_to_function = self.fget.__qualname__.split(".")
class_name_where_defined = (
list_of_modules[-2] if len(list_of_modules) > 1 else owner.__name__
path_to_function[-2] if len(path_to_function) > 1 else owner.__name__
)
classes_where_defined = [
c
for c in inspect.getmro(owner)
if c.__name__ == class_name_where_defined
]

def contains_class_name(states: Sequence[Type]) -> bool:
return any(c.__name__ == class_name_where_defined for c in states)

def is_not_mixin(state: Type[BaseState]) -> bool:
return not state._mixin

def length_of_state(state: Type[BaseState]) -> int:
return len(inspect.getmro(state))

class_where_defined = cast(
Type[BaseState],
classes_where_defined[0] if classes_where_defined else owner,
min(
filter(
is_not_mixin,
filter(
lambda state: contains_class_name(inspect.getmro(state)),
inspect.getmro(owner),
),
),
default=owner,
key=length_of_state,
),
)

return self._replace(
_var_name=format_state_name(class_where_defined.get_full_name())
+ "."
Expand Down

0 comments on commit 397683a

Please sign in to comment.