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

ObjectVar __getitem__ typing issues #4380

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions tests/units/vars/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,34 @@ def serialize_bare(obj: Bare) -> dict:
return {"quantity": obj.quantity}


class Tag(rx.Base):
"""A Tag for testing."""

pass


class Base(rx.Base):
"""A reflex base class with a single attribute."""

quantity: int = 0
collection: list[Tag] = []


class ObjectState(rx.State):
"""A reflex state with bare and base objects."""

bare: rx.Field[Bare] = rx.field(Bare())
base: rx.Field[Base] = rx.field(Base())
d: rx.Field[dict[str, str]] = rx.field({})
d_bool: rx.Field[dict[str, bool]] = rx.field({})

@rx.var
def computed_d(self) -> dict[str, str]:
return self.d

@rx.var
def computed_d_bool(self) -> dict[str, bool]:
return self.d_bool


@pytest.mark.parametrize("type_", [Base, Bare])
Expand Down Expand Up @@ -100,3 +117,10 @@ def test_typing() -> None:
# Base
var = ObjectState.base
_ = assert_type(var, ObjectVar[Base])

# Collection
ObjectState.d["key"]
ObjectState.computed_d["key"]
ObjectState.d_bool["key"]
ObjectState.computed_d_bool["key"]
ObjectState.base.collection[0]
Loading