fix: raise TypeError when @app.template_filter is used without parentheses

This commit is contained in:
gable-github 2025-05-16 15:07:40 -07:00
parent a5f9742398
commit 2c331f67ea
2 changed files with 17 additions and 0 deletions

View file

@ -672,10 +672,19 @@ class App(Scaffold):
def reverse(s):
return s[::-1]
You must include parentheses when using this decorator,
even if no arguments are provided.
:param name: the optional name of the filter, otherwise the
function name will be used.
"""
if callable(name):
raise TypeError(
"Did you mean to use @app.template_filter()? "
"Missing parentheses () in decorator"
)
def decorator(f: T_template_filter) -> T_template_filter:
self.add_template_filter(f, name=name)
return f

View file

@ -130,6 +130,14 @@ def test_template_filter(app):
assert app.jinja_env.filters["my_reverse"]("abcd") == "dcba"
def test_template_filter_requires_parentheses(app):
with pytest.raises(TypeError):
@app.template_filter
def my_reverse(s):
return s[::-1]
def test_add_template_filter(app):
def my_reverse(s):
return s[::-1]