Skip to content

Using namespaces asynchronously raises fatal AttributeError #1180

@bartfeenstra

Description

@bartfeenstra

When asynchronously rendering templates that make use of namespaces, rendering fails on AttributeError: __class__.

Expected Behavior

No error is raised.

Actual Behavior

Using namespaces when rendering templates asynchronously fails on the isawaitable() check performed on Namespace.

Template Code

{% set ns = namespace(foo=[]) %}

Reproduction:

import asyncio

from jinja2 import Environment

async def main():
    environment = Environment(
        enable_async=True
    )
    template = '{% set ns = namespace(foo=[]) %}'
    await environment.from_string(template).render_async()

asyncio.get_event_loop().run_until_complete(main())

Full Traceback

Traceback (most recent call last):
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/site-packages/jinja2/utils.py", line 699, in __getattribute__
    return self.__attrs[name]
KeyError: '__class__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "namespace.py", line 12, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "namespace.py", line 10, in main
    await environment.from_string(template).render_async()
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/site-packages/jinja2/asyncsupport.py", line 65, in render_async
    return self.environment.handle_exception()
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/site-packages/jinja2/asyncsupport.py", line 25, in concat_async
    await collect()
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/site-packages/jinja2/asyncsupport.py", line 22, in collect
    async for event in async_gen:
  File "<template>", line 1, in top-level template code
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/site-packages/jinja2/asyncsupport.py", line 173, in auto_await
    if inspect.isawaitable(value):
  File "/usr/lib/python3.8/inspect.py", line 234, in isawaitable
    isinstance(object, collections.abc.Awaitable))
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/abc.py", line 98, in __instancecheck__
    return _abc_instancecheck(cls, instance)
  File "/home/bart/tmp/jinja2-async-namespacce/venv/lib/python3.8/site-packages/jinja2/utils.py", line 701, in __getattribute__
    raise AttributeError(name)
AttributeError: __class__

Your Environment

  • Python version: Python 3.8
  • Jinja version: 2.11.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions