Fix assertion customization kwargs handling and template interpolation
This PR fixes a critical issue where runtime assertion customization was broken due to improper kwargs handling and missing template interpolation support.
Problem
The issue manifested in two ways:
-
Missing kwargs support: Assertion
conditionmethods didn't accept**kwargs, preventing custom parameters likeexpected_verbandaggregate_verbfrom being used. -
No template interpolation: Custom assertion messages with template variables like
{left}and{right}were displayed literally instead of being interpolated with actual values.
# This would show "{left} was not {right}" instead of "1 was not 2"
assert_equal(1, 2, assertion_message="{left} was not {right}")
# This would be ignored
assert_equal(1, 2, expected_verb="to be exactly")
Solution
1. Fixed kwargs handling
Updated all condition methods in runtime.py to accept **kwargs, enabling custom parameters to flow through the assertion system properly.
2. Added template interpolation
Implemented template variable substitution in RuntimeAssertionFeedback.__init__():
-
assertion_messageandassertionparameters now support{left}and{right}template variables -
explanationparameter also supports template interpolation - Graceful fallback to literal text if template interpolation fails
3. Parameter support
Made expected_verb and aggregate_verb available as constructor parameters, not just class fields.
4. Fixed field conflicts
Resolved an issue where processed kwargs were conflicting with base class field initialization by removing processed parameters before calling super().
Examples
# Template interpolation now works
assert_equal(1, 2, assertion_message="Expected {left} to equal {right}")
# Output: "Expected 1 to equal 2"
# Complex types supported
assert_length_equal([1, 2], 5, assertion_message="Length of {left} should be {right}")
# Output: "Length of [1, 2] should be 5"
# Custom parameters work
assert_equal(result, 3, expected_verb="to be exactly")
# Uses custom verb in context-based assertion messages
# Explanation templates
assert_equal(1, 2, explanation="Values {left} and {right} should match")
# Output: "Values 1 and 2 should match"
Testing
Added comprehensive test suite with 10 test cases covering:
- Basic template interpolation
- Complex data type handling
- Custom parameter usage
- Error fallback behavior
- Multiple parameter combinations
All existing tests continue to pass, ensuring no regressions in core assertion functionality.
Fixes #137.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.