Skip to content

Fix indent filter ignoring blank=False for empty first line#2181

Closed
sapirbaruch wants to merge 2 commits into
pallets:mainfrom
sapirbaruch:fix-indent-blank-first-line
Closed

Fix indent filter ignoring blank=False for empty first line#2181
sapirbaruch wants to merge 2 commits into
pallets:mainfrom
sapirbaruch:fix-indent-blank-first-line

Conversation

@sapirbaruch

Copy link
Copy Markdown

Fixes #2176.

When first=True is passed to the indent filter, the code was
unconditionally prepending the indentation string to the output, without
checking whether blank=False should suppress it when the first line is
empty.

Before:

>>> env.from_string('{% filter indent(4, first=true) %}{% endfilter %}').render()
'    '  # four spaces added to an empty string

After:

>>> env.from_string('{% filter indent(4, first=true) %}{% endfilter %}').render()
''  # blank=False (default) suppresses indentation of the empty first line

The fix is a one-liner in do_indent: instead of if first:, check
if first and (blank or rv.split("\n", 1)[0]): so that an empty first
line is only indented when blank=True is explicitly requested.

A new test (test_indent_first_blank) covers the three cases:

  1. Empty string with first=True → no indentation
  2. Leading blank line with first=True → blank line left alone, rest indented
  3. Empty string with first=True, blank=True → indentation applied

SapirBaruch and others added 2 commits June 2, 2026 10:30
When first=True is set, the indent filter was unconditionally prepending
the indentation to the whole string, even when blank=False and the first
line was empty. Now the first-line indentation is skipped when blank=False
and the first line has no content.

Fixes pallets#2176
@sapirbaruch

Copy link
Copy Markdown
Author

The PyPy failure is a pre-existing trio incompatibility that affects other open PRs too — not caused by this change. All other checks pass.

@davidism davidism closed this Jun 2, 2026
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.

indent filter ignores blank=False when first=True on first line

2 participants