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

Get attribute access type fix #3803

Merged
Merged
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
7 changes: 6 additions & 1 deletion reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,14 @@ 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

if (
hasattr(cls, "__fields__")
and name in cls.__fields__
Expand Down
46 changes: 46 additions & 0 deletions tests/test_attribute_access_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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=[
Expand Down Expand Up @@ -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:
Expand Down
Loading