Skip to content

Commit 786867a

Browse files
authored
Merge pull request #1181 from bartfeenstra/async-namespace
Allow namespaces to be used in asynchronously rendered templates.
2 parents 5f95471 + 0fd45a4 commit 786867a

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Unreleased
2323
:pr:`1182`
2424
- Fix line numbers in error messages when newlines are stripped.
2525
:pr:`1178`
26+
- The special ``namespace()`` assignment object in templates works in
27+
async environments. :issue:`1180`
2628

2729

2830
Version 2.11.1

src/jinja2/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,8 @@ def __init__(*args, **kwargs): # noqa: B902
697697
self.__attrs = dict(*args, **kwargs)
698698

699699
def __getattribute__(self, name):
700-
if name == "_Namespace__attrs":
700+
# __class__ is needed for the awaitable check in async mode
701+
if name in {"_Namespace__attrs", "__class__"}:
701702
return object.__getattribute__(self, name)
702703
try:
703704
return self.__attrs[name]

tests/test_async.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,14 @@ def test_bare_async(self, test_env_async):
578578
def test_awaitable_property_slicing(self, test_env_async):
579579
t = test_env_async.from_string("{% for x in a.b[:1] %}{{ x }}{% endfor %}")
580580
assert t.render(a=dict(b=[1, 2, 3])) == "1"
581+
582+
583+
def test_namespace_awaitable(test_env_async):
584+
async def _test():
585+
t = test_env_async.from_string(
586+
'{% set ns = namespace(foo="Bar") %}{{ ns.foo }}'
587+
)
588+
actual = await t.render_async()
589+
assert actual == "Bar"
590+
591+
run(_test())

0 commit comments

Comments
 (0)