Просмотр исходного кода

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>
abulvenz 8 месяцев назад
Родитель
Сommit
95631ffdba
2 измененных файлов с 21 добавлено и 3 удалено
  1. 2 2
      reflex/ivars/base.py
  2. 19 1
      tests/test_var.py

+ 2 - 2
reflex/ivars/base.py

@@ -416,7 +416,7 @@ class ImmutableVar(Var, Generic[VAR_TYPE]):
             return ToArrayOperation.create(self, var_type or list)
             return ToArrayOperation.create(self, var_type or list)
 
 
         if issubclass(output, StringVar):
         if issubclass(output, StringVar):
-            return ToStringOperation.create(self)
+            return ToStringOperation.create(self, var_type or str)
 
 
         if issubclass(output, (ObjectVar, Base)):
         if issubclass(output, (ObjectVar, Base)):
             return ToObjectOperation.create(self, var_type or dict)
             return ToObjectOperation.create(self, var_type or dict)
@@ -496,7 +496,7 @@ class ImmutableVar(Var, Generic[VAR_TYPE]):
         if issubclass(fixed_type, (list, tuple, set)):
         if issubclass(fixed_type, (list, tuple, set)):
             return self.to(ArrayVar, self._var_type)
             return self.to(ArrayVar, self._var_type)
         if issubclass(fixed_type, str):
         if issubclass(fixed_type, str):
-            return self.to(StringVar)
+            return self.to(StringVar, self._var_type)
         if issubclass(fixed_type, Base):
         if issubclass(fixed_type, Base):
             return self.to(ObjectVar, self._var_type)
             return self.to(ObjectVar, self._var_type)
         return self
         return self

+ 19 - 1
tests/test_var.py

@@ -1,7 +1,7 @@
 import json
 import json
 import math
 import math
 import typing
 import typing
-from typing import Dict, List, Set, Tuple, Union
+from typing import Dict, List, Optional, Set, Tuple, Union, cast
 
 
 import pytest
 import pytest
 from pandas import DataFrame
 from pandas import DataFrame
@@ -1756,3 +1756,21 @@ def test_invalid_computed_var_deps(deps: List):
         )
         )
         def test_var(state) -> int:
         def test_var(state) -> int:
             return 1
             return 1
+
+
+def test_to_string_operation():
+    class Email(str): ...
+
+    class TestState(BaseState):
+        optional_email: Optional[Email] = None
+        email: Email = Email("test@reflex.dev")
+
+    assert (
+        str(TestState.optional_email) == f"{TestState.get_full_name()}.optional_email"
+    )
+    my_state = TestState()
+    assert my_state.optional_email is None
+    assert my_state.email == "test@reflex.dev"
+
+    assert cast(ImmutableVar, TestState.email)._var_type == Email
+    assert cast(ImmutableVar, TestState.optional_email)._var_type == Optional[Email]