Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make vardata merge not use classmethod #4245

Merged
merged 6 commits into from
Oct 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions reflex/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ def old_school_imports(self) -> ImportDict:
"""
return dict((k, list(v)) for k, v in self.imports)

@classmethod
def merge(cls, *others: VarData | None) -> VarData | None:
def merge(self, *others: VarData | None) -> VarData | None:
"""Merge multiple var data objects.

Args:
Expand All @@ -161,21 +160,24 @@ def merge(cls, *others: VarData | None) -> VarData | None:
Returns:
The merged var data object.
"""
state = ""
field_name = ""
_imports = {}
hooks = {}
for var_data in others:
if var_data is None:
continue
state = state or var_data.state
field_name = field_name or var_data.field_name
_imports = imports.merge_imports(_imports, var_data.imports)
hooks.update(
var_data.hooks
if isinstance(var_data.hooks, dict)
else {k: None for k in var_data.hooks}
)
all_var_datas = [self] + [
var_data for var_data in others if var_data is not None
]

field_name = next(
adhami3310 marked this conversation as resolved.
Show resolved Hide resolved
(var_data.field_name for var_data in all_var_datas if var_data.field_name),
"",
)

state = next(
(var_data.state for var_data in all_var_datas if var_data.state), ""
)

hooks = {hook: None for var_data in all_var_datas for hook in var_data.hooks}

_imports = imports.merge_imports(
*(var_data.imports for var_data in all_var_datas)
)

if state or _imports or hooks or field_name:
return VarData(
Expand All @@ -184,6 +186,7 @@ def merge(cls, *others: VarData | None) -> VarData | None:
imports=_imports,
hooks=hooks,
)

return None

def __bool__(self) -> bool:
Expand Down
Loading