Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a NativeTemplate directly doesn't create a NativeEnvironment #1091

Closed
cindyzwang opened this issue Oct 25, 2019 · 6 comments · Fixed by #1095
Closed

Creating a NativeTemplate directly doesn't create a NativeEnvironment #1091

cindyzwang opened this issue Oct 25, 2019 · 6 comments · Fixed by #1095
Labels
bug
Milestone

Comments

@cindyzwang
Copy link

@cindyzwang cindyzwang commented Oct 25, 2019

Expected Behavior

Create a dictionary with keys and values that are strings. I was able to do this correctly when I was doing NativeTemplate(...).render(...) so I would expect that the behavior would be the same if I used a NativeEnvironment(...).from_string(...).render(...).

Actual Behavior

I have some templates that create dictionaries. However, those tests error out because I expect to create a payload like {'apples': 'bananas'} but am instead creating {'apples: bananas'}.

I dug around and saw that preserve_quotes=False here, which was odd because I thought the behavior should be the same if I was creating an environment or using the spontaneous environment. But it turns out the spontaneous environment is the base Environment.

Template Code

>>> env = NativeEnvironment()
>>> env.from_string('{ "{{a}}": "{{b}}" }').render(a='apple', b='banana')
{'apple: banana'}
>>> NativeTemplate('{ "{{a}}": "{{b}}" }').render(a='apple', b='banana')
{'apple': 'banana'}
>>> NativeTemplate('').environment
<jinja2.environment.Environment object at 0x10a688cc0>

Full Traceback

No traceback because it doesn't throw an error, this is just not the expected behavior

Your Environment

  • Python version: 3.6.6
  • Jinja version: 2.10

How I'm currently working around it

env = Environment()
... # set up globals & filters
env.template_class = NativeTemplate
@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Oct 25, 2019

The issue you're describing is real (get_spontaneous_environment doesn't take into account what type of environment to create, only the args to it), but the example you've shown works correctly on my machine, both lines produce dicts.

@cindyzwang

This comment has been minimized.

Copy link
Author

@cindyzwang cindyzwang commented Oct 25, 2019

I just updated my version of Jinja2 to 2.10.3 and still have the same output:
Screen Shot 2019-10-25 at 12 56 28 PM

If you're using the NativeEnvironment, shouldn't you expect this same output? It's documented here, and preserve_quotes is hard-coded to False

@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Oct 25, 2019

The behavior and fix (#1086) you're referring to is part of 2.11, which is not released yet. You're looking at the master branch, but you're running 2.10.3, those aren't the same.

@cindyzwang

This comment has been minimized.

Copy link
Author

@cindyzwang cindyzwang commented Oct 25, 2019

so this will be fixed in 2.11?

@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Oct 25, 2019

Yes, it's marked as part of the 2.11 milestone.

@cindyzwang cindyzwang closed this Oct 25, 2019
@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Oct 25, 2019

Sorry, I don't think I was clear. You've reported a real issue, in that NativeEnvironment.from_string() and NativeTemplate() produce different results. It's just that the specific issue you showed is already fixed as part of another fix. But more complex templates will behave differently if create_spontaneous_environment() isn't fixed.

@davidism davidism reopened this Oct 25, 2019
@davidism davidism changed the title "preserve_quotes=False" for NativeEnvironment causes inconsistent behavior, can't create dictionaries Creating a NativeTemplate directly doesn't create a NativeEnvironment Oct 28, 2019
@davidism davidism added the bug label Oct 28, 2019
@davidism davidism added this to the 2.11.0 milestone Oct 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants
You can’t perform that action at this time.