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

Async support does not work with Native environment #1362

Closed
remidebette opened this issue Mar 2, 2021 · 4 comments · Fixed by #1363
Closed

Async support does not work with Native environment #1362

remidebette opened this issue Mar 2, 2021 · 4 comments · Fixed by #1363
Labels
Milestone

Comments

@remidebette
Copy link
Contributor

Hi,

We have been starting to porting existing Flask code to an asyncio based server (Quart).
Part of it makes use of the Jinja Native environment.

The render_async works very well, but not in conjunction with Native.

Here is replicable code in a python -m asyncio console:

from jinja2.nativetypes import NativeEnvironment

def render(s, my_vars):
    native_environment = NativeEnvironment()
    template = native_environment.from_string(s)
    return template.render(**my_vars)


async def render_async(s, my_vars):
    native_environment = NativeEnvironment(enable_async=True)
    template = native_environment.from_string(s)
    return await template.render_async(**my_vars)

global_vars = {}

number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
global_vars["number_list"] = number_list

out = render("{{35}}", global_vars)
# int: 35

out = await render_async("{{35}}", global_vars)
# str: '35'


out = render("{{number_list}}", global_vars)
# list: [1, 2, 3, 4, 5, 6, 7, 8, 9]

out = await render_async("{{number_list}}", global_vars)
# Traceback (most recent call last):
#   File "/Users/A492YC/.pyenv/versions/3.8.1/lib/python3.8/concurrent/futures/_base.py", line 439, in result
#     return self.__get_result()
#   File "/Users/A492YC/.pyenv/versions/3.8.1/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result
#     raise self._exception
#   File "<console>", line 1, in <module>
#   File "<console>", line 4, in render_async
#   File "/Users/A492YC/.virtualenvs/jinja_test-SmX6WV4i/lib/python3.8/site-packages/jinja2/asyncsupport.py", line 65, in render_async
#     return self.environment.handle_exception()
#   File "/Users/A492YC/.virtualenvs/jinja_test-SmX6WV4i/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
#     reraise(*rewrite_traceback_stack(source=source))
#   File "/Users/A492YC/.virtualenvs/jinja_test-SmX6WV4i/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
#     raise value.with_traceback(tb)
#   File "/Users/A492YC/.virtualenvs/jinja_test-SmX6WV4i/lib/python3.8/site-packages/jinja2/asyncsupport.py", line 26, in concat_async
#     return concat(rv)
# TypeError: sequence item 0: expected str instance, list found

Expected:

In Native mode, instead of forcefully concatenating outputs as strings, outputs of the render_async() should be on par with the render()

Environment:

  • Python version: 3.8.1
  • Jinja version: 2.11.3
@davidism
Copy link
Member

davidism commented Mar 2, 2021

If you have time, I'd appreciate if you could look into fixing this. I'm unlikely to address this any time remotely soon.

@remidebette
Copy link
Contributor Author

Done.

It is my first time contributing so I am not sure I did it properly.
If the code change is acceptable, would you consider having an intermediate version for it, since this is a bug fix?

Thank you

@davidism
Copy link
Member

davidism commented Mar 2, 2021

Looks good overall. It will be in 3.0, I'm only doing security fixes for 2.x.

@remidebette
Copy link
Contributor Author

Ok, would you happen to know the timeline for the 3.0?

@davidism davidism added this to the 3.0.0 milestone Mar 8, 2021
@davidism davidism added the async label Mar 8, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants