Skip to content

fix: parenthesize a unary base in the compiled ** expression - #7565

Merged
FarhanAliRaza merged 6 commits into
reflex-dev:mainfrom
darshjoshi:fix-negative-exponent-base
Oct 22, 2026
Merged

FarhanAliRaza merged 6 commits into
reflex-dev:mainfrom
darshjoshi:fix-negative-exponent-base

Conversation

@darshjoshi

@darshjoshi darshjoshi commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Bug fix (non-breaking change which fixes an issue)

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?

Description

number_exponent_operation renders ({lhs} ** {rhs}) as is. JavaScript doesn't allow a unary expression as the base of ** (-2 ** 2 is a SyntaxError, unlike Python where it means -(2 ** 2)), so when the base is a negative literal or a negated var the compiled page doesn't build. For example (-State.velocity) ** 2 renders as (-(…velocity…) ** 2) and rx.Var.create(-2) ** State.n as (-2 ** …n…).

The base is now parenthesized when it starts with a unary operator, so those become ((-(…velocity…)) ** 2) and ((-2) ** …n…). Everything else renders exactly as before ((1 ** 2), (x ** -1), and -(x ** 2) from -x ** 2), so no existing snapshot changes.

Before / After with a one-page app, rx.text("squared: ", (-State.velocity) ** 2), running reflex export --frontend-only:

# before (main)
error: Build failed with 1 error:
[builtin:vite-transform] A unary expression with the '-' operator cannot
be used as the left operand of an exponentiation expression
    ╭─[ app_components/powapp/powapp.jsx:21:10 ]
 21 │ (-(reflex___state____state__powapp___powapp____state.velocity_rx_state_) ** 2)
    │ Help: Wrap the unary expression in parentheses to make the precedence explicit
exit=1

# after
Creating Production Build:  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 4/4
exit=0

I also ran the rendered expressions through Node and they match Python: ((-(x)) ** 2) is 9, ((-2) ** x) is -8, ((-2.5) ** 2) is 6.25 for x = 3.

test_exponent_parenthesizes_a_unary_base covers the negative-literal and negated-var bases and the cases that must stay unwrapped, and fails on main with '(-2 ** 3)' == '((-2) ** 3)'. tests/units passes (11965 passed), along with ruff and pyright on the changed files.

View guided diff

@darshjoshi
darshjoshi requested a review from a team as a code owner October 14, 2026 15:32

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issues were found.

Summary

Fixes JavaScript build errors when a negative number or negated variable is raised to a power.

  • Power expressions now group unary bases before exponentiation.

Reviews (6) · Last reviewed commit: "Merge branch 'main' into fix-negative-ex..." · Reviewed by Greptile

Comment thread packages/reflex-base/src/reflex_base/vars/number.py
Comment thread tests/units/test_var.py Outdated
@codspeed

codspeed Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 13 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 148 untouched benchmarks


Comparing darshjoshi:fix-negative-exponent-base (3e82720) with main (be0c685)

Open in CodSpeed

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

View guided diff | Re-trigger cubic

Comment thread tests/units/test_var.py
Comment thread tests/units/test_var.py
Comment thread tests/units/test_var.py
Comment thread tests/units/test_var.py
@masenf masenf added the on deck PRs lined up to review / merge next label Oct 22, 2026

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, appreciate the fix

@masenf
masenf enabled auto-merge (squash) October 22, 2026 00:19
@FarhanAliRaza
FarhanAliRaza merged commit 74bcd8b into reflex-dev:main Oct 22, 2026
169 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants