You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
generate_async type annotation does not support .aclose() #1955
importcontextlibimportsysimporttriofromcollections.abcimportAsyncGeneratorfromjinja2importTemplate, select_autoescapeclassMyModel:
classobjects:
@staticmethodasyncdefall() ->AsyncGenerator[str, None]:
whileTrue:
yield"hello"template="""<html> <head></head> <body> {% for m in model.objects.all() %} {{ m }} {% break %} {% endfor %} </body></html>"""asyncdefamain() ->None:
agen=Template(
source=template, enable_async=True, extensions=["jinja2.ext.loopcontrols"]
).generate_async(model=MyModel)
asyncforvinagen:
print(v)
breakdefmain() ->int|None:
returntrio.run(amain)
if__name__=="__main__":
sys.exit(main())
it outputs:
python -Wall demo_jinja_asyncgens.py
<html>
<head></head>
<body>
/home/graingert/.virtualenvs/demo-jinja-asyncgens/lib/python3.12/site-packages/trio/_core/_run.py:2599: ResourceWarning: Async generator 'jinja2.environment.Template.generate_async' was garbage collected before it had been exhausted. Surround its use in 'async with aclosing(...):' to ensure that it gets cleaned up as soon as you're done using it.
msg = task.context.run(next_send_fn, next_send)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/graingert/.virtualenvs/demo-jinja-asyncgens/lib/python3.12/site-packages/jinja2/environment.py:1371: ResourceWarning: Async generator '<<template>>.root' was garbage collected before it had been exhausted. Surround its use in 'async with aclosing(...):' to ensure that it gets cleaned up as soon as you're done using it.
yield event
ResourceWarning: Enable tracemalloc to get the object allocation traceback
following the instructions in mypy results in:
demo_jinja_asyncgens.py:31: error: Value of type variable "_SupportsAcloseT" of "aclosing" cannot be "AsyncIterator[str]" [type-var]
Activity
return an aclose-able AsyncGenerator from Template.generate_async
return an aclose-able AsyncGenerator from Template.generate_async