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, IsPositiveInt, IsStrictDict

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 Contains, IsOneOf

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([])

IsUrl

IsUrl(
    any_url: bool = False,
    any_http_url: bool = False,
    http_url: bool = False,
    file_url: bool = False,
    postgres_dsn: bool = False,
    ampqp_dsn: bool = False,
    redis_dsn: bool = False,
    **expected_attributes: Any
)

Bases: DirtyEquals[str]

A class that checks if a value is a valid URL, optionally checking different URL types and attributes with Pydantic.

Parameters:

Name Type Description Default
any_url bool

any scheme allowed, TLD not required, host required

False
any_http_url bool

scheme http or https, TLD not required, host required

False
http_url bool

scheme http or https, TLD required, host required, max length 2083

False
file_url bool

scheme file, host not required

False
postgres_dsn bool

user info required, TLD not required

False
ampqp_dsn bool

schema amqp or amqps, user info not required, TLD not required, host not required

False
redis_dsn bool

scheme redis or rediss, user info not required, tld not required, host not required

False
**expected_attributes Any

Expected values for url attributes

{}
IsUrl
from dirty_equals import IsUrl

assert 'https://example.com' == IsUrl
assert 'https://example.com' == IsUrl(tld='com')
assert 'https://example.com' == IsUrl(scheme='https')
assert 'https://example.com' != IsUrl(scheme='http')
assert 'postgres://user:[email protected]:5432/app' == IsUrl(postgres_dsn=True)
assert 'postgres://user:[email protected]:5432/app' != IsUrl(http_url=True)

IsHash

IsHash(hash_type: HashTypes)

Bases: DirtyEquals[str]

A class that checks if a value is a valid common hash type, using a simple length and allowed characters regex.

Parameters:

Name Type Description Default
hash_type HashTypes

The hash type to check. Must be specified.

required
IsHash
from dirty_equals import IsHash

assert 'f1e069787ece74531d112559945c6871' == IsHash('md5')
assert b'f1e069787ece74531d112559945c6871' == IsHash('md5')
assert 'f1e069787ece74531d112559945c6871' != IsHash('sha-256')
assert 'F1E069787ECE74531D112559945C6871' == IsHash('md5')
assert '40bd001563085fc35165329ea1ff5c5ecbdbbeef' == IsHash('sha-1')
assert 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3' == IsHash(
    'sha-256'
)

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, IPv4Network, IPv6Address

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