Skip to content

Constrain renderers to only stringify items inheriting from BaseTag#561

Merged
pydanny merged 2 commits intomainfrom
547-bug-jinja2renderers-tag-rendering-is-too-broad-in-scope-1
Oct 15, 2025
Merged

Constrain renderers to only stringify items inheriting from BaseTag#561
pydanny merged 2 commits intomainfrom
547-bug-jinja2renderers-tag-rendering-is-too-broad-in-scope-1

Conversation

@pydanny
Copy link
Member

@pydanny pydanny commented Oct 12, 2025

Issue(s)

#547

Description

@bluerosej discovered that Air's Jinja rendering system is stringifying everything. She also provided a fix in pyladiesmanila/VoicesOfPyLadiesManila#18. We expand on that excellent fix by constraining it to just stringify BaseTag-inherited items so that Jinja can operate normally.

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • New feature
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

Pull request tasks

The following have been completed for this task:

  • Code changes
  • Documentation changes for new or changed features
  • Alterations of behavior come with a working implementation in the examples folder
  • Tests on new or altered behaviors

Checklist

  • I have run just test and just qa, ensuring my code changes passes all existing tests
  • I have performed a self-review of my own code
  • I have ensured that there are tests to cover my changes

@pydanny pydanny added bug python Pull requests that update python code Air Tags labels Oct 12, 2025
@pydanny pydanny linked an issue Oct 12, 2025 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Oct 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pydanny pydanny requested review from Copilot and pygarap October 12, 2025 06:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where Air's Jinja rendering system was stringifying all variables in the template context, preventing normal Jinja operations on non-tag objects like lists and dictionaries. The fix constrains stringification to only objects that inherit from BaseTag, allowing Jinja to process other data types normally.

  • Modified context processing to only stringify BaseTag instances
  • Added test coverage for Jinja's handling of lists and dictionaries
  • Created supporting test template to verify proper rendering behavior

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/air/templating.py Updated context processing logic to conditionally stringify only BaseTag instances
tests/test_templating.py Added test to verify Jinja can process lists and dicts without stringification
tests/templates/lists_and_dicts.html Created test template that renders metadata and iterates over list items

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@pydanny pydanny requested a review from audreyfeldroy October 12, 2025 07:21
@pydanny pydanny force-pushed the 547-bug-jinja2renderers-tag-rendering-is-too-broad-in-scope-1 branch from 378f383 to df35751 Compare October 14, 2025 05:06
@intellectronica
Copy link
Contributor

LGTM


One other option that could make this a bit more readable (subjectively, to my eyes) would be:

def _jinja_context_item(self, item):
    """Prepare an item for processing by Jinja
    
    BaseTag instances are converted to string.
    All other objects are handled by Jinja directly.
    """

    if isinstance(item, BaseTag):
        return str(item)
    return item

...

context = {k: self._jinja_context_item(v) for k, v in context.items()}

It's a matter of taste and I wouldn't block on merging - the change looks perfect - but this more verbose version is self-documenting and easier to extend if the need ever arises.

@@ -184,7 +185,7 @@ def _prepare_context(self, context: dict[Any, Any] | None, kwargs: dict[Any, Any

Copy link
Contributor

@alaminopu alaminopu Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of the __call__ function should be updated accordingly to _TemplateResponse or str | _TemplateResponse

Copy link
Member Author

@pydanny pydanny Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggested change throws all kinds of type errors. For now I'm merging this in, will look at the typing issue in a separate PR.

pydanny and others added 2 commits October 16, 2025 05:29
@bluerosej discovered that Air's Jinja rendering system is stringifying everything. She also provided a fix in pyladiesmanila/VoicesOfPyLadiesManila#18. We expand on that excellent fix by constraining it to just stringify BaseTag-inherited items so that Jinja can operate normally.

Co-Authored-By: Rose J <195869163+bluerosej@users.noreply.github.com>
Introduced _jinja_context_item helper to consistently convert BaseTag instances to strings before rendering with Jinja. Updated JinjaRenderer and Renderer to use this helper for context preparation. Also added .cov_html and .benchmarks to skip list in pyproject.toml.

Co-Authored-By: Eleanor Berger <46264+intellectronica@users.noreply.github.com>
Co-Authored-By: Md Al Amin  <alaminopu.me@gmail.com>
@pydanny pydanny force-pushed the 547-bug-jinja2renderers-tag-rendering-is-too-broad-in-scope-1 branch from c33d9c1 to e18d419 Compare October 15, 2025 21:55
@pydanny
Copy link
Member Author

pydanny commented Oct 15, 2025

@intellectronica I made the change you suggested and gave you attribution. Thanks!

@pydanny pydanny merged commit 0ec67b3 into main Oct 15, 2025
10 checks passed
@pydanny pydanny deleted the 547-bug-jinja2renderers-tag-rendering-is-too-broad-in-scope-1 branch October 15, 2025 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Air Tags bug python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Jinja2Renderer's tag rendering is too broad in scope

3 participants

Comments