Numeric assertions¶
Assertions for int, float, and complex values.
Numeric assertions mixin.
is_zero ¶
Asserts that val is numeric and is zero.
Examples:
Usage:
assert_that(0).is_zero()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not zero |
Source code in assertpy2/numeric.py
is_not_zero ¶
Asserts that val is numeric and is not zero.
Examples:
Usage:
assert_that(1).is_not_zero()
assert_that(123.4).is_not_zero()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is zero |
Source code in assertpy2/numeric.py
is_nan ¶
Asserts that val is real number and is NaN (not a number).
Examples:
Usage:
assert_that(float('nan')).is_nan()
assert_that(float('inf') * 0).is_nan()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not NaN |
Source code in assertpy2/numeric.py
is_not_nan ¶
Asserts that val is real number and is not NaN (not a number).
Examples:
Usage:
assert_that(0).is_not_nan()
assert_that(123.4).is_not_nan()
assert_that(float('inf')).is_not_nan()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is NaN |
Source code in assertpy2/numeric.py
is_inf ¶
Asserts that val is real number and is Inf (infinity).
Examples:
Usage:
assert_that(float('inf')).is_inf()
assert_that(float('inf') * 1).is_inf()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not Inf |
Source code in assertpy2/numeric.py
is_not_inf ¶
Asserts that val is real number and is not Inf (infinity).
Examples:
Usage:
assert_that(0).is_not_inf()
assert_that(123.4).is_not_inf()
assert_that(float('nan')).is_not_inf()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is Inf |
Source code in assertpy2/numeric.py
is_greater_than ¶
Asserts that val is numeric and is greater than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the other date, expected to be less than val |
required |
Examples:
Usage:
assert_that(1).is_greater_than(0)
assert_that(123.4).is_greater_than(111.1)
For dates, behavior is identical to is_after():
import datetime
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
assert_that(today).is_greater_than(yesterday)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not greater than other |
Note
A NaN value is unordered, so it fails every relational assertion here
(is_greater_than, is_less_than, is_between, is_positive, ...). This diverges
from the original assertpy, where NaN silently passes.
Source code in assertpy2/numeric.py
is_greater_than_or_equal_to ¶
Asserts that val is numeric and is greater than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the other date, expected to be less than or equal to val |
required |
Examples:
Usage:
assert_that(1).is_greater_than_or_equal_to(0)
assert_that(1).is_greater_than_or_equal_to(1)
assert_that(123.4).is_greater_than_or_equal_to(111.1)
For dates, behavior is identical to is_after() except when equal:
import datetime
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
assert_that(today).is_greater_than_or_equal_to(yesterday)
assert_that(today).is_greater_than_or_equal_to(today)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not greater than or equal to other |
Source code in assertpy2/numeric.py
is_less_than ¶
Asserts that val is numeric and is less than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the other date, expected to be greater than val |
required |
Examples:
Usage:
assert_that(0).is_less_than(1)
assert_that(123.4).is_less_than(555.5)
For dates, behavior is identical to is_before():
import datetime
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
assert_that(yesterday).is_less_than(today)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not less than other |
Note
A NaN value is unordered and fails here, diverging from the original assertpy where
NaN silently passes relational assertions.
Source code in assertpy2/numeric.py
is_less_than_or_equal_to ¶
Asserts that val is numeric and is less than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the other date, expected to be greater than or equal to val |
required |
Examples:
Usage:
assert_that(1).is_less_than_or_equal_to(0)
assert_that(1).is_less_than_or_equal_to(1)
assert_that(123.4).is_less_than_or_equal_to(100.0)
For dates, behavior is identical to is_before()
except when equal:
import datetime
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
assert_that(yesterday).is_less_than_or_equal_to(today)
assert_that(today).is_less_than_or_equal_to(today)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not less than or equal to other |
Source code in assertpy2/numeric.py
is_positive ¶
Asserts that val is numeric and is greater than zero.
Examples:
Usage:
assert_that(1).is_positive()
assert_that(123.4).is_positive()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not positive |
Note
NaN is not positive, so this fails for float("nan"). This diverges from the
original assertpy, where NaN silently passes relational assertions.
Source code in assertpy2/numeric.py
is_negative ¶
Asserts that val is numeric and is less than zero.
Examples:
Usage:
assert_that(-1).is_negative()
assert_that(-123.4).is_negative()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not negative |
Note
NaN is not negative, so this fails for float("nan"). This diverges from the
original assertpy, where NaN silently passes relational assertions.
Source code in assertpy2/numeric.py
is_between ¶
Asserts that val is numeric and is between low and high.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
object
|
the low value |
required |
high
|
object
|
the high value |
required |
Examples:
Usage:
assert_that(1).is_between(0, 2)
assert_that(123.4).is_between(111.1, 222.2)
For dates, works as expected:
import datetime
today = datetime.datetime.now()
middle = today - datetime.timedelta(hours=12)
yesterday = today - datetime.timedelta(days=1)
assert_that(middle).is_between(yesterday, today)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not between low and high |
Note
NaN is not within any range, so this fails for float("nan"). This diverges from
the original assertpy, where NaN silently passes relational assertions.
Source code in assertpy2/numeric.py
is_not_between ¶
Asserts that val is numeric and is not between low and high.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
object
|
the low value |
required |
high
|
object
|
the high value |
required |
Examples:
Usage:
assert_that(1).is_not_between(2, 3)
assert_that(1.1).is_not_between(2.2, 3.3)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is between low and high |
Source code in assertpy2/numeric.py
is_even ¶
Asserts that val is an integer and is even.
Examples:
Usage:
assert_that(0).is_even()
assert_that(2).is_even()
assert_that(-4).is_even()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not even |
Source code in assertpy2/numeric.py
is_odd ¶
Asserts that val is an integer and is odd.
Examples:
Usage:
assert_that(1).is_odd()
assert_that(3).is_odd()
assert_that(-5).is_odd()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not odd |
Source code in assertpy2/numeric.py
is_divisible_by ¶
Asserts that val is an integer and is divisible by divisor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
divisor
|
SupportsIndex
|
the divisor to check against (must be a non-zero integer) |
required |
Examples:
Usage:
assert_that(10).is_divisible_by(5)
assert_that(12).is_divisible_by(3)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not divisible by divisor |
Source code in assertpy2/numeric.py
is_close_to ¶
is_close_to(
other: SupportsFloat | SupportsIndex | datetime,
tolerance: SupportsFloat | SupportsIndex | timedelta,
) -> Self
Asserts that val is numeric and is close to other within tolerance.
The distance is taken three ways, the difference and the window around each side, and any of them holding is enough, so the answer is the same whichever operand is val. An infinity is close only to itself, whatever the tolerance, and a NaN to nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the other value, expected to be close to val within tolerance |
required |
tolerance
|
object
|
the tolerance |
required |
Examples:
Usage:
assert_that(123).is_close_to(100, 25)
assert_that(123.4).is_close_to(123, 0.5)
For dates, works as expected:
import datetime
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
assert_that(today).is_close_to(yesterday, datetime.timedelta(hours=36))
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not close to other within tolerance |
TypeError
|
if val, other or tolerance is not a number (or a |
ValueError
|
if tolerance is negative or NaN |
Source code in assertpy2/numeric.py
is_not_close_to ¶
is_not_close_to(
other: SupportsFloat | SupportsIndex | datetime,
tolerance: SupportsFloat | SupportsIndex | timedelta,
) -> Self
Asserts that val is numeric and is not close to other within tolerance.
Measured as is_close_to() measures, so an infinity
is not close to anything but itself and a NaN to nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the other value |
required |
tolerance
|
object
|
the tolerance |
required |
Examples:
Usage:
assert_that(123).is_not_close_to(100, 22)
assert_that(123.4).is_not_close_to(123, 0.1)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is close to other within tolerance |
TypeError
|
if val, other or tolerance is not a number (or a |
ValueError
|
if tolerance is negative or NaN |