use getattr in jinja2.filters.make_attrgetter instead of getitem#57
Closed
kilink wants to merge 1 commit intopallets:masterfrom
kilink:master
Closed
use getattr in jinja2.filters.make_attrgetter instead of getitem#57kilink wants to merge 1 commit intopallets:masterfrom kilink:master
kilink wants to merge 1 commit intopallets:masterfrom
kilink:master
Conversation
Contributor
|
That absolutely makes sense, in my opinion. The dot notation and the fact that filters like |
Contributor
|
Unfortunately, I just discovered a problem with using getattr() instead of getitem(). If you use a value for the lookup, that isn't a string, a TypeError is raised and no item lookup is done. That's probably the reason why getitem() is used, here. |
Contributor
|
You can customize how Environment.getitem works by overriding it. I would do that instead of changing the semantics of the template engine for everybody :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
IMO, jinja2.filters.make_attrgetter should use environment.getattr instead of environment.getitem, which tries Python's getattr first and then falls back to attempting to treat the object as a dict/list.
For context, we ran into an issue when trying to use the sort filter's attribute argument on a list of security-proxied objects; trying to use index notation on them raises an exception that is not caught in environment.getitem.
I've tested this change minor change locally and all of the tests pass.