Skip to content

fix(hosting-cli): make ScaleParams.as_json() side-effect free#6415

Merged
masenf merged 1 commit intoreflex-dev:mainfrom
BABTUNA:fix-hosting-cli-scaleparams-pure
May 1, 2026
Merged

fix(hosting-cli): make ScaleParams.as_json() side-effect free#6415
masenf merged 1 commit intoreflex-dev:mainfrom
BABTUNA:fix-hosting-cli-scaleparams-pure

Conversation

@BABTUNA
Copy link
Copy Markdown
Contributor

@BABTUNA BABTUNA commented Apr 29, 2026

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

ScaleParams.as_json() was mutating self.type when the type was unset:

  • previous behavior: if self.type is None: self.type = ScaleType.REGION

This made serialization stateful and introduced side effects for callers reusing the same ScaleParams object.

This patch makes serialization pure by using a local effective_type variable:

  • effective_type = self.type or ScaleType.REGION

No object fields are mutated during as_json().

Tests

Added regression test:

  • test_scale_params_as_json_is_pure_when_type_is_unspecified

The test verifies:

  • repeated as_json() calls are idempotent
  • scale_params.type remains None after serialization

Validation

  • uv run ruff check packages/reflex-hosting-cli/src/reflex_cli/utils/hosting.py tests/units/reflex_cli/utils/test_hosting.py
  • uv run pytest tests/units/reflex_cli/utils/test_hosting.py -q
  • uv run pytest tests/units/reflex_cli/v2/test_apps.py -k scale -q

Follow-up to #6369

@BABTUNA BABTUNA requested a review from a team as a code owner April 29, 2026 01:09
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 29, 2026

Greptile Summary

This PR fixes a side-effect in ScaleParams.as_json() that mutated self.type to ScaleType.REGION when it was None, making the method non-idempotent and surprising for callers reusing the same instance. The fix introduces a local effective_type variable so serialization is now pure. A focused regression test validates both idempotency and that the field is left unmutated.

Confidence Score: 5/5

Safe to merge — minimal, targeted fix with a direct regression test and no logic changes beyond removing the mutation.

The change is a one-line, obviously correct substitution of a local variable for a mutating assignment. The regression test directly exercises both properties (immutability of the field, idempotency of output). No other code paths are affected.

No files require special attention.

Important Files Changed

Filename Overview
packages/reflex-hosting-cli/src/reflex_cli/utils/hosting.py Replaces in-place mutation of self.type with a local effective_type variable, making as_json() side-effect free.
tests/units/reflex_cli/utils/test_hosting.py Adds a regression test verifying that as_json() is idempotent and leaves ScaleParams.type unmutated when it is None.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant ScaleParams

    Note over Caller,ScaleParams: Before fix (mutating)
    Caller->>ScaleParams: as_json() [type=None]
    ScaleParams->>ScaleParams: self.type = ScaleType.REGION
    ScaleParams-->>Caller: {"type": "region", ...}
    Caller->>ScaleParams: as_json() [type=REGION now!]
    ScaleParams-->>Caller: {"type": "region", ...}

    Note over Caller,ScaleParams: After fix (pure)
    Caller->>ScaleParams: as_json() [type=None]
    ScaleParams->>ScaleParams: effective_type = REGION (local only)
    ScaleParams-->>Caller: {"type": "region", ...}
    Caller->>ScaleParams: as_json() [type=None still]
    ScaleParams->>ScaleParams: effective_type = REGION (local only)
    ScaleParams-->>Caller: {"type": "region", ...}
Loading

Reviews (1): Last reviewed commit: "fix(hosting-cli): keep ScaleParams.as_js..." | Re-trigger Greptile

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 29, 2026

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing BABTUNA:fix-hosting-cli-scaleparams-pure (ff48f3a) with main (8f7ad9a)

Open in CodSpeed
@masenf masenf merged commit 76ff38c into reflex-dev:main May 1, 2026
69 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants