Skip to content

Dictionary Types

IsDict

IsDict(
    *expected_args: Dict[Any, Any], **expected_kwargs: Any
)

Bases: DirtyEquals[Dict[Any, Any]]

Base class for comparing dictionaries. By default, IsDict isn't particularly useful on its own (it behaves pretty much like a normal dict), but it can be subclassed (see IsPartialDict and IsStrictDict) or modified with .settings(...) to powerful things.

Can be created from either keyword arguments or an existing dictionary (same as dict()).

IsDict is not particularly useful on its own, but it can be subclassed or modified with .settings(...) to facilitate powerful comparison of dictionaries.

IsDict
from dirty_equals import IsDict

assert {'a': 1, 'b': 2} == IsDict(a=1, b=2)
assert {1: 2, 3: 4} == IsDict({1: 2, 3: 4})

settings

settings(
    *,
    strict: Optional[bool] = None,
    partial: Optional[bool] = None,
    ignore: Union[
        None, Container[Any], Callable[[Any], bool]
    ] = NotGiven
) -> IsDict

Allows you to customise the behaviour of IsDict, technically a new IsDict is required to allow chaining.

Parameters:

Name Type Description Default
strict bool

If True, the order of key/value pairs must match.

None
partial bool

If True, only keys include in the wrapped dict are checked.

None
ignore Union[None, Container[Any], Callable[[Any], bool]]

Values to omit from comparison. Can be either a Container (e.g. set or list) of values to ignore, or a function that takes a value and should return True if the value should be ignored.

NotGiven
IsDict.settings(...)
from dirty_equals import IsDict

assert {'a': 1, 'b': 2, 'c': None} != IsDict(a=1, b=2)
assert {'a': 1, 'b': 2, 'c': None} == IsDict(a=1, b=2).settings(partial=True) #(1)!

assert {'b': 2, 'a': 1} == IsDict(a=1, b=2)
assert {'b': 2, 'a': 1} != IsDict(a=1, b=2).settings(strict=True) #(2)!

# combining partial and strict
assert {'a': 1, 'b': None, 'c': 3} == IsDict(a=1, c=3).settings(strict=True, partial=True)
assert {'b': None, 'c': 3, 'a': 1} != IsDict(a=1, c=3).settings(strict=True, partial=True)
  1. This is the same as IsPartialDict(a=1, b=2)
  2. This is the same as IsStrictDict(a=1, b=2)

IsPartialDict

Bases: IsDict

Partial dictionary comparison, this is the same as IsDict(...).settings(partial=True).

IsPartialDict
from dirty_equals import IsPartialDict

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

assert {'a': 1, 'b': 2, 'c': 3} != IsPartialDict(a=1, b=3)
assert {'a': 1, 'b': 2, 'd': 3} != IsPartialDict(a=1, b=2, c=3)

# combining partial and strict
assert {'a': 1, 'b': None, 'c': 3} == IsPartialDict(a=1, c=3).settings(strict=True)
assert {'b': None, 'c': 3, 'a': 1} != IsPartialDict(a=1, c=3).settings(strict=True)

IsIgnoreDict

Bases: IsDict

Dictionary comparison with None values ignored, this is the same as IsDict(...).settings(ignore={None}).

.settings(...) can be used to customise the behaviour of IsIgnoreDict, in particular changing which values are ignored.

IsIgnoreDict
from dirty_equals import IsIgnoreDict

assert {'a': 1, 'b': 2, 'c': None} == IsIgnoreDict(a=1, b=2)
assert {'a': 1, 'b': 2, 'c': None, 'c': 'ignore'} == (
    IsIgnoreDict(a=1, b=2).settings(ignore={None, 'ignore'})
)

def is_even(v: int) -> bool:
    return v % 2 == 0

assert {'a': 1, 'b': 2, 'c': 3, 'd': 4} == (
    IsIgnoreDict(a=1, c=3).settings(ignore=is_even)
)

# combining partial and strict
assert {'a': 1, 'b': None, 'c': 3} == IsIgnoreDict(a=1, c=3).settings(strict=True)
assert {'b': None, 'c': 3, 'a': 1} != IsIgnoreDict(a=1, c=3).settings(strict=True)

IsStrictDict

Bases: IsDict

Dictionary comparison with order enforced, this is the same as IsDict(...).settings(strict=True).

IsDict.settings(...)
from dirty_equals import IsStrictDict

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

# combining partial and strict
assert {'a': 1, 'b': None, 'c': 3} == IsStrictDict(a=1, c=3).settings(partial=True)
assert {'b': None, 'c': 3, 'a': 1} != IsStrictDict(a=1, c=3).settings(partial=True)