Skip to content

Commit 95631ff

Browse files
abulvenzmasenf
andauthored
Fix type propagation in ToStringOperation (#3895)
* fix: Adding type propagation to ToStringOperation. * fix: Better naming. * fix: Added test that fails without the fix. * Update reflex/ivars/base.py Co-authored-by: Masen Furer <m_github@0x26.net> * Retain mutability inside `async with self` block (#3884) When emitting a state update, restore `_self_mutable` to the value it had previously so that `yield` in the middle of `async with self` does not result in an immutable StateProxy. Fix #3869 * Include child imports in markdown component_map (#3883) If a component in the markdown component_map contains children components, use `_get_all_imports` to recursively enumerate them. Fix #3880 * [REF-3570] Remove deprecated REDIS_URL syntax (#3892) * fix: Instead of researching the type after dropping it, preserve it. * Apply suggestions from code review Co-authored-by: Masen Furer <m_github@0x26.net> --------- Co-authored-by: Masen Furer <m_github@0x26.net>
1 parent 0810bd8 commit 95631ff

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

reflex/ivars/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def to(
416416
return ToArrayOperation.create(self, var_type or list)
417417

418418
if issubclass(output, StringVar):
419-
return ToStringOperation.create(self)
419+
return ToStringOperation.create(self, var_type or str)
420420

421421
if issubclass(output, (ObjectVar, Base)):
422422
return ToObjectOperation.create(self, var_type or dict)
@@ -496,7 +496,7 @@ def guess_type(self) -> ImmutableVar:
496496
if issubclass(fixed_type, (list, tuple, set)):
497497
return self.to(ArrayVar, self._var_type)
498498
if issubclass(fixed_type, str):
499-
return self.to(StringVar)
499+
return self.to(StringVar, self._var_type)
500500
if issubclass(fixed_type, Base):
501501
return self.to(ObjectVar, self._var_type)
502502
return self

tests/test_var.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import math
33
import typing
4-
from typing import Dict, List, Set, Tuple, Union
4+
from typing import Dict, List, Optional, Set, Tuple, Union, cast
55

66
import pytest
77
from pandas import DataFrame
@@ -1756,3 +1756,21 @@ def test_invalid_computed_var_deps(deps: List):
17561756
)
17571757
def test_var(state) -> int:
17581758
return 1
1759+
1760+
1761+
def test_to_string_operation():
1762+
class Email(str): ...
1763+
1764+
class TestState(BaseState):
1765+
optional_email: Optional[Email] = None
1766+
email: Email = Email("test@reflex.dev")
1767+
1768+
assert (
1769+
str(TestState.optional_email) == f"{TestState.get_full_name()}.optional_email"
1770+
)
1771+
my_state = TestState()
1772+
assert my_state.optional_email is None
1773+
assert my_state.email == "test@reflex.dev"
1774+
1775+
assert cast(ImmutableVar, TestState.email)._var_type == Email
1776+
assert cast(ImmutableVar, TestState.optional_email)._var_type == Optional[Email]

0 commit comments

Comments
 (0)