From 6f1843dbb9095c7a46233a6b64a1315dbac37a13 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Thu, 14 Nov 2024 00:01:02 +0100 Subject: [PATCH 1/2] showcase ObjectVar __getitem__ typing issues --- tests/units/vars/test_object.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/units/vars/test_object.py b/tests/units/vars/test_object.py index efcb211660..b2025cd493 100644 --- a/tests/units/vars/test_object.py +++ b/tests/units/vars/test_object.py @@ -26,10 +26,17 @@ 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): @@ -100,3 +107,6 @@ def test_typing() -> None: # Base var = ObjectState.base _ = assert_type(var, ObjectVar[Base]) + + # Collection + ObjectState.base.collection[0] From 13845143f8d5d571700cb6125e78adf2b8b10193 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Thu, 14 Nov 2024 21:29:50 +0100 Subject: [PATCH 2/2] funny dict bool getitem --- tests/units/vars/test_object.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/units/vars/test_object.py b/tests/units/vars/test_object.py index b2025cd493..6b7b2f020d 100644 --- a/tests/units/vars/test_object.py +++ b/tests/units/vars/test_object.py @@ -44,6 +44,16 @@ class ObjectState(rx.State): 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]) @@ -109,4 +119,8 @@ def test_typing() -> None: _ = 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]