joythief.data_structures module

class joythief.data_structures.DictContaining(**kwargs: Any)[source]
class joythief.data_structures.DictContaining(content: Mapping[Hashable, Any], /, **kwargs: Any)
class joythief.data_structures.DictContaining(content: Iterable[tuple[Hashable, Any]], /, **kwargs: Any)

Bases: Matcher[Mapping[Hashable, Any]], dict[Hashable, Any]

Match the specified keys in a mapping, ignoring any extra keys.

Parameters:
  • content (Any) – mapping or sequence of key-value pairs to include in the comparison

  • **kwargs (Any) – additional key-value pairs to include in the comparison

Raises:

ValueError – if no keys are specified (use InstanceOf with dict instead).

assert (
    actual
    == DictContaining([("foo", 123), ("bar", 456)], baz=InstanceOf(int))
)

Note: this subclasses dict so that pytest will show the common and differing items, e.g.:

>       assert mapping == dict(foo=123, bar=456)
E       AssertionError: assert DictContaining(**{'foo': 123, 'bar': 0, 'baz': 789}) == {'foo': 123, 'bar': 456}
E
E         Common items:
E         {'foo': 123}
E         Differing items:
E         {'bar': 0} != {'bar': 456}
E         Left contains 1 more item:
E         {'baz': 789}
# ...