Matchers¶
The composable matchers exposed by the match namespace.
Call them as match.greater_than(5), match.is_uuid(), and so on, then combine with &, |,
and ~. See the Matchers guide for usage, composition, and custom matchers.
Factory namespace for creating matcher instances.
Usage:
from assertpy2 import match
assert_that(value).satisfies(match.greater_than(5) & match.less_than(10))
assert_that(items).each(match.is_positive())
equal_to
staticmethod
¶
Matcher for a value equal to expected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
object
|
the value to compare against |
required |
strict_types
|
bool
|
also require the same type, so |
False
|
**options
|
object
|
the rest of what
|
{}
|
Examples:
Usage:
assert_that(reading).satisfies(match.equal_to(expected, tolerance=0.01))
assert_that(payload).matches_structure({"user": match.equal_to(user, ignore="updated_at")})
Source code in assertpy2/matchers.py
greater_than
staticmethod
¶
greater_than_or_equal_to
staticmethod
¶
less_than
staticmethod
¶
less_than_or_equal_to
staticmethod
¶
between
staticmethod
¶
close_to
staticmethod
¶
Matcher for a value within tolerance of expected (abs(value - expected) <= tolerance).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
object
|
the target value |
required |
tolerance
|
object
|
the maximum allowed absolute difference from |
required |
Source code in assertpy2/matchers.py
is_none
staticmethod
¶
is_not_none
staticmethod
¶
is_instance_of
staticmethod
¶
Matcher for an instance of expected_type (via isinstance).
Accepts whatever isinstance accepts: a class, a union (int | str), or a tuple of
either. The builder assertion of the same name stays narrower on purpose - its overloads
refine the tracked value to the given class, and a union has no single class to refine to.
Reach for is_instance_of_any there.
Source code in assertpy2/matchers.py
is_type_of
staticmethod
¶
Matcher for exactly expected_type, rejecting subclasses (int but not bool).
is_truthy
staticmethod
¶
is_falsy
staticmethod
¶
has_length
staticmethod
¶
is_length
staticmethod
¶
Matcher for a value whose len() equals length.
The same matcher as has_length(), under the name the fluent assertion uses
(is_length()). One relation was reachable as
has_length from the matcher namespace and as is_length from the builder, so which name
worked depended on which of the two a reader had seen first. Both work from both now.
Source code in assertpy2/matchers.py
is_empty
staticmethod
¶
is_not_empty
staticmethod
¶
is_positive
staticmethod
¶
is_negative
staticmethod
¶
is_zero
staticmethod
¶
is_even
staticmethod
¶
is_odd
staticmethod
¶
is_divisible_by
staticmethod
¶
is_callable
staticmethod
¶
is_in
staticmethod
¶
Matcher for a value present in values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*values
|
object
|
the candidate values; the matched value must equal one of them |
()
|
has_property
staticmethod
¶
Matcher for an object with attribute name, optionally matching matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the attribute name the object must have |
required |
matcher
|
Matcher | None
|
optional matcher the attribute value must satisfy; if |
None
|
Source code in assertpy2/matchers.py
contains_string
staticmethod
¶
Matcher for text containing substring, on str and on bytes alike.
matches_regex
staticmethod
¶
starts_with
staticmethod
¶
Matcher for text starting with prefix, on str and on bytes alike.
ends_with
staticmethod
¶
all_of
staticmethod
¶
Matcher that holds when every one of matchers matches (the & operator).
any_of
staticmethod
¶
Matcher that holds when at least one of matchers matches (the | operator).
not_
staticmethod
¶
ignore
staticmethod
¶
is_uuid
staticmethod
¶
is_non_empty_string
staticmethod
¶
is_now
staticmethod
¶
Matcher for a datetime within delta of the current time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta
|
float | timedelta
|
tolerance as seconds (a number) or a |
2.0
|
Source code in assertpy2/matchers.py
is_before
staticmethod
¶
Matcher for a datetime strictly before other (a non-comparable value never matches).
is_after
staticmethod
¶
Matcher for a datetime strictly after other (a non-comparable value never matches).
contains
staticmethod
¶
Matcher for a collection containing every one of items.
The spec spelling of contains(), with the same
rules: a mapping is searched by key, and a matcher among the items is satisfied by any element.
Examples:
Usage:
assert_that(payload).matches_structure({"tags": match.contains("beta")})
assert_that(rows).satisfies(match.contains(match.greater_than(100)))
Source code in assertpy2/matchers.py
contains_only
staticmethod
¶
Matcher for a collection holding these items and nothing else.
The spec spelling of contains_only().
Source code in assertpy2/matchers.py
is_subset_of
staticmethod
¶
Matcher for a collection whose items all appear in superset.
The spec spelling of is_subset_of().
Takes the superset either as one collection or as loose items, which is why it is overloaded:
read off a single union, a checker cannot tell [1, 2] the collection from [1, 2] the item.
An ordinary collection stays a live view of itself, the way equal_to keeps its expected
value; a one-shot iterator is drained when the matcher is built, since it could not answer a
second time otherwise. Handing in an endless iterator therefore never returns.
Source code in assertpy2/matchers.py
is_sorted
staticmethod
¶
is_sorted(
key: Callable[[Any], Any] | None = None,
reverse: bool = False,
) -> Matcher[Iterable[Any]]
Matcher for a collection in order, optionally by key and optionally reversed.
The spec spelling of is_sorted().
Source code in assertpy2/matchers.py
each_item
staticmethod
¶
Matcher for an iterable whose every item matches matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher[Any]
|
the matcher each item of the iterable must satisfy; a non-iterable value never matches |
required |
Source code in assertpy2/matchers.py
structure
staticmethod
¶
Matcher for a dict matching spec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
dict[Any, Any]
|
dict whose values are matchers, raw values (compared with |
required |
Examples:
Usage:
assert_that(user).satisfies(
match.structure({"id": match.is_instance_of(int), "name": "Alice"})
)
Source code in assertpy2/matchers.py
BaseMatcher ¶
Abstract base for all matchers with operator support.
A subclass implements either matches() or evaluate(), and gets the other one from here.
matches() is the cheap primitive: it is what == calls, and matchers are used as dict values in
matches_structure and as snapshot placeholders, so a comparison must not have to build a result
object. evaluate() is the whole answer, for a caller that would otherwise ask the same value
three questions in a row.
matches ¶
evaluate ¶
The verdict, the requirement and the reason, from one look at value.
The default composes the three older methods, so a matcher written before this existed answers
it without changing. Overriding it is for a matcher whose reason costs what the verdict already
paid for: the alternative is matches() and describe_mismatch() walking the same value twice,
which is how a matcher over a one-shot iterator used to name the wrong element.
Source code in assertpy2/_matcher_impls.py
describe ¶
MatchResult
dataclass
¶
MatchResult(
*,
matched: bool,
description: str,
mismatch: str = "",
diff: DiffResult | None = None,
)
What a matcher decided about one value, in one object instead of three calls.
Deliberately not an AssertionOutcome, which is the record of
a failed assertion. A matcher is asked about every leaf of a structure and about every element of
a collection, so its result has to stay cheap: four fields, no location, no group, nothing that has
to be computed before it is known whether anyone will read it.
description
instance-attribute
¶
What the matcher requires, in the words it uses in a failure message: a positive value.
mismatch
class-attribute
instance-attribute
¶
Why this value did not match, in the words a failure message continues with: was <-1>.
Empty when it matched. There is nothing to say about a value that satisfied the matcher, and the text would have to be invented.
diff
class-attribute
instance-attribute
¶
A structured diff, from a matcher that compares rather than tests: equality has one, is_odd
does not.