Skip to content

Commit 2e2a9ec

Browse files
committed
fix(component): compare field values in BaseComponent.__eq__
bool() on a generator is always True, so any two same-class components compared equal. Use all() to actually evaluate the per-field comparisons.
1 parent 941f9bb commit 2e2a9ec

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

packages/reflex-base/src/reflex_base/components/component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def __eq__(self, value: Any) -> bool:
346346
Returns:
347347
Whether the component is equal to the value.
348348
"""
349-
return type(self) is type(value) and bool(
349+
return type(self) is type(value) and all(
350350
getattr(self, key) == getattr(value, key) for key in self.get_fields()
351351
)
352352

tests/units/components/test_component.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,3 +2301,36 @@ def test_ref():
23012301
assert id_component._render().props["ref"].equals(Var("ref_custom_id"))
23022302

23032303
assert "ref" not in rx.box()._render().props
2304+
2305+
2306+
def test_component_equality_compares_fields():
2307+
"""``BaseComponent.__eq__`` must compare field values, not just class identity.
2308+
2309+
HTML ``Element`` subclasses (``rx.box`` etc.) override ``__eq__`` to compare by
2310+
tag only, so this test uses a plain ``Component`` subclass to exercise the
2311+
base implementation.
2312+
"""
2313+
2314+
class EqProbe(Component):
2315+
tag = "EqProbe"
2316+
label: str = ""
2317+
2318+
class OtherProbe(Component):
2319+
tag = "OtherProbe"
2320+
label: str = ""
2321+
2322+
a = EqProbe.create(label="x")
2323+
b = EqProbe.create(label="x")
2324+
c = EqProbe.create(label="y")
2325+
2326+
assert a == b
2327+
assert a != c
2328+
2329+
parent_same = EqProbe.create(EqProbe.create(label="leaf"), label="root")
2330+
parent_other = EqProbe.create(EqProbe.create(label="leaf"), label="root")
2331+
parent_diff = EqProbe.create(EqProbe.create(label="other"), label="root")
2332+
assert parent_same == parent_other
2333+
assert parent_same != parent_diff
2334+
2335+
assert EqProbe.create() != OtherProbe.create()
2336+
assert EqProbe.create() != "not a component"

0 commit comments

Comments
 (0)