Skip to content

Commit

Permalink
solve pydantic issues
Browse files Browse the repository at this point in the history
  • Loading branch information
adhami3310 committed Jul 16, 2024
1 parent 0ae9f8f commit 7f03ea1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions reflex/experimental/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def create(
_var_type=type_,
_var_data=(
ImmutableVarData(
state=_var_data.state,
imports=_var_data.imports,
hooks=_var_data.hooks,
state=_var_data.state,
)
if _var_data
else None
Expand Down Expand Up @@ -364,7 +364,7 @@ def json_loads(s):
_var_value=value,
_var_name=f'"{value}"',
_var_type=str,
_var_data=_var_data,
_var_data=ImmutableVarData.merge(_var_data),
)


Expand All @@ -386,7 +386,7 @@ def __init__(self, _var_value: tuple[Var, ...], _var_data: VarData | None = None
_var_data: Additional hooks and imports associated with the Var.
"""
super(ConcatVarOperation, self).__init__(
_var_name="", _var_data=_var_data, _var_type=str
_var_name="", _var_data=ImmutableVarData.merge(_var_data), _var_type=str
)
object.__setattr__(self, "_var_value", _var_value)
object.__setattr__(self, "_var_name", self._cached_var_name)
Expand Down
6 changes: 4 additions & 2 deletions reflex/utils/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def collapse_imports(
The collapsed import dict.
"""
return {
lib: list(set(import_vars)) if isinstance(import_vars, list) else import_vars
lib: list(set(import_vars))
if isinstance(import_vars, list)
else list(import_vars)
for lib, import_vars in (
imports if isinstance(imports, tuple) else imports.items()
)
Expand Down Expand Up @@ -128,4 +130,4 @@ def __hash__(self) -> int:
ImportTypes = Union[str, ImportVar, List[Union[str, ImportVar]], List[ImportVar]]
ImportDict = Dict[str, ImportTypes]
ParsedImportDict = Dict[str, List[ImportVar]]
ImmutableParsedImportDict = Tuple[Tuple[str, Tuple[ImportVar]]]
ImmutableParsedImportDict = Tuple[Tuple[str, Tuple[ImportVar]], ...]
11 changes: 8 additions & 3 deletions reflex/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ def __init__(
imports: Imports needed to render this var.
hooks: Hooks that need to be present in the component to render this var.
"""
imports = tuple(
immutable_imports: ImmutableParsedImportDict = tuple(
sorted(
((k, tuple(sorted(v))) for k, v in parse_imports(imports or {}).items())
)
)
object.__setattr__(self, "state", state)
object.__setattr__(self, "imports", imports)
object.__setattr__(self, "imports", immutable_imports)
object.__setattr__(self, "hooks", tuple(hooks or {}))

@classmethod
Expand Down Expand Up @@ -335,7 +335,12 @@ def __eq__(self, other: Any) -> bool:
# not part of the vardata itself.
return (
self.state == other.state
and self.hooks.keys() == other.hooks.keys()
and self.hooks
== (
other.hooks
if isinstance(other, ImmutableVarData)
else tuple(other.hooks.keys())
)
and imports.collapse_imports(self.imports)
== imports.collapse_imports(other.imports)
)
Expand Down
6 changes: 6 additions & 0 deletions reflex/vars.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class ImmutableVarData:
state: str = ""
imports: ImmutableParsedImportDict = tuple()
hooks: Tuple[str, ...] = tuple()
def __init__(
self,
state: str = "",
imports: ImportDict | ParsedImportDict | None = None,
hooks: dict[str, None] | None = None,
) -> None: ...
@classmethod
def merge(
cls, *others: ImmutableVarData | VarData | None
Expand Down

0 comments on commit 7f03ea1

Please sign in to comment.