Skip to content

Add pragma: no cover implementation#110

Open
marcgibbons wants to merge 5 commits intocoveragepy:mainfrom
marcgibbons:pragma-no-cover
Open

Add pragma: no cover implementation#110
marcgibbons wants to merge 5 commits intocoveragepy:mainfrom
marcgibbons:pragma-no-cover

Conversation

@marcgibbons
Copy link
Copy Markdown
Contributor

@marcgibbons marcgibbons commented Apr 7, 2026

This PR implements pragma: no cover for templates. I tried to mirror the same behaviour used in Python; when declared on a block, the entire block is excluded. When declared on a single line / not a block tag, only that line is excluded. The implementation supports the exclude_lines and exclude_also coverage.py report settings.

Example:

{% if condition %}{# pragma: no cover #} 
    {{ something }} {# This gets excluded, as do lines above and below #} 
{% endif %}
<div>
   {% cycle "1" "2"%}{# pragma: no cover #}   {# Only this line is excluded #}
</div>

I tried keeping iterations to a minimum. I evaluate the tokens as list prior to the for loop to support a lookahead search for nested closing tags.

Fixes #70
Fixes #45

Tests cover block-tag exclusion (with both true/false conditions),
plain text line exclusion, nested block exclusion, custom exclude
patterns via coverage config, and self-closing tags like {% cycle %}
that should not be treated as block openers.
Allow users to exclude template lines from coverage using Django
comment syntax. A pragma on a block-opening tag excludes the entire
block (including nested blocks) through its matching closing tag.
A pragma on a plain text or variable line excludes that line only.

Uses coverage.py's report:exclude_lines patterns, so the default
pragma: no cover works with zero configuration and custom patterns
defined in .coveragerc / pyproject.toml are picked up automatically.

Block openers are identified by a forward look-ahead for a matching
end tag, so self-closing tags like cycle, load, and url are never
mistakenly treated as block openers.
@marcgibbons marcgibbons changed the title Pragma no cover Add pragma: no cover implementation Apr 7, 2026
@marcgibbons marcgibbons marked this pull request as ready for review April 7, 2026 12:31
@marcgibbons marcgibbons marked this pull request as draft April 7, 2026 14:49
@marcgibbons marcgibbons marked this pull request as ready for review April 7, 2026 15:31
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.

Ignore lines in templates Ignoring coverage for some branches

1 participant