Update StringVar to match Python str behavior (#5417)#6596
Conversation
Add missing `lstrip` and `rstrip` methods to align with Python's `str` interface. Update `strip` method to accept a `chars` argument, consistent with `str.strip`.
Greptile SummaryThis PR adds
Confidence Score: 3/5Safe to merge only for the whitespace-strip path; calling strip/lstrip/rstrip with a chars argument produces incorrect JavaScript and would silently give wrong results for users. The zero-argument whitespace-trimming path works correctly and the new method signatures are sound. However, every code path that goes through the regex branch (i.e., any call that passes a chars value) will generate broken JavaScript: string literals carry their surrounding quotes into the character class, and state-variable references are embedded statically into the regex literal rather than used as a dynamic value. These are silent runtime-logic failures that would be hard for app authors to debug. packages/reflex-base/src/reflex_base/vars/sequence.py β specifically the regex-construction branches in string_strip_operation, string_lstrip_operation, and string_rstrip_operation. Important Files Changed
|
| return var_operation_return( | ||
| js_expression=f"{string}.replace(/^[{chars}]+|[{chars}]+$/g, '')", | ||
| var_type=str, | ||
| ) |
There was a problem hiding this comment.
Quoted string literal injected directly into regex character class
When chars is a plain Python str (e.g., "abc"), the @var_operation wrapper converts it to LiteralVar.create("abc"), whose JavaScript serialization is "abc" (with quotes). Interpolating that into the regex literal produces /^["abc"]+.../ β the double-quote characters become literal members of the character class, so .strip("abc") would also strip " from the string.
For a StringVar state-variable, the JS variable expression (e.g., state.chars) is embedded verbatim inside the literal regex /.../, which is static syntax β it does not reference the variable at runtime and instead treats the characters of the expression name as the set to strip. The same problem exists identically in string_lstrip_operation and string_rstrip_operation.
| return var_operation_return( | ||
| js_expression=f"{string}.replace(/^[{chars}]+|[{chars}]+$/g, '')", | ||
| var_type=str, | ||
| ) |
There was a problem hiding this comment.
Regex special characters in
chars are not escaped
Characters such as ], \, ^, and - carry special meaning inside a regex character class. If chars contains any of them (e.g., strip("a-z") would make - a range operator), the generated JavaScript regex will silently behave differently from Python's character-set strip semantics. There is no escaping of these characters before they are placed between [ and ]. The same applies to string_lstrip_operation and string_rstrip_operation.
Add missing
lstripandrstripmethods to align with Python'sstrinterface. Updatestripmethod to accept acharsargument, consistent withstr.strip. Closes #5417.All Submissions:
Type of change
Please delete options that are not relevant.
New Feature Submission:
Changes To Core Features: