Skip to content

Keep ui.range's value inside its bounds when they shrink - #6559

Open
Luckyou-d wants to merge 1 commit into
zauberzeug:mainfrom
Luckyou-d:fix/range-out-of-limits
Open

Luckyou-d wants to merge 1 commit into
zauberzeug:mainfrom
Luckyou-d:fix/range-out-of-limits

Conversation

@Luckyou-d

Copy link
Copy Markdown
Contributor

Motivation

ui.range's min and max setters write the prop and stop there, so moving a bound past the current position leaves the value outside the range. With value={'min': 10, 'max': 90}:

r.max = 50
r.value  # {'min': 10, 'max': 90} - the upper position is now outside the bounds

The browser keeps that stale model value as well, because QRange does not emit an update when its bounds move. I first suspected the same problem in ui.pagination (it has the same setters), but there QPagination reports the clamped value back, so it recovers on its own and needs no change.

Implementation

ui.number already clamps in exactly these two setters (self.sanitize() at the end of min and max), so ui.range follows that path instead of gaining new public API: after writing the prop, both setters call a small _clamp_value, which pulls each position into [min, max] and writes the value only when something actually moved. Widening the bounds therefore stays a no-op, and None stays None.

The degenerate case behaves like ui.number's arithmetic: when the bounds themselves are crossed (min > max), both positions land on max.

Two screen tests cover it: they shrink the upper bound and raise the lower bound, then check the clamping through a label bound to value. A third test asserts that widening the bounds leaves the value untouched.

Progress

  • The PR title is a short phrase starting with a verb like "Add ...", "Fix ...", "Update ...", "Remove ...", etc.
  • The implementation is complete. (Otherwise, open a draft PR.)
  • This PR does not address a security issue. (Security fixes must be coordinated via the security advisory process before opening a PR.)
  • Pytests: added
  • Documentation: not necessary, the bounds setters behave as their ui.number counterparts now
  • Public API: no breaking changes

Before the change, the two clamping tests fail on main; with it, tests/test_range.py, tests/test_number.py and tests/test_pagination.py pass together (31 tests, Python 3.13, Windows).

`ui.range`'s min and max setters only wrote the props, so narrowing a bound
left the current position outside the range: with value={'min': 10, 'max': 90}
and then `r.max = 50`, the value stayed {'min': 10, 'max': 90} and the browser
kept the old model value too, because QRange does not emit an update when its
bounds move. QPagination does emit one, which is why it recovers on its own.

`ui.number` already clamps inside the very same setters, so this follows that
path: both setters call `_clamp_value`, which pulls both positions into the
current bounds and writes only when something actually moved.

Covered by screen tests: the two clamping cases fail on main and pass here, and
test_range, test_number and test_pagination pass together (31 tests).
@falkoschindler falkoschindler self-assigned this Dec 2, 2026
@falkoschindler falkoschindler added this to the Next milestone Dec 2, 2026
@falkoschindler falkoschindler added the bug Type/scope: Incorrect behavior in existing functionality label Dec 2, 2026

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

bug Type/scope: Incorrect behavior in existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants