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

Raise AttributeError when attempting to set value on an rx obj #1022

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions param/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,11 @@ def _eval_operation(self, obj, operation):
obj = fn(obj, *resolved_args, **resolved_kwargs)
return obj

def __setattr__(self, name, value):
if name == "value":
Copy link
Member

Choose a reason for hiding this comment

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

Why only value?

And what if the underlying object itself has a value attribute?

Copy link
Member Author

Choose a reason for hiding this comment

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

And what if the underlying object itself has a value attribute?

I don't think this has any consequence? Currently, for an object that has a value attribute, rx(obj).value returns a reactive expression. Setting rx(obj).value sets value on the reactive expression, not on the underlying object, which can be done with rx(obj).rx.value = <value>. Let me know if I got this wrong.

Why only value?

Because that's the most error-prone attribute in my opinion and I don't want to restrict __setattr__ too much beyond that.

Copy link
Member

Choose a reason for hiding this comment

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

Because that's the most error-prone attribute in my opinion and I don't want to restrict setattr too much beyond that.

A bit opinionated, but I think I agree. Can you add a comment in the code about this?

raise AttributeError("'rx' has no attribute 'value', try '<reactive_expr>.rx.value = <val>'.")
super().__setattr__(name, value)


def _rx_transform(obj):
if not isinstance(obj, rx):
Expand Down
5 changes: 5 additions & 0 deletions tests/testreactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,8 @@ def test_reactive_callback_resolve_accessor():
dfx = rx(df)
out = dfx["name"].str._callback()
assert out is df["name"].str

def test_reactive_set_value_attributeerror():
x = rx(1)
with pytest.raises(AttributeError, match="'rx' has no attribute 'value'"):
x.value = 1
Loading