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

fallback to ObjectVar in guess_type #3911

Closed
Show file tree
Hide file tree
Changes from 4 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
10 changes: 2 additions & 8 deletions reflex/components/gridjs/datatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,8 @@ def add_imports(self) -> ImportDict:

def _render(self) -> Tag:
if isinstance(self.data, Var) and types.is_dataframe(self.data._var_type):
self.columns = self.data._replace(
_js_expr=f"{self.data._js_expr}.columns",
_var_type=List[Any],
)
self.data = self.data._replace(
_js_expr=f"{self.data._js_expr}.data",
_var_type=List[List[Any]],
)
self.columns = self.data.columns
self.data = self.data.data
if types.is_dataframe(type(self.data)):
# If given a pandas df break up the data and columns
data = serialize(self.data)
Expand Down
6 changes: 6 additions & 0 deletions reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None
return hints[name]
except exceptions as e:
console.warn(f"Failed to resolve ForwardRefs for {cls}.{name} due to {e}")
# hardcoded fallbacks for forward ref issues
if is_dataframe(cls):
if name == "columns":
return List[Any]
if name == "data":
return List[List[Any]]
pass
return None # Attribute is not accessible.

Expand Down
9 changes: 1 addition & 8 deletions reflex/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,7 @@ def guess_type(self) -> Var:
):
return self.to(NumberVar, self._var_type)

if all(
inspect.isclass(t)
and (issubclass(t, Base) or dataclasses.is_dataclass(t))
for t in inner_types
):
return self.to(ObjectVar, self._var_type)

return self
return self.to(ObjectVar, self._var_type)

if not inspect.isclass(fixed_type):
raise TypeError(f"Unsupported type {var_type} for guess_type.")
Expand Down
2 changes: 1 addition & 1 deletion tests/components/datadisplay/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@pytest.fixture
def data_table_state(request):
def data_table_state(request: pytest.FixtureRequest):
"""Get a data table state.

Args:
Expand Down
12 changes: 6 additions & 6 deletions tests/components/datadisplay/test_datatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
indirect=["data_table_state"],
)
def test_validate_data_table(data_table_state: rx.State, expected):
def test_validate_data_table(data_table_state: rx.State, expected: str) -> None:
"""Test the str/render function.

Args:
Expand All @@ -40,13 +40,13 @@ def test_validate_data_table(data_table_state: rx.State, expected):

data_table_dict = data_table_component.render()

# prefix expected with state name
state_name = data_table_state.get_name()
expected = f"{state_name}.{expected}" if expected else state_name
var = data_table_state
if expected:
var = getattr(var, expected)

assert data_table_dict["props"] == [
f"columns={{{expected}.columns}}",
f"data={{{expected}.data}}",
f"columns={{{var.columns._var_name}}}",
f"data={{{var.data._var_name}}}",
benedikt-bartscher marked this conversation as resolved.
Show resolved Hide resolved
]


Expand Down
Loading