Skip to content

Keep ui.number format display-only in sanitize() - #6550

Merged
falkoschindler merged 4 commits into
zauberzeug:mainfrom
SHANNWD:fix/number-format-sanitize
Dec 2, 2026
Merged

falkoschindler merged 4 commits into
zauberzeug:mainfrom
SHANNWD:fix/number-format-sanitize

Conversation

@SHANNWD

@SHANNWD SHANNWD commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes #6548.

ui.number's format parameter is documented as formatting the displayed value, which _value_to_model_value already handles. However, Number.sanitize() ended with self.value = float(self.format % value) if self.format else value, writing the formatted string back into self.value on blur and whenever min, max or precision change. Because this happened after clamping, the rounding could push the value outside [min, max]: ui.number(value=0.5, min=0.4, format='%.0f') became 0.0 with out_of_limits == True.

Implementation

  • nicegui/elements/number.py: sanitize() assigns self.value = value instead of re-parsing self.format % value, so it only clamps to [min, max] and applies precision, leaving format strictly for display. The format docstring now says that the value itself is not rounded.
  • examples/sqlite_database/main.py, examples/reaktiv/main.py: the age and price inputs relied on format to round the stored value; they get a matching precision so they keep storing what they show.
  • tests/test_number.py: test_apply_format_on_blur shows the unrounded value in a bound label next to the formatted input, and test_format_does_not_round_the_value_below_the_minimum covers the limit case from the issue.

Migration: apps that relied on format to round the value (e.g. the value passed to on_change after a blur) now get the unrounded value and need precision in addition, e.g. format='%.2f', precision=2.

Progress

  • The PR title is a short phrase starting with a verb like "Add ...", "Fix ...", "Update ...", "Remove ...", etc.
  • The implementation is complete.
  • This PR does not address a security issue.
  • Pytests: added/updated
  • Documentation: added/updated
  • Public API: breaking changes with migration steps described above

Disclosure by the author: assisted by an AI coding assistant; all changes and tests were reviewed and verified locally.

※

@falkoschindler falkoschindler added bug Type/scope: Incorrect behavior in existing functionality review Status: PR is open and needs review labels Dec 1, 2026
@falkoschindler falkoschindler self-assigned this Dec 1, 2026
@falkoschindler
falkoschindler self-requested a review December 1, 2026 14:59
@falkoschindler falkoschindler added this to the 4.2 milestone Dec 1, 2026
…ough the display

The SQLite and reaktiv examples used `format` to round the age and the prices; now that `format` only affects the display, they get a matching `precision` so they keep storing what they show. The `format` docstring says so explicitly.

The new test asserted on the private `model-value` prop. Instead, `test_apply_format_on_blur` now shows the unrounded value in a bound label next to the formatted input, and a `User` test covers the limit case from zauberzeug#6548 through `value` and `out_of_limits` only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgKVNb2aAsju6ryUWTWzky
falkoschindler
falkoschindler previously approved these changes Dec 1, 2026

@falkoschindler falkoschindler 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 @SHANNWD, this is exactly the fix the issue asked for: sanitize() now only clamps and rounds to precision, and format is display-only. I pushed a small follow-up commit onto your branch for the points below and brought the description onto our PR template, including the migration note for the release notes.

Addressed in b987018
  • Two checked-in examples relied on format to round the stored value: the age in examples/sqlite_database/main.py and the prices in examples/reaktiv/main.py. With format display-only they would show "26" but store 25.7, so they now pass a matching precision, which is the migration the issue prescribes.
  • The format docstring now says that the value itself is not rounded and points to precision.
  • The new test asserted on the private _props['model-value']. Per our guidelines tests assert observable behaviour, so test_apply_format_on_blur now shows the unrounded value in a bound label next to the formatted input, and a User test covers the limit case from #6548 through value and out_of_limits only.

One thing I noticed along the way is out of scope here: format is a plain attribute, so changing it at runtime does not re-render the field. That is handled separately in #6553.

※

@falkoschindler
falkoschindler added this pull request to the merge queue Dec 1, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Dec 1, 2026
Resolves the conflict with zauberzeug#6546 in `sanitize()`: the inward step after rounding stays, and the last line keeps `self.value = value` so the format is not written back into the value.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgKVNYuqJYSmAQ5KHxKYPn
falkoschindler
falkoschindler previously approved these changes Dec 2, 2026
@falkoschindler
falkoschindler added this pull request to the merge queue Dec 2, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 2, 2026
…anitize

# Conflicts:
#	nicegui/elements/number.py
@falkoschindler
falkoschindler added this pull request to the merge queue Dec 2, 2026
Merged via the queue into zauberzeug:main with commit f60102b Dec 2, 2026
8 checks passed
falkoschindler added a commit that referenced this pull request Dec 2, 2026
Both sides appended a test to tests/test_number.py; the merge keeps
test_format_does_not_round_the_value_below_the_minimum from #6550
followed by test_changing_format.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgKVNZ3csUJiybGywqbjcv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Type/scope: Incorrect behavior in existing functionality review Status: PR is open and needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ui.number writes its format back into the value, which can push it outside the limits

2 participants