Skip to content

Commit d8cb464

Browse files
masenfclaudeFarhanAliRaza
authored
fix: remove dead serialize_dict call in format_prop (#7587)
* fix: remove dead serialize_dict call in format_prop format_prop's dict branch called serializers.serialize_dict, which no longer exists, so a plain dict raised AttributeError instead of being formatted. Every framework caller passes a Var or EventChain and returns before reaching it, so the branch was dead internally. Let dicts fall through to json_dumps like every other literal, which encodes nested values through the registered serializers. Closes ENG-10109. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VmkRjNPQfbihbxXpYNqXh6 * refactor: drop unreachable checks in format_prop json_dumps always returns a str, so the ValueError guard and the InvalidStylePropError re-raise can never fire. Narrow the return type to str and cover a serializer-encoded nested value in the raw dict test. --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Farhan <www.mfarvirus@gmail.com> Co-authored-by: Farhan Ali Raza <62690310+FarhanAliRaza@users.noreply.github.com>
1 parent f51f3d3 commit d8cb464

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Passing a plain `dict` to `format_prop` no longer raises `AttributeError`; the dict is JSON-encoded like any other literal.

‎packages/reflex-base/src/reflex_base/utils/format.py‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from rich.markup import escape as escape_markup
1414

1515
from reflex_base import constants
16-
from reflex_base.utils import exceptions
1716
from reflex_base.utils.types import is_non_string_sequence
1817

1918
if TYPE_CHECKING:
@@ -369,7 +368,7 @@ def format_match(
369368

370369
def format_prop(
371370
prop: Var | EventChain | ComponentStyle | str,
372-
) -> int | float | str:
371+
) -> str:
373372
"""Format a prop.
374373
375374
Args:
@@ -379,13 +378,10 @@ def format_prop(
379378
The formatted prop to display within a tag.
380379
381380
Raises:
382-
exceptions.InvalidStylePropError: If the style prop value is not a valid type.
383381
TypeError: If the prop is not valid.
384-
ValueError: If the prop is not a string.
385382
"""
386383
# import here to avoid circular import.
387384
from reflex_base.event import EventChain
388-
from reflex_base.utils import serializers
389385
from reflex_base.vars import Var
390386

391387
try:
@@ -403,23 +399,13 @@ def format_prop(
403399
return prop
404400
return json_dumps(prop)
405401

406-
# For dictionaries, convert any properties to strings.
407-
if isinstance(prop, dict):
408-
prop = serializers.serialize_dict(prop) # pyright: ignore [reportAttributeAccessIssue]
409-
410-
else:
411-
# Dump the prop as JSON.
412-
prop = json_dumps(prop)
413-
except exceptions.InvalidStylePropError:
414-
raise
402+
# Dump the prop as JSON.
403+
prop = json_dumps(prop)
415404
except TypeError as e:
416405
msg = f"Could not format prop: {prop} of type {type(prop)}"
417406
raise TypeError(msg) from e
418407

419408
# Wrap the variable in braces.
420-
if not isinstance(prop, str):
421-
msg = f"Invalid prop: {prop}. Expected a string."
422-
raise ValueError(msg)
423409
return wrap(prop, "{", check_first=False)
424410

425411

‎tests/units/utils/test_format.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,25 @@ def test_format_prop(prop: Var, formatted: str):
578578
assert format.format_prop(LiteralVar.create(prop)) == formatted
579579

580580

581+
@pytest.mark.parametrize(
582+
("prop", "formatted"),
583+
[
584+
({"a": 1}, '{{"a": 1}}'),
585+
({"nested": {"b": [1, 2]}}, '{{"nested": {"b": [1, 2]}}}'),
586+
({}, "{{}}"),
587+
({"d": datetime.date(2026, 1, 2)}, '{{"d": "2026-01-02"}}'),
588+
],
589+
)
590+
def test_format_prop_raw_dict(prop: dict, formatted: str):
591+
"""Test that a raw dict prop is JSON-dumped and wrapped in braces.
592+
593+
Args:
594+
prop: The dict prop to test.
595+
formatted: The expected formatted value.
596+
"""
597+
assert format.format_prop(prop) == formatted
598+
599+
581600
@pytest.mark.parametrize(
582601
("single_props", "key_value_props", "output"),
583602
[

0 commit comments

Comments
 (0)