Skip to content

mtchrs

mtchrs provides small, composable matchers for validating values in tests and data structures. It is particularly useful when your code returns values that change from run to run, such as database generated IDs or UUIDs. Matchers can be nested inside dictionaries or lists and even used in unittest.mock assertions.

Installation

pip install mtchrs

Quickstart

from mtchrs import mtch

assert mtch.any() == 123
assert mtch.type(int) == 42
assert mtch.regex(r"\d+") == "456"
assert mtch.pred(lambda v: v > 0, "positive") == 1

assert mtch.partial_dict({"id": mtch.type(int)}) == {"id": 7, "name": "Ada"}
assert mtch.unordered([1, 2]) == (2, 1)

id_matcher = mtch.eq()
assert {"id": 1, "child": {"id": 1}} == {"id": id_matcher, "child": {"id": id_matcher}}

mtch.eq() captures the first value it is compared with. Use it to verify that an ID or token stays the same across a complex result or a series of operations.

See the Usage page for details on available matchers.