fix: raise TypeError when @app.template_filter is used without parentheses
This commit is contained in:
parent
a5f9742398
commit
2c331f67ea
2 changed files with 17 additions and 0 deletions
|
|
@ -672,10 +672,19 @@ class App(Scaffold):
|
||||||
def reverse(s):
|
def reverse(s):
|
||||||
return s[::-1]
|
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
|
:param name: the optional name of the filter, otherwise the
|
||||||
function name will be used.
|
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:
|
def decorator(f: T_template_filter) -> T_template_filter:
|
||||||
self.add_template_filter(f, name=name)
|
self.add_template_filter(f, name=name)
|
||||||
return f
|
return f
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,14 @@ def test_template_filter(app):
|
||||||
assert app.jinja_env.filters["my_reverse"]("abcd") == "dcba"
|
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 test_add_template_filter(app):
|
||||||
def my_reverse(s):
|
def my_reverse(s):
|
||||||
return s[::-1]
|
return s[::-1]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue