Dictionary Types¶
IsDict ¶
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.
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: bool | None = None, partial: bool | None = None, ignore: 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 |
None
|
partial |
bool
|
If |
None
|
ignore |
Union[None, Container[Any], Callable[[Any], bool]]
|
Values to omit from comparison.
Can be either a |
NotGiven
|
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)
assert {'b': 2, 'a': 1} == IsDict(a=1, b=2)
assert {'b': 2, 'a': 1} != IsDict(a=1, b=2).settings(strict=True)
# 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
)
IsPartialDict ¶
Bases: IsDict
Partial dictionary comparison, this is the same as
IsDict(...).settings(partial=True)
.
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)
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.
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})
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.
from dirty_equals import IsIgnoreDict
assert {'a': 1, 'b': 2, 'c': None} == IsIgnoreDict(a=1, b=2)
assert {'a': 1, 'b': 2, '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)
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.
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})
IsStrictDict ¶
Bases: IsDict
Dictionary comparison with order enforced, this is the same as
IsDict(...).settings(strict=True)
.
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)
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.
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})