Skip to content

Fix kwargs replacement bug in template function formatting#1356

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-1030
Draft

Fix kwargs replacement bug in template function formatting#1356
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-1030

Conversation

Copy link

Copilot AI commented Jul 30, 2025

The formatter was potentially replacing **kwargs syntax with {} in Jinja2 template function calls due to unsafe evaluation of function parameters.

Problem

When formatting templates like:

{% macro link_button(text, href='') %}
    {{ button(text, type="link", _="on click go to url '"~href~"'", **kwargs) }}
{% endmacro %}

The **kwargs could be replaced with {}, breaking the template functionality.

Root Cause

The format_data function in src/djlint/formatter/indent.py attempts to evaluate template function parameters using eval() as a fallback when JSON parsing fails. While **kwargs syntax normally fails eval and should preserve the original content, there were edge cases where this could interfere with Python unpacking syntax.

Solution

Added a defensive check to prevent eval() from being called on any content containing ** syntax:

# Before (risky):
evaluated = str(eval(contents)) if contents != "object" else contents

# After (safe):  
if contents != "object" and "**" not in contents:
    evaluated = str(eval(contents))
    # ... existing unwrap logic
else:
    contents = contents.strip()  # Preserve original content

This ensures that **kwargs, **data, and any other Python unpacking syntax is always preserved in template function calls.

Changes

  • Modified format_data function to skip evaluation for content containing ** syntax
  • Added comprehensive test cases covering various **kwargs scenarios
  • Preserved all existing functionality for content without ** syntax

The fix is minimal and surgical - it only affects the specific eval logic path that could cause issues, while maintaining all other formatter functionality.

Fixes #1030.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@netlify
Copy link

netlify bot commented Jul 30, 2025

Deploy Preview for djlint failed.

Name Link
🔨 Latest commit 1f3ca9f
🔍 Latest deploy log https://app.netlify.com/projects/djlint/deploys/688a6cef948d360008a43756

Copilot AI and others added 2 commits July 30, 2025 19:02
…data

Co-authored-by: monosans <76561516+monosans@users.noreply.github.com>
Co-authored-by: monosans <76561516+monosans@users.noreply.github.com>
Copilot AI changed the title [WIP] [BUG] [Formatter] Replaces kwargs with empty {} Fix kwargs replacement bug in template function formatting Jul 30, 2025
Copilot AI requested a review from monosans July 30, 2025 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [Formatter] Replaces kwargs with empty {}

2 participants