fix: Determine var type from value. (#4143)

This commit is contained in:
abulvenz 2024-10-10 00:33:34 +00:00 committed by GitHub
parent 7a971e5842
commit 87648af3ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -545,7 +545,7 @@ class LiteralStringVar(LiteralVar, StringVar):
def create(
cls,
value: str,
_var_type: GenericType | None = str,
_var_type: GenericType | None = None,
_var_data: VarData | None = None,
) -> StringVar:
"""Create a var from a string value.
@ -558,6 +558,9 @@ class LiteralStringVar(LiteralVar, StringVar):
Returns:
The var.
"""
# Determine var type in case the value is inherited from str.
_var_type = _var_type or type(value) or str
if REFLEX_VAR_OPENING_TAG in value:
strings_and_vals: list[Var | str] = []
offset = 0

View File

@ -1809,3 +1809,6 @@ def test_to_string_operation():
assert cast(Var, TestState.email)._var_type == Email
assert cast(Var, TestState.optional_email)._var_type == Optional[Email]
single_var = Var.create(Email())
assert single_var._var_type == Email