Matchers Module

Argument matchers are placed as values inside expected args dicts to assert on the shape of an argument rather than its exact value. See Argument Matchers for a guide with runnable examples.

Argument matcher objects for use inside expected-args dicts.

Matchers are placed as values inside the expected argument dicts passed to toolscore.evaluate(). They work via operator overloading: Matcher.__eq__(other) performs the match, so they compose transparently with plain dict equality and with the argument-comparison logic in toolscore.metrics.arguments.

Example:

from toolscore import evaluate, ANY, Regex, Approx

result = evaluate(
    expected=[{"tool": "get_weather", "args": {"city": Regex(r"NYC|JFK")}}],
    actual=[{"tool": "get_weather", "args": {"city": "NYC"}}],
)
assert result.argument_f1 == 1.0
toolscore.matchers.ANY: _AnyMatcher = ANY

Module-level singleton; matches any value.

class toolscore.matchers.Approx(value, rel=1e-06, abs=0.0)[source]

Bases: Matcher

Numeric closeness matcher (similar to pytest.approx semantics).

Matches int and float values but explicitly excludes bool (even though bool is a subclass of int in Python).

The comparison uses the larger of the relative and absolute tolerances:

|actual - expected| <= max(rel * |expected|, abs_tol)
Parameters:
  • value (float) – The expected numeric value.

  • rel (float) – Relative tolerance (default 1e-6).

  • abs (float) – Absolute tolerance (default 0.0). Note this differs from pytest.approx, whose default absolute tolerance is 1e-12. With abs=0.0 the match is purely relative, so a comparison against an expected value of 0 requires exact equality.

__init__(value, rel=1e-06, abs=0.0)[source]
Parameters:
Return type:

None

matches(value)[source]

Return True if value satisfies this matcher.

Return type:

bool

Parameters:

value (object)

class toolscore.matchers.Contains(item)[source]

Bases: Matcher

Membership matcher: checks item in value.

Works for str, list, tuple, set, and dict (key membership for dicts). Non-container types never match.

Parameters:

item (object) – The item to look for inside the value.

__init__(item)[source]
Parameters:

item (object)

Return type:

None

matches(value)[source]

Return True if value satisfies this matcher.

Return type:

bool

Parameters:

value (object)

class toolscore.matchers.IsType(*types)[source]

Bases: Matcher

Type-check matcher using isinstance().

Note

IsType(int) does not match True or False even though bool is a subclass of int in Python. This avoids a common footgun when you want to match plain integers but not accidental booleans. Use IsType(bool) explicitly to match booleans.

Parameters:

*types (type) – One or more types to check against.

__init__(*types)[source]
Parameters:

types (type)

Return type:

None

matches(value)[source]

Return True if value satisfies this matcher.

Return type:

bool

Parameters:

value (object)

class toolscore.matchers.Matcher[source]

Bases: ABC

Abstract base class for all argument matchers.

Subclasses implement matches() which is called by __eq__. Because __eq__ is overridden, __hash__ must be explicitly preserved — we delegate to object.__hash__() so matchers remain usable as dict keys / set members.

abstractmethod matches(value)[source]

Return True if value satisfies this matcher.

Return type:

bool

Parameters:

value (object)

class toolscore.matchers.OneOf(*values)[source]

Bases: Matcher

Value-is-one-of matcher.

Checks whether value equals any of the provided values. The comparison uses == so the provided values may themselves be Matcher instances (their __eq__ will be invoked).

Parameters:

*values (object) – Candidate values (or Matchers) to test against.

__init__(*values)[source]
Parameters:

values (object)

Return type:

None

matches(value)[source]

Return True if value satisfies this matcher.

Return type:

bool

Parameters:

value (object)

class toolscore.matchers.Regex(pattern, flags=0)[source]

Bases: Matcher

Full-match a string against a regular expression pattern.

Non-string values never match.

Parameters:
  • pattern (str) – Regular expression pattern string.

  • flags (int) – Optional re flags (e.g. re.IGNORECASE).

__init__(pattern, flags=0)[source]
Parameters:
Return type:

None

matches(value)[source]

Return True if value satisfies this matcher.

Return type:

bool

Parameters:

value (object)