From 4e4542004f945ffdf88a68d90d5dfb537c3d15ca Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Fri, 16 Aug 2024 17:03:48 +0200 Subject: [PATCH 1/2] add failing test --- reflex/utils/types.py | 1 + tests/test_attribute_access_type.py | 46 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/reflex/utils/types.py b/reflex/utils/types.py index 3bb5eae355..8fbe975c7e 100644 --- a/reflex/utils/types.py +++ b/reflex/utils/types.py @@ -225,6 +225,7 @@ def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None attr = getattr(cls, name, None) if hint := get_property_hint(attr): return hint + if ( hasattr(cls, "__fields__") and name in cls.__fields__ diff --git a/tests/test_attribute_access_type.py b/tests/test_attribute_access_type.py index 0813a5e62d..0d490ec1e2 100644 --- a/tests/test_attribute_access_type.py +++ b/tests/test_attribute_access_type.py @@ -94,6 +94,15 @@ def str_or_int_property(self) -> Union[str, int]: """ return self.name + @hybrid_property + def first_label(self) -> Optional[SQLALabel]: + """First label property. + + Returns: + First label + """ + return self.labels[0] if self.labels else None + class ModelClass(rx.Model): """Test reflex model.""" @@ -125,6 +134,15 @@ def str_or_int_property(self) -> Union[str, int]: """ return self.name + @property + def first_label(self) -> Optional[SQLALabel]: + """First label property. + + Returns: + First label + """ + return self.labels[0] if self.labels else None + class BaseClass(rx.Base): """Test rx.Base class.""" @@ -156,6 +174,15 @@ def str_or_int_property(self) -> Union[str, int]: """ return self.name + @property + def first_label(self) -> Optional[SQLALabel]: + """First label property. + + Returns: + First label + """ + return self.labels[0] if self.labels else None + class BareClass: """Bare python class.""" @@ -187,6 +214,15 @@ def str_or_int_property(self) -> Union[str, int]: """ return self.name + @property + def first_label(self) -> Optional[SQLALabel]: + """First label property. + + Returns: + First label + """ + return self.labels[0] if self.labels else None + @attrs.define class AttrClass: @@ -219,6 +255,15 @@ def str_or_int_property(self) -> Union[str, int]: """ return self.name + @property + def first_label(self) -> Optional[SQLALabel]: + """First label property. + + Returns: + First label + """ + return self.labels[0] if self.labels else None + @pytest.fixture( params=[ @@ -254,6 +299,7 @@ def cls(request: pytest.FixtureRequest) -> type: pytest.param("dict_str_str", Dict[str, str], id="Dict[str, str]"), pytest.param("str_property", str, id="str_property"), pytest.param("str_or_int_property", Union[str, int], id="str_or_int_property"), + pytest.param("first_label", Optional[SQLALabel], id="first_label"), ], ) def test_get_attribute_access_type(cls: type, attr: str, expected: GenericType) -> None: From d9b66b759db4a72cd6ddb9f00c8abf0e688fad39 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Fri, 16 Aug 2024 17:04:28 +0200 Subject: [PATCH 2/2] catch unhandled NotImplementedError --- reflex/utils/types.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/reflex/utils/types.py b/reflex/utils/types.py index 8fbe975c7e..c2b2070f10 100644 --- a/reflex/utils/types.py +++ b/reflex/utils/types.py @@ -222,7 +222,11 @@ def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None """ from reflex.model import Model - attr = getattr(cls, name, None) + try: + attr = getattr(cls, name, None) + except NotImplementedError: + attr = None + if hint := get_property_hint(attr): return hint