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:
- Raises:
ValueError – if no keys are specified (use
InstanceOfwithdictinstead).
Added in version 0.7.0.
Changed in version 0.8.0: added
optionally().assert ( actual == DictContaining([("foo", 123), ("bar", 456)], baz=InstanceOf(int)) )
Note: this subclasses
dictso thatpytestwill show the common and differing items. After a single comparison with a mapping, any keys that exist in the mapping but that are not specified in the matcher will appear to be in the matcher, with the same value. For example:> assert DictContaining(foo=123, bar=0, baz=789) == dict(foo=123, bar=456, qux=999) E AssertionError: assert DictContainin..., 'baz': 789}) == {'bar': 456, ...3, 'qux': 999} E E Omitting 2 identical items, use -vv to show E Differing items: E {'bar': 0} != {'bar': 456} E Left contains 1 more item: E {'baz': 789} E Use -v to get more diff
- static optionally(value)[source]¶
Matcher factory for keys that may not be present.
assert ( actual == DictContaining(foo=DictContaining.optionally(123)) )
Note: this allows keys to be missing entirely, but matches the value strictly. For the equivalent of
typing.Optional, useNullable. These can be combined if required, e.g. to allow the key"foo"to be either: missing; present with the value123; or present with the valueNone, use:assert ( actual == DictContaining(foo=DictContaining.optionally(Nullable(123))) )