Skip to content

Other Types

FunctionCheck

FunctionCheck(func: Callable[[Any], bool])

Bases: DirtyEquals[Any]

Use a function to check if a value "equals" whatever you want to check

Parameters:

Name Type Description Default
func Callable[[Any], bool]

callable that takes a value and returns a bool.

required
FunctionCheck
from dirty_equals import FunctionCheck

def is_even(x):
    return x % 2 == 0

assert 2 == FunctionCheck(is_even)
assert 3 != FunctionCheck(is_even)

IsInstance

IsInstance(
    expected_type: ExpectedType,
    *,
    only_direct_instance: bool = False
)

Bases: DirtyEquals[ExpectedType]

A type which checks that the value is an instance of the expected type.

Parameters:

Name Type Description Default
expected_type ExpectedType

The type to check against.

required
only_direct_instance bool

whether instances of subclasses of expected_type should be considered equal.

False

Note

IsInstance can be parameterized or initialised with a type - IsInstance[Foo] is exactly equivalent to IsInstance(Foo).

This allows usage to be analogous to type hints.

Example:

IsInstance
from dirty_equals import IsInstance

class Foo:
    pass

class Bar(Foo):
    pass

assert Foo() == IsInstance[Foo]
assert Foo() == IsInstance(Foo)
assert Foo != IsInstance[Bar]

assert Bar() == IsInstance[Foo]
assert Foo() == IsInstance(Foo, only_direct_instance=True)
assert Bar() != IsInstance(Foo, only_direct_instance=True)

IsJson

IsJson(
    expected_value: JsonType = AnyJson,
    **expected_kwargs: Any
)

Bases: DirtyEquals[JsonType]

A class that checks if a value is a JSON object, and check the contents of the JSON.

Parameters:

Name Type Description Default
expected_value JsonType

Value to compare the JSON to, if omitted, any JSON is accepted.

AnyJson
**expected_kwargs Any

Keyword arguments forming a dict to compare the JSON to, expected_value and expected_kwargs may not be combined.

{}

As with any dirty_equals type, types can be nested to provide more complex checks.

Note

Like IsInstance, IsJson can be parameterized or initialised with a value - IsJson[xyz] is exactly equivalent to IsJson(xyz).

This allows usage to be analogous to type hints.

IsJson
from dirty_equals import IsJson, IsStrictDict, IsPositiveInt

assert '{"a": 1, "b": 2}' == IsJson
assert '{"a": 1, "b": 2}' == IsJson(a=1, b=2)
assert '{"a": 1}' != IsJson(a=2)
assert 'invalid json' != IsJson
assert '{"a": 1}' == IsJson(a=IsPositiveInt)
assert '"just a quoted string"' == IsJson('just a quoted string')

assert '{"a": 1, "b": 2}' == IsJson[IsStrictDict(a=1, b=2)]
assert '{"b": 2, "a": 1}' != IsJson[IsStrictDict(a=1, b=2)]

IsUUID

IsUUID(version: Literal[None, 1, 2, 3, 4, 5] = None)

Bases: DirtyEquals[UUID]

A class that checks if a value is a valid UUID, optionally checking UUID version.

Parameters:

Name Type Description Default
version Literal[None, 1, 2, 3, 4, 5]

The version of the UUID to check, if omitted, all versions are accepted.

None
IsUUID
import uuid
from dirty_equals import IsUUID

assert 'edf9f29e-45c7-431c-99db-28ea44df9785' == IsUUID
assert 'edf9f29e-45c7-431c-99db-28ea44df9785' == IsUUID(4)
assert 'edf9f29e45c7431c99db28ea44df9785' == IsUUID(4)
assert 'edf9f29e-45c7-431c-99db-28ea44df9785' != IsUUID(5)
assert uuid.uuid4() == IsUUID(4)

AnyThing

Bases: DirtyEquals[Any]

A type which matches any value. AnyThing isn't generally very useful on its own, but can be used within other comparisons.

AnyThing
from dirty_equals import AnyThing, IsList, IsStrictDict

assert 1 == AnyThing
assert 'foobar' == AnyThing
assert [1, 2, 3] == AnyThing

assert [1, 2, 3] == IsList(AnyThing, 2, 3)

assert {'a': 1, 'b': 2, 'c': 3} == IsStrictDict(a=1, b=AnyThing, c=3)

IsOneOf

IsOneOf(expected_value: Any, *more_expected_values: Any)

Bases: DirtyEquals[Any]

A type which checks that the value is equal to one of the given values.

Can be useful with boolean operators.

Parameters:

Name Type Description Default
expected_value Any

Expected value for equals to return true.

required
*more_expected_values Any

More expected values for equals to return true.

()
IsOneOf
from dirty_equals import IsOneOf, Contains

assert 1 == IsOneOf(1, 2, 3)
assert 4 != IsOneOf(1, 2, 3)
# check that a list either contain 1 or is empty
assert [1, 2, 3] == Contains(1) | IsOneOf([])
assert [] == Contains(1) | IsOneOf([])

IsIP

IsIP(
    *,
    version: Literal[None, 4, 6] = None,
    netmask: Optional[str] = None
)

Bases: DirtyEquals[IP]

A class that checks if a value is a valid IP address, optionally checking IP version, netmask.

Parameters:

Name Type Description Default
version Literal[None, 4, 6]

The version of the IP to check, if omitted, versions 4 and 6 are both accepted.

None
netmask Optional[str]

The netmask of the IP to check, if omitted, any netmask is accepted. Requires version.

None
IsIP
from ipaddress import IPv4Address, IPv6Address, IPv4Network
from dirty_equals import IsIP

assert '179.27.154.96' == IsIP
assert '179.27.154.96' == IsIP(version=4)
assert '2001:0db8:0a0b:12f0:0000:0000:0000:0001' == IsIP(version=6)
assert IPv4Address('127.0.0.1') == IsIP
assert IPv4Network('43.48.0.0/12') == IsIP
assert IPv6Address('::eeff:ae3f:d473') == IsIP
assert '54.43.53.219/10' == IsIP(version=4, netmask='255.192.0.0')
assert '54.43.53.219/10' == IsIP(version=4, netmask=4290772992)
assert '::ffff:aebf:d473/12' == IsIP(version=6, netmask='fff0::')
assert 3232235521 == IsIP