-
-
Notifications
You must be signed in to change notification settings - Fork 16.4k
The template_filter
decorator doesn't work if you don't pass an argument
#5729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
What is it about this that has attracted five completely new contributors who aren't reading the contributing guide:
|
This fixes an issue where template decorators like @app.template_filter don't work when used without parentheses. The fix allows the decorators to be used in both ways: - @app.template_filter - @app.template_filter() The following decorators have been fixed: - template_filter, template_test, template_global in Flask - app_template_filter, app_template_test, app_template_global in Blueprint Fixes pallets#5729
What's the issue?
You can use
template_filter
as a decorator, but it only registers the filter if you write an explicit name or an empty set of parentheses. If you call it without parens, the filter doesn't get registered.It's a small difference and can be confusing.
Minimal example
Consider the following program:
If you run this app (
flask run --port 8008 --debug
) and then open it in your browser (http://localhost:8008
) you'll get an error:This is confusing, and it took me a while to realise the missing parentheses in
app.template_filter
were at fault.Suggested fix
I think it would be helpful if the decorator either:
Did you use 'template_filter' as a decorator without parentheses? You need to call it with 'template_filter()'
This is caught by type checkers, but not everybody type checks their Python and the error message is less obvious:
I've had a look at the relevant code, and I'd be happy to provide a patch if you think this is a useful change.
Environment
The text was updated successfully, but these errors were encountered: