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

add Objectvar.get, Closes #4928 #4956

Merged
merged 5 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions reflex/vars/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,27 @@ def __getitem__(self, key: Var | Any) -> Var:
return self.__getattr__(key)
return ObjectItemOperation.create(self, key).guess_type()

def get(self, key: Var | Any, default: Var | Any | None = None) -> Var:
"""Get an item from the object.

Args:
key: The key to get from the object.
default: The default value if the key is not found.

Returns:
The item from the object.
"""
from reflex.components.core.cond import cond

if default is None:
default = Var.create(None)

return cond( # pyright: ignore[reportUnknownVariableType,reportCallIssue]
self.__getattr__(key), # pyright: ignore[reportArgumentType]
self.__getattr__(key), # pyright: ignore[reportArgumentType]
default,
)

# NoReturn is used here to catch when key value is Any
@overload
def __getattr__( # pyright: ignore [reportOverlappingOverload]
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/test_var_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def VarOperations():

class Object(rx.Base):
name: str = "hello"
optional_none: str | None = None
optional_str: str | None = "hello"

class Person(TypedDict):
name: str
Expand Down Expand Up @@ -47,6 +49,7 @@ class VarOperationState(rx.State):
people: rx.Field[list[Person]] = rx.field(
[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
)
obj: rx.Field[Object] = rx.field(Object())

app = rx.App(_state=rx.State)

Expand Down Expand Up @@ -713,6 +716,27 @@ def index():
rx.text.span(f"{rx.Var.create(13212312312.1231231):_.2f}"),
id="float_format_underscore_2f",
),
# ObjectVar
rx.box(
rx.text(VarOperationState.obj.name),
id="obj_name",
),
rx.box(
rx.text(VarOperationState.obj.optional_none),
id="obj_optional_none",
),
rx.box(
rx.text(VarOperationState.obj.optional_str),
id="obj_optional_str",
),
rx.box(
rx.text(VarOperationState.obj.get("optional_none")),
id="obj_optional_none_get_none",
),
rx.box(
rx.text(VarOperationState.obj.get("optional_none", "foo")),
id="obj_optional_none_get_foo",
),
)


Expand Down Expand Up @@ -936,6 +960,11 @@ def test_var_operations(driver, var_operations: AppHarness):
("float_format_underscore_0f", "13_212_312_312"),
("float_format_underscore_1f", "13_212_312_312.1"),
("float_format_underscore_2f", "13_212_312_312.12"),
("obj_name", "hello"),
("obj_optional_none", ""),
("obj_optional_str", "hello"),
("obj_optional_none_get_none", ""),
("obj_optional_none_get_foo", "foo"),
]

for tag, expected in tests:
Expand Down