Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix(templates): ignore escaped braces when collecting fixture keys (#151)
Rudy Mizrahi Celeklicac69fb17b5f
diff --git a/ifixai/tests/test_template_escaped_placeholders.py b/ifixai/tests/test_template_escaped_placeholders.pynew file mode 100644index 0000000..a515a6f--- /dev/null+++ b/ifixai/tests/test_template_escaped_placeholders.py@@ -0,0 +1,30 @@+import pytest++from ifixai.utils.template_renderer import (+ MissingPlaceholderError,+ extract_placeholders,+ render,+)+++ "template,expected",+ [+ ("Literal {{example}}; real {name}", {"name"}),+ ("{{{{field}}}}", set()),+ ("{{{name}}}", {"name"}),+ ("{value!r:>12}", {"value"}),+ ("{value:{width}.{precision}f}", {"value", "width", "precision"}),+ ],+)+def test_extracted_keys_follow_actual_formatter_fields(template, expected):+ assert extract_placeholders(template) == expected+++def test_literal_field_does_not_require_fixture_value():+ assert (+ render("Literal {{example}}; real {name}", {"name": "Rudy"})+ == "Literal {example}; real Rudy"+ )+ with pytest.raises(MissingPlaceholderError):+ render("Literal {{example}}; real {name}", {})diff --git a/ifixai/utils/template_renderer.py b/ifixai/utils/template_renderer.pyindex 34e8d9a..6fbbf38 100644--- a/ifixai/utils/template_renderer.py+++ b/ifixai/utils/template_renderer.py@@ -1,9 +1,9 @@from __future__ import annotations-import refrom collections.abc import Mapping+from string import Formatter-_PLACEHOLDER_RE = re.compile(r"\{([a-zA-Z_][a-zA-Z0-9_]*)\}")+_FORMATTER = Formatter()class MissingPlaceholderError(KeyError):@@ -29,7 +29,12 @@ class _StrictFormatDict(dict): # type: ignore[type-arg]def extract_placeholders(template: str) -> set[str]:- return set(_PLACEHOLDER_RE.findall(template))+ fields: set[str] = set()+ for _, field, format_spec, _ in _FORMATTER.parse(template):+ if field:+ fields.add(field.split(".", 1)[0].split("[", 1)[0])+ fields.update(extract_placeholders(format_spec))+ return fieldsdef render(template: str, context: Mapping[str, object]) -> str: