Skip to content

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

Open
darshjoshi wants to merge 4 commits into
reflex-dev:mainfrom
darshjoshi:fix-negative-exponent-base
Open

darshjoshi wants to merge 4 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 outstanding findings remain.

Summary

Fixes JavaScript builds for powers with negative or negated bases.

  • Powers with negative bases now compile as JavaScript.

Reviews (4) · Last reviewed commit: "Keep test_var.py as it is on the base br..." · 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

⚠️ 15 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 148 untouched benchmarks


Comparing darshjoshi:fix-negative-exponent-base (2150318) with main (5ae5dcd)

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.

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/units/test_var.py">

<violation number="1" location="tests/units/test_var.py:1593">
P2: This test expects `code_uses_state_contexts` to recognize `useStateContext`, but the helper only recognizes `useContext(StateContexts...)`; the unit suite will fail. Use a representative `useContext(StateContexts.state)` snippet instead.</violation>
</file>

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 Outdated
@pytest.mark.parametrize(
("javascript_code", "expected"),
[
("const state = useStateContext('state')", True),

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.

P2: This test expects code_uses_state_contexts to recognize useStateContext, but the helper only recognizes useContext(StateContexts...); the unit suite will fail. Use a representative useContext(StateContexts.state) snippet instead.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/units/test_var.py, line 1593:

<comment>This test expects `code_uses_state_contexts` to recognize `useStateContext`, but the helper only recognizes `useContext(StateContexts...)`; the unit suite will fail. Use a representative `useContext(StateContexts.state)` snippet instead.</comment>

<file context>
@@ -1598,6 +1587,17 @@ def test_retrieval():
+@pytest.mark.parametrize(
+    ("javascript_code", "expected"),
+    [
+        ("const state = useStateContext('state')", True),
+        ("const state = useContext(ThemeContext)", False),
+    ],
</file context>
Suggested change
("const state = useStateContext('state')", True),
("const state = useContext(StateContexts.state)", True),

Comment thread tests/units/test_var.py Outdated
Comment thread tests/units/test_var.py Outdated
Comment thread tests/units/test_var.py Outdated

This branch has not been deployed

No deployments
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.

1 participant