Skip to content

bagof.validators.numbers

Validators for numeric types (int, float, etc.).

Classes

IsNumber

IsNumber(hint: Any = UNSET, compose: bool = False)

Bases: Validator[NUMBER]

Validator for Number.

Note

Numeric widening is accepted: an int passes a float hint, and an int or float passes a complex hint.

IsPositive

IsPositive(hint: Any = UNSET, compose: bool = False)

Bases: IsNumber[NUMBER]

Validator for positive numbers.

IsNegative

IsNegative(hint: Any = UNSET, compose: bool = False)

Bases: IsNumber[NUMBER]

Validator for negative numbers.

IsNonNegative

IsNonNegative(hint: Any = UNSET, compose: bool = False)

Bases: IsNumber[NUMBER]

Validator for non-negative numbers.

IsNonPositive

IsNonPositive(hint: Any = UNSET, compose: bool = False)

Bases: IsNumber[NUMBER]

Validator for non-positive numbers.

IsLessThan

IsLessThan(threshold: NUMBER, hint: Any = UNSET, compose: bool = False)

Bases: _ComparatorValidator[NUMBER]

Validator for numbers less than a threshold.

IsLessEqual

IsLessEqual(threshold: NUMBER, hint: Any = UNSET, compose: bool = False)

Bases: _ComparatorValidator[NUMBER]

Validator for numbers less than or equal to a threshold.

IsGreaterThan

IsGreaterThan(threshold: NUMBER, hint: Any = UNSET, compose: bool = False)

Bases: _ComparatorValidator[NUMBER]

Validator for numbers greater than a threshold.

IsGreaterEqual

IsGreaterEqual(threshold: NUMBER, hint: Any = UNSET, compose: bool = False)

Bases: _ComparatorValidator[NUMBER]

Validator for numbers greater than or equal to a threshold.

IsInRange

IsInRange(min_value: NUMBER, max_value: NUMBER, inclusive: bool | tuple[bool, bool] = True, hint: Any = UNSET, compose: bool = False)

Bases: IsNumber[NUMBER]

Validator for numbers in a range.

Example

>>> from bagof.validators.numbers import IsInRange
>>> validate = IsInRange(0, 1)
>>> validate(0.5)
>>> validate(2)
ValueValidationError: IsInRange(0, 1): Not in range [0, 1].
|> value = 2

Parameters:

Name Type Description Default
min_value NUMBER

The minimum value of the range.

required
max_value NUMBER

The maximum value of the range.

required
inclusive bool | (bool, bool)

Whether the range is inclusive on both ends. If a single boolean is provided, it applies to both ends.

True
hint Any

The type hint to validate against.

UNSET
compose bool

Whether to compose this validator with others, when they are found in Annotated metadata.

False

Methods: