Skip to content

Prevent ChainableUndefined from testing as escaped - #2259

Open
MatMoore wants to merge 1 commit into
pallets:mainfrom
MatMoore:is-escaped-should-fail-for-chainable-undefined
Open

Prevent ChainableUndefined from testing as escaped#2259
MatMoore wants to merge 1 commit into
pallets:mainfrom
MatMoore:is-escaped-should-fail-for-chainable-undefined

Conversation

@MatMoore

@MatMoore MatMoore commented Sep 1, 2026

Copy link
Copy Markdown

fixes #2244

The implementation of the escaped test uses hasattr to detect escaped strings, but ChainableUndefined returns self for any attribute, so hasattr always returns True.

This means the value is escaped test returned true for ChainableUndefined even though other Undefined values correctly returned false.

I don't think any documentation needs updating, but please correct me if I'm wrong. The contributing guide said adding a changelog is optional, and I wasn't sure how to do it, so I've left that out.

@MatMoore
MatMoore marked this pull request as ready for review September 1, 2026 15:47
Comment thread src/jinja2/tests.py Outdated
def test_escaped(value: t.Any) -> bool:
"""Check if the value is escaped."""
return hasattr(value, "__html__")
return hasattr(value, "__html__") and not isinstance(value, ChainableUndefined)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's be safe and use the base Undefined. No undefined value should ever be considered escaped, regardless of the specifics.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No problem, have just adjusted it

@davidism

davidism commented Sep 1, 2026

Copy link
Copy Markdown
Member

I can write a changelog, no problem. I really appreciate that you checked the contributing guide!

The implementation of the `escaped` test uses `hasattr` to detect
escaped string, but `ChainableUndefined` returns self for any attribute,
so `hasattr` always returns True.

This means the test returned true for `ChainableUndefined` even though
other Undefined values correctly returned false.
@MatMoore
MatMoore force-pushed the is-escaped-should-fail-for-chainable-undefined branch from 5460156 to 76c223f Compare September 2, 2026 08:34
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.

ChainableUndefined causes is escaped test to pass for undefined child elements

2 participants