Override _var_is_string when handling str literals (#3473)

* Override _var_is_string when handling str literals

Maintain the pre 0.5.4 behavior when dealing with string literal
props, without displaying the deprecation warning.

Wait, isn't it weird to pass `_var_is_string=False` when the type is actually a str??

Yes, yes it is. However this is currently needed to avoid raising the
DeprecationWarning internally. These str-type vars with
_var_is_string set to false are handled by
`reflex.utils.format.format_prop`, but setting them to be
_var_is_string=True causes them to get quoted twice, which is not
what we want.

Var operations refactor will take care of cleaning this up, but for
now, we will go with the hack.

* Ignore type checks

Since I'm using an `isinstance` check now, the type checker thinks that `value`
could possibly be a string (instead of Any), which raises typing errors that
have to be ignored (they were ignored before implicitly due to being Any-typed)
This commit is contained in:
Masen Furer 2024-06-11 13:18:34 -07:00 committed by GitHub
parent 66f0a49b75
commit bd799a2680
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -352,7 +352,7 @@ class Component(BaseComponent, ABC):
**{
prop: Var.create(
kwargs[prop],
_var_is_string=False,
_var_is_string=False if isinstance(kwargs[prop], str) else None,
)
for prop in self.get_initial_props()
if prop in kwargs
@ -400,7 +400,10 @@ class Component(BaseComponent, ABC):
passed_types = None
try:
# Try to create a var from the value.
kwargs[key] = Var.create(value, _var_is_string=False)
kwargs[key] = Var.create(
value,
_var_is_string=False if isinstance(value, str) else None,
)
# Check that the var type is not None.
if kwargs[key] is None:
@ -425,7 +428,9 @@ class Component(BaseComponent, ABC):
if types.is_union(passed_type):
# We need to check all possible types in the union.
passed_types = (
arg for arg in passed_type.__args__ if arg is not type(None)
arg
for arg in passed_type.__args__ # type: ignore
if arg is not type(None)
)
if (
# If the passed var is a union, check if all possible types are valid.
@ -447,7 +452,8 @@ class Component(BaseComponent, ABC):
if key in component_specific_triggers:
# Temporarily disable full control for event triggers.
kwargs["event_triggers"][key] = self._create_event_chain(
value=value, args_spec=component_specific_triggers[key]
value=value, # type: ignore
args_spec=component_specific_triggers[key],
)
# Remove any keys that were added as events.