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.
from dirty_equals import IsInt
assert 1 == IsInt
assert 2 == IsInt
assert 1.0 != IsInt
assert 'foobar' != IsInt
assert True != IsInt
allowed_types = int
classattribute
¶
As the name suggests, only integers are allowed, booleans (True
are False
) are explicitly excluded although
technically they are subtypes of int
.
IsFloat
¶
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
).
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
).
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
).
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
).
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 int
s.
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 int
s.
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 float
s.
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 float
s.
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
IsApprox ¶
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 
None

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 ¶
IsNumeric ¶
IsNumeric(
*,
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 

approx 
Optional[N]

A value to approximately compare to. 
None

delta 
Optional[N]

The allowable different when comparing to the value to 
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:
from dirty_equals import IsNumeric
from datetime import datetime
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
classattribute
¶
It allows any of the types supported in its subclasses.