Skip to content

Numeric Types

IsInt

Bases: IsNumeric[int]

Checks that a value is an integer.

Inherits from IsNumeric and can therefore be initialised with any of its arguments.

IsInt
from dirty_equals import IsInt

assert 1 == IsInt
assert -2 == IsInt
assert 1.0 != IsInt
assert 'foobar' != IsInt
assert True != IsInt
assert 1 == IsInt(exactly=1)
assert -2 != IsInt(exactly=1)

allowed_types = int class-attribute instance-attribute

As the name suggests, only integers are allowed, booleans (True are False) are explicitly excluded although technically they are sub-types of int.

IsFloat

Bases: IsNumeric[float]

Checks that a value is a float.

Inherits from IsNumeric and can therefore be initialised with any of its arguments.

IsFloat
from dirty_equals import IsFloat

assert 1.0 == IsFloat
assert 1 != IsFloat
assert 1.0 == IsFloat(exactly=1.0)
assert 1.001 != IsFloat(exactly=1.0)

allowed_types = float class-attribute instance-attribute

As the name suggests, only floats are allowed.

IsPositive

Bases: IsNumber

Check that a value is positive (> 0), can be an int, a float or a Decimal (or indeed any value which implements __gt__ for 0).

IsPositive
from decimal import Decimal

from dirty_equals import IsPositive

assert 1.0 == IsPositive
assert 1 == IsPositive
assert Decimal('3.14') == IsPositive
assert 0 != IsPositive
assert -1 != IsPositive

IsNegative

Bases: IsNumber

Check that a value is negative (< 0), can be an int, a float or a Decimal (or indeed any value which implements __lt__ for 0).

IsNegative
from decimal import Decimal

from dirty_equals import IsNegative

assert -1.0 == IsNegative
assert -1 == IsNegative
assert Decimal('-3.14') == IsNegative
assert 0 != IsNegative
assert 1 != IsNegative

IsNonNegative

Bases: IsNumber

Check that a value is positive or zero (>= 0), can be an int, a float or a Decimal (or indeed any value which implements __ge__ for 0).

IsNonNegative
from decimal import Decimal

from dirty_equals import IsNonNegative

assert 1.0 == IsNonNegative
assert 1 == IsNonNegative
assert Decimal('3.14') == IsNonNegative
assert 0 == IsNonNegative
assert -1 != IsNonNegative
assert Decimal('0') == IsNonNegative

IsNonPositive

Bases: IsNumber

Check that a value is negative or zero (<=0), can be an int, a float or a Decimal (or indeed any value which implements __le__ for 0).

IsNonPositive
from decimal import Decimal

from dirty_equals import IsNonPositive

assert -1.0 == IsNonPositive
assert -1 == IsNonPositive
assert Decimal('-3.14') == IsNonPositive
assert 0 == IsNonPositive
assert 1 != IsNonPositive
assert Decimal('-0') == IsNonPositive
assert Decimal('0') == IsNonPositive

IsPositiveInt

Bases: IsInt

Like IsPositive but only for ints.

IsPositiveInt
from decimal import Decimal

from dirty_equals import IsPositiveInt

assert 1 == IsPositiveInt
assert 1.0 != IsPositiveInt
assert Decimal('3.14') != IsPositiveInt
assert 0 != IsPositiveInt
assert -1 != IsPositiveInt

IsNegativeInt

Bases: IsInt

Like IsNegative but only for ints.

IsNegativeInt
from decimal import Decimal

from dirty_equals import IsNegativeInt

assert -1 == IsNegativeInt
assert -1.0 != IsNegativeInt
assert Decimal('-3.14') != IsNegativeInt
assert 0 != IsNegativeInt
assert 1 != IsNegativeInt

IsPositiveFloat

Bases: IsFloat

Like IsPositive but only for floats.

IsPositiveFloat
from decimal import Decimal

from dirty_equals import IsPositiveFloat

assert 1.0 == IsPositiveFloat
assert 1 != IsPositiveFloat
assert Decimal('3.14') != IsPositiveFloat
assert 0.0 != IsPositiveFloat
assert -1.0 != IsPositiveFloat

IsNegativeFloat

Bases: IsFloat

Like IsNegative but only for floats.

IsNegativeFloat
from decimal import Decimal

from dirty_equals import IsNegativeFloat

assert -1.0 == IsNegativeFloat
assert -1 != IsNegativeFloat
assert Decimal('-3.14') != IsNegativeFloat
assert 0.0 != IsNegativeFloat
assert 1.0 != IsNegativeFloat

IsFloatInf

Bases: IsFloat

Checks that a value is float and infinite (positive or negative).

Inherits from IsFloat.

IsFloatInf
from dirty_equals import IsFloatInf

assert float('inf') == IsFloatInf
assert float('-inf') == IsFloatInf
assert 1.0 != IsFloatInf

IsFloatInfPos

Bases: IsFloatInf

Checks that a value is float and positive infinite.

Inherits from IsFloatInf.

IsFloatInfPos
from dirty_equals import IsFloatInfPos

assert float('inf') == IsFloatInfPos
assert -float('-inf') == IsFloatInfPos
assert -float('inf') != IsFloatInfPos
assert float('-inf') != IsFloatInfPos

IsFloatInfNeg

Bases: IsFloatInf

Checks that a value is float and negative infinite.

Inherits from IsFloatInf.

IsFloatInfNeg
from dirty_equals import IsFloatInfNeg

assert -float('inf') == IsFloatInfNeg
assert float('-inf') == IsFloatInfNeg
assert float('inf') != IsFloatInfNeg
assert -float('-inf') != IsFloatInfNeg

IsFloatNan

Bases: IsFloat

Checks that a value is float and nan (not a number).

Inherits from IsFloat.

IsFloatNan
from dirty_equals import IsFloatNan

assert float('nan') == IsFloatNan
assert 1.0 != IsFloatNan

IsApprox

IsApprox(approx: Num, *, delta: Optional[Num] = None)

Bases: IsNumber

Simplified subclass of IsNumber that only allows approximate comparisons.

Parameters:

Name Type Description Default
approx Num

A value to approximately compare to.

required
delta Optional[Num]

The allowable different when comparing to the value to approx, if omitted value / 100 is used.

None
IsApprox
from dirty_equals import IsApprox

assert 1.0 == IsApprox(1)
assert 123 == IsApprox(120, delta=4)
assert 201 == IsApprox(200)
assert 201 != IsApprox(200, delta=0.1)

IsNumber

Bases: IsNumeric[AnyNumber]

Base class for all types that can be used with all number types, e.g. numeric but not date or datetime.

Inherits from IsNumeric and can therefore be initialised with any of its arguments.

allowed_types class-attribute instance-attribute

allowed_types = (int, float, Decimal)

It allows any of the number types.

IsNumeric

IsNumeric(*, exactly: Optional[N] = None, approx: Optional[N] = None, delta: Optional[N] = None, gt: Optional[N] = None, lt: Optional[N] = None, ge: Optional[N] = None, le: Optional[N] = None)

Bases: DirtyEquals[N]

Base class for all numeric types, IsNumeric implements approximate and inequality comparisons, as well as the type checks.

This class can be used directly or via any of its subclasses.

Parameters:

Name Type Description Default
exactly Optional[N]

A value to exactly compare to - useful when you want to make sure a value is an int or float, while also checking its value.

None
approx Optional[N]

A value to approximately compare to.

None
delta Optional[N]

The allowable different when comparing to the value to approx, if omitted value / 100 is used except for datetimes where 2 seconds is used.

None
gt Optional[N]

Value which the compared value should be greater than.

None
lt Optional[N]

Value which the compared value should be less than.

None
ge Optional[N]

Value which the compared value should be greater than or equal to.

None
le Optional[N]

Value which the compared value should be less than or equal to.

None

If not values are provided, only the type is checked.

If approx is provided as well a gt, lt, ge, or le, a TypeError is raised.

Example of direct usage:

IsNumeric
from datetime import datetime

from dirty_equals import IsNumeric

assert 1.0 == IsNumeric
assert 4 == IsNumeric(gt=3)
d = datetime(2020, 1, 1, 12, 0, 0)
assert d == IsNumeric(approx=datetime(2020, 1, 1, 12, 0, 1))

allowed_types class-attribute instance-attribute

allowed_types: Union[Type[N], Tuple[type, ...]] = (int, float, Decimal, date, datetime)

It allows any of the types supported in its subclasses.