You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[REF-3014] Bug in serializing lists/dict of rx.Base #3438
we need to unwrap vars inside lists and dicts when they are real JS values (like functions), but for things that serialize to dictionaries, the unwrapping is taking away the outer curly braces where it shouldn't.
yeah, so you need to know either the format of a var string so you can more precisely target it, with the regex.
Or you need to somehow leverage the recursion that json.dumps creates, and maybe detect when you are serializing a var, so you can take away the curly braces at that point. I would prefer that second option, because it follows the semantics of what you are trying to serialize. Is there specific serialization logic for vars?
The problem with touching anything in this formatting code is that it's going to have fallout for other parts of the framework that have been working fine. Any changes must be very carefully considered to limit the scope of the change to the bug in question without introducing other regressions.
The entire Var system is begging for a refactor because it's too hard to special case serialization for different types of vars. I was just running into this myself while trying to get nested dict serialization to work.
Whenever I am in doubt whether it is time for a refactor it is usually means I should have refactored 2 weeks ago. Refactoring is often considered terrifying. The fear usually stems from the following reasoning: "Before I can refactor I must understand how this code works". But these days I take a different perspective: Refactoring is restructuring code that leads to understanding. I start by making small simplifications (that do not change behavior) and then work my way up, never making any big change. I also see refactoring as fundamentally different from solving bugs.
But... to work towards a solution: wouldn't removing the part of the regex while adding the following work?
@serializerdefserialize_var(var: rx.Var) ->str:
# solve the problem here# You can make more fine grained serializers if you wantreturnformat_var(var) # maybe not even format?
modified the milestones: This milestone has been deleted, This milestone has been deleted, This milestone has been deleted, This milestone has been deletedon Jun 30, 2024
Activity
[-]Bug in serializing lists of `rx.Base` [/-][+][REF-3014] Bug in serializing lists of `rx.Base` [/+]guidocalvano commentedon Jun 5, 2024
I suspect
For example, "{var}" will be unwrapped to "var".
in unwrap_vars,
specifcally this:
{(.*?)} # extract the value between curly braces (non-greedy)
from the regex
and if I remove format.unwrap_vars from the serialize_list callback I get this:
{"key": "1", "value": "text1"}
["{"key": "1", "value": "text1"}"]
so that is a smoking gun...
removing this stops the curly braces from disappearing
{(.*?)} # extract the value between curly braces (non-greedy)
masenf commentedon Jun 5, 2024
we need to unwrap vars inside lists and dicts when they are real JS values (like functions), but for things that serialize to dictionaries, the unwrapping is taking away the outer curly braces where it shouldn't.
guidocalvano commentedon Jun 5, 2024
yeah, so you need to know either the format of a var string so you can more precisely target it, with the regex.
Or you need to somehow leverage the recursion that json.dumps creates, and maybe detect when you are serializing a var, so you can take away the curly braces at that point. I would prefer that second option, because it follows the semantics of what you are trying to serialize. Is there specific serialization logic for vars?
guidocalvano commentedon Jun 5, 2024
Maybe something like this in the super class of all vars? https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable
masenf commentedon Jun 5, 2024
vars are serialized up front using reflex.utils.serializers and related functions... the
_var_nameis already the serialized formmasenf commentedon Jun 5, 2024
The problem with touching anything in this formatting code is that it's going to have fallout for other parts of the framework that have been working fine. Any changes must be very carefully considered to limit the scope of the change to the bug in question without introducing other regressions.
The entire Var system is begging for a refactor because it's too hard to special case serialization for different types of vars. I was just running into this myself while trying to get nested
dictserialization to work.guidocalvano commentedon Jun 5, 2024
Whenever I am in doubt whether it is time for a refactor it is usually means I should have refactored 2 weeks ago. Refactoring is often considered terrifying. The fear usually stems from the following reasoning: "Before I can refactor I must understand how this code works". But these days I take a different perspective: Refactoring is restructuring code that leads to understanding. I start by making small simplifications (that do not change behavior) and then work my way up, never making any big change. I also see refactoring as fundamentally different from solving bugs.
guidocalvano commentedon Jun 5, 2024
But... to work towards a solution: wouldn't removing the part of the regex while adding the following work?
guidocalvano commentedon Jun 5, 2024
The serialization code in the reflex code looks pretty organized....
guidocalvano commentedon Jun 5, 2024
What should a var look like in the output?
6 remaining items
adhami3310 commentedon Aug 26, 2024
The new immutable var system seems to handle this just fine.