Skip to content
Merged
Changes from 4 commits
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
54 changes: 53 additions & 1 deletion reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import contextlib
import copy
import dataclasses
import enum
import functools
import inspect
import typing
Expand Down Expand Up @@ -479,6 +480,57 @@ def _components_from(
return ()


def _deterministic_hash(value: object) -> int:
"""Hash a rendered dictionary.

Args:
value: The dictionary to hash.

Returns:
The hash of the dictionary.

Raises:
TypeError: If the value is not hashable.
"""
if isinstance(value, BaseComponent):
# If the value is a component, hash its rendered code.
rendered_code = value.render()
return _deterministic_hash(rendered_code)
if isinstance(value, Var):
return _deterministic_hash((value._js_expr, value._get_all_var_data()))
if isinstance(value, VarData):
return _deterministic_hash(dataclasses.asdict(value))
if isinstance(value, dict):
# Sort the dictionary to ensure consistent hashing.
return _deterministic_hash(
tuple(sorted((k, _deterministic_hash(v)) for k, v in value.items()))
)
if isinstance(value, int):
# Hash numbers and booleans directly.
return int(value)
if isinstance(value, float):
return _deterministic_hash(str(value))
Comment on lines +511 to +512
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not hash the float directly, like the int?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_deterministic_hash has to return an int, so I'm not sure how to map it

if isinstance(value, str):
return int(md5(f'"{value}"'.encode()).hexdigest(), 16)
if isinstance(value, (tuple, list)):
# Hash tuples by hashing each element.
return _deterministic_hash(
"[" + ",".join(map(str, map(_deterministic_hash, value))) + "]"
)
if isinstance(value, enum.Enum):
# Hash enums by their name.
return _deterministic_hash(value.name)
Comment thread
adhami3310 marked this conversation as resolved.
Outdated
if value is None:
# Hash None as a special case.
return _deterministic_hash("None")

msg = (
f"Cannot hash value `{value}` of type `{type(value).__name__}`. "
"Only BaseComponent, Var, VarData, dict, str, tuple, and enum.Enum are supported."
)
raise TypeError(msg)


DEFAULT_TRIGGERS: Mapping[str, types.ArgsSpec | Sequence[types.ArgsSpec]] = {
EventTriggers.ON_FOCUS: no_args_event_spec,
EventTriggers.ON_BLUR: no_args_event_spec,
Expand Down Expand Up @@ -2430,7 +2482,7 @@ def _get_tag_name(cls, component: Component) -> str | None:
return None

# Compute the hash based on the rendered code.
code_hash = md5(str(rendered_code).encode("utf-8")).hexdigest()
code_hash = _deterministic_hash(rendered_code)

# Format the tag name including the hash.
return format.format_state_name(
Expand Down
Loading