improve datatable hacks, also default to ObjectVar for Unions
This commit is contained in:
parent
23e7ef5444
commit
0812edd3a2
@ -116,14 +116,8 @@ class DataTable(Gridjs):
|
|||||||
if isinstance(self.data, ImmutableVar) and types.is_dataframe(
|
if isinstance(self.data, ImmutableVar) and types.is_dataframe(
|
||||||
self.data._var_type
|
self.data._var_type
|
||||||
):
|
):
|
||||||
self.columns = self.data._replace(
|
self.columns = self.data.columns
|
||||||
_var_name=f"{self.data._var_name}.columns",
|
self.data = self.data.data
|
||||||
_var_type=List[Any],
|
|
||||||
)
|
|
||||||
self.data = self.data._replace(
|
|
||||||
_var_name=f"{self.data._var_name}.data",
|
|
||||||
_var_type=List[List[Any]],
|
|
||||||
)
|
|
||||||
if types.is_dataframe(type(self.data)):
|
if types.is_dataframe(type(self.data)):
|
||||||
# If given a pandas df break up the data and columns
|
# If given a pandas df break up the data and columns
|
||||||
data = serialize(self.data)
|
data = serialize(self.data)
|
||||||
|
@ -482,7 +482,7 @@ class ImmutableVar(Var, Generic[VAR_TYPE]):
|
|||||||
if all(inspect.isclass(t) and issubclass(t, Base) for t in inner_types):
|
if all(inspect.isclass(t) and issubclass(t, Base) for t in inner_types):
|
||||||
return self.to(ObjectVar, self._var_type)
|
return self.to(ObjectVar, self._var_type)
|
||||||
|
|
||||||
return self
|
return self.to(ObjectVar, self._var_type)
|
||||||
|
|
||||||
if not inspect.isclass(fixed_type):
|
if not inspect.isclass(fixed_type):
|
||||||
raise TypeError(f"Unsupported type {var_type} for guess_type.")
|
raise TypeError(f"Unsupported type {var_type} for guess_type.")
|
||||||
|
@ -338,6 +338,12 @@ def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None
|
|||||||
return hints[name]
|
return hints[name]
|
||||||
except exceptions as e:
|
except exceptions as e:
|
||||||
console.warn(f"Failed to resolve ForwardRefs for {cls}.{name} due to {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
|
pass
|
||||||
return None # Attribute is not accessible.
|
return None # Attribute is not accessible.
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ from reflex.state import BaseState
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def data_table_state(request):
|
def data_table_state(request: pytest.FixtureRequest):
|
||||||
"""Get a data table state.
|
"""Get a data table state.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -23,7 +23,7 @@ from reflex.utils.serializers import serialize, serialize_dataframe
|
|||||||
],
|
],
|
||||||
indirect=["data_table_state"],
|
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.
|
"""Test the str/render function.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -40,13 +40,13 @@ def test_validate_data_table(data_table_state: rx.State, expected):
|
|||||||
|
|
||||||
data_table_dict = data_table_component.render()
|
data_table_dict = data_table_component.render()
|
||||||
|
|
||||||
# prefix expected with state name
|
var = data_table_state
|
||||||
state_name = data_table_state.get_name()
|
if expected:
|
||||||
expected = f"{state_name}.{expected}" if expected else state_name
|
var = getattr(var, expected)
|
||||||
|
|
||||||
assert data_table_dict["props"] == [
|
assert data_table_dict["props"] == [
|
||||||
f"columns={{{expected}.columns}}",
|
f"columns={{{var.columns._var_name}}}",
|
||||||
f"data={{{expected}.data}}",
|
f"data={{{var.data._var_name}}}",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user