Skip to content

Update StringVar to match Python str behavior (#5417)#6596

Open
ubaidrmn wants to merge 4 commits into
reflex-dev:mainfrom
ubaidrmn:fix-5417
Open

Update StringVar to match Python str behavior (#5417)#6596
ubaidrmn wants to merge 4 commits into
reflex-dev:mainfrom
ubaidrmn:fix-5417

Conversation

@ubaidrmn

@ubaidrmn ubaidrmn commented Jun 2, 2026

Copy link
Copy Markdown

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. Closes #5417.

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

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`.
@ubaidrmn ubaidrmn requested a review from a team as a code owner June 2, 2026 13:01
@greptile-apps

greptile-apps Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds lstrip and rstrip methods to StringVar and updates the existing strip method to accept an optional chars argument, aligning StringVar's string API with Python's built-in str.

  • state.js gains three new helpers (pyLstrip, pyRstrip, pyStrip) that use new Set(chars) for character matching and correctly handle multi-code-unit (surrogate-pair) emoji in both forward and backward traversal. When chars is null/undefined they delegate to the native trimStart/trimEnd/trim.
  • sequence.py integrates the helpers through the @var_operation decorator; Python None is converted to JS null via LiteralVar.create(None), which the JS helpers test with chars == null before branching.
  • Unit and integration tests cover all three methods for the no-arg (whitespace) case, a literal chars string, and a StringVar state variable as chars.

Confidence Score: 5/5

Safe to merge — the change is additive, the JS helpers are correct for well-formed strings, and existing behaviour of the no-arg strip path is preserved.

All three strip variants correctly fall back to native trim* methods when no chars are supplied, and use a Set-based character lookup (not a regex) for the chars path. Surrogate-pair handling in the backward-scan of pyRstrip uses codePointAt(end-2), which correctly identifies two-code-unit code points for well-formed strings. The @var_operation decorator's LiteralVar.create(None) → null serialisation is confirmed by the updated unit tests.

No files require special attention.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/.templates/web/utils/state.js Adds pyLstrip, pyRstrip, pyStrip JS helpers. Uses Set-based character matching (not regex), correct Unicode/surrogate-pair handling in both directions.
packages/reflex-base/src/reflex_base/vars/sequence.py Adds lstrip/rstrip methods to StringVar and updates strip to accept an optional chars arg; delegates to new JS helpers via @var_operation with proper LiteralVar conversion of None → null.
tests/units/test_var.py Adds unit assertions for lstrip/rstrip/strip covering both zero-arg (null) and literal-chars paths, plus a StringVar-as-chars path.
tests/integration/test_var_operations.py Integration tests for all three strip variants with no-arg whitespace, literal chars, and state-var chars; expected values match Python semantics.
packages/reflex-base/news/5417.feature.md Changelog entry for the new lstrip/rstrip methods and updated strip signature.

Reviews (2): Last reviewed commit: "Drop the NoneVar fast path; helpers hand..." | Re-trigger Greptile

Comment thread packages/reflex-base/src/reflex_base/vars/sequence.py
Comment thread packages/reflex-base/src/reflex_base/vars/sequence.py
@FarhanAliRaza

Copy link
Copy Markdown
Contributor

I think the greptile raises some valid points.

@ubaidrmn

ubaidrmn commented Jun 2, 2026

Copy link
Copy Markdown
Author

I think the greptile raises some valid points.

Hey @FarhanAliRaza, I agree. The escaping issue should be a straightforward fix. I’m working on a solution for the first point. New to the codebase, so still exploring.

@codspeed-hq

codspeed-hq Bot commented Jun 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing ubaidrmn:fix-5417 (e223f21) with main (9972800)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

masenf and others added 3 commits June 11, 2026 13:54
…rals

Constructing JS regex character classes from Python values was broken:
quoted literal serialization leaked into the class, var expressions were
embedded verbatim into static regex syntax, and regex-special characters
(], \, ^, -) were not escaped.

Replace with pyStrip/pyLstrip/pyRstrip helpers in state.js that match
Python str semantics (code-point aware, null chars strips whitespace),
and detect the no-args case with isinstance(chars, NoneVar) rather than
comparing against the string "null".

Add end-to-end integration tests covering whitespace and chars-set
stripping, including regex-special characters and state var chars.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pyStrip/pyLstrip/pyRstrip already fall back to trim/trimStart/trimEnd
when chars is null, so always emit the helper call.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StringVar.strip() takes 1 positional argument but 2 were given

3 participants