-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REF-3225] implement __format__ for immutable vars #3617
[REF-3225] implement __format__ for immutable vars #3617
Conversation
adhami3310
commented
Jul 3, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code looks good, going to test it now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the ImmutableVar
cannot be created from an f-string with vars inside of it
import reflex as rx
class S(rx.State):
foo: str = "foo"
# Create an new style Var with data.
iv = rx._x.vars.ImmutableVar.create("foo", _var_data=S.foo._var_data)
# Create an f-string using the new style Var.
f_s = f"{iv}bar"
# Create an old style Var from the f-string.
n_v = rx.Var.create(f_s)
print(n_v, repr(n_v))
# Create a new style Var from the f-string.
n_iv = rx._x.vars.ImmutableVar.create(f_s)
print(n_iv, repr(n_iv))
It seems there is a bit of code in rx.Var.__post_init__
which is trying to assign to self._var_name
and self._var_data
, but this does not work for immutable instances.
Instead we can change the code to call __init__
again with the updated values, and this should be fine in nearly every case. It sort of breaks immutability, but if we consider __post_init__
to be just an extension of __init__
, then it makes sense.
diff --git a/reflex/vars.py b/reflex/vars.py
index ad7c223a..f55aa025 100644
--- a/reflex/vars.py
+++ b/reflex/vars.py
@@ -502,8 +502,10 @@ class Var:
# Decode any inline Var markup and apply it to the instance
_var_data, _var_name = _decode_var(self._var_name)
if _var_data:
- self._var_name = _var_name
- self._var_data = VarData.merge(self._var_data, _var_data)
+ self.__init__(
+ _var_name=_var_name,
+ _var_data=VarData.merge(self._var_data, _var_data),
+ )
def _replace(self, merge_var_data=None, **kwargs: Any) -> BaseVar:
"""Make a copy of this Var with updated fields.
changed post_init behavior in a different file
* implement format for immutable vars * add some basic test * make reference only after formatting * win over pyright * hopefully now pyright doesn't hate me * forgot some _var_data * i don't know how imports work * use f_string var and remove assignments from pyi file * override post_init to not break immutability * add create_safe and test for it