Skip to content

fix: correct Range semantics for zero and empty specs (#15, #83) - #159

Open
mpounsett wants to merge 2 commits into
mainfrom
fix/15-83-range-spec
Open

mpounsett wants to merge 2 commits into
mainfrom
fix/15-83-range-spec

Conversation

@mpounsett

Copy link
Copy Markdown
Owner

Summary

Two long-standing Range spec bugs, fixed together because they share a root cause (the spec = spec or '' falsy check in Range.__new__) and because the fix for one forces a decision about the other.

#83: Range(0) produced 0:inf instead of 0:0. The falsy check turned the int 0 into '', so critical=0 could never trigger. The numeric branch in __new__ was also dead code: its result was overwritten by the string parse on the next line. Numeric specs are now used directly rather than round-tripped through str(), which also makes floats whose repr uses an exponent (e.g. 1e20) and float('inf') usable as specs.

#15: an empty spec meant 0:, so negative values tripped omitted thresholds. Range(''), Range(None), and an omitted ScalarContext threshold are now unbounded in both directions, equivalent to '~:', and never alert. The unbounded range still renders as '' so performance data for omitted thresholds is byte-for-byte unchanged. Range('0:') now renders as '0:' rather than '' so the two ranges remain distinguishable in perfdata and repr().

Also: Range(False) (which only worked by accident of the falsy check) and Range(True) now raise TypeError with a hint to use None. NaN bounds raise ValueError instead of silently matching every value.

This supersedes #128, which patched the or '' symptom but left the numeric branch dead.

Related issue

Fixes #15
Fixes #83
Closes #128

Testing

  • Full suite locally on Python 3.14: 151 passed, 1 skipped.
  • New tests cover zero int/float specs including str(), violation and match output, exponent-repr floats, infinity, NaN, bools, the '' vs '0:' distinction, repr round-tripping for nine specs, and the exact None passed as range defaults to "0:" #15 scenario at the ScalarContext level including perfdata output.
  • Ran the reproducer from None passed as range defaults to "0:" #15 end to end: SENSORS OK - sens1 is -3 | sens1=-3;-10:10, exit 0. With an explicit critical='0:' it still goes CRITICAL.
  • Ruff and bandit report no new findings.

Compatibility and security

Public behaviour changes, all called out in CHANGELOG.txt with an IMPORTANT NOTE at the top of the 1.5.0 section:

  • Range(0) / Range(0.0): 0:inf becomes 0:0. Plugins passing an int 0 had a silently disabled threshold and will now alert as intended.
  • Empty spec / None: 0:inf becomes -inf:inf. Plugins relying on an omitted threshold to alert on negative values must pass '0:' explicitly.
  • str(Range('0:')): '' becomes '0:'. Shows up in perfdata for plugins that pass '0:' explicitly.
  • Range(False): was 0:inf, now TypeError.
  • Range('@'): was @0:, now inverted-unbounded (never matches). Falls out of the parser change; unlikely to be in use.

No untrusted input handling or security implications.

Checklist

  • I have read and followed the contribution guide
  • Tests were added or updated where appropriate
  • Documentation and the changelog were updated where appropriate
  • The PR title clearly describes the change

Range(0) and Range(0.0) were coerced to Range('') by the falsy check in
Range.__new__, producing 0:inf instead of 0:0, so a threshold such as
critical=0 could never trigger. Numeric specs are now used directly
instead of being round-tripped through str(), which also makes floats
with an exponent in their repr and float('inf') usable as specs.

An empty spec (Range(''), Range(None), or an omitted ScalarContext
threshold) is now unbounded in both directions, equivalent to '~:',
so it never alerts. Previously it meant '0:' and a negative metric
value would unexpectedly produce a WARNING or CRITICAL. The unbounded
range still renders as '' so performance data is unchanged, and
Range('0:') now renders as '0:' so the two stay distinguishable.

Range(False), which worked only by accident of the falsy check, and
Range(True) now both raise TypeError. NaN bounds raise ValueError
instead of silently matching every value.

Fixes #15
Fixes #83
Closes #128
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.

Bug Class Range: Range(0) gives wrong (empty) Range whereas Range('0') works properly None passed as range defaults to "0:"

1 participant