Skip to content

feat: support call template_filter without parens #5736

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kadai0308
Copy link

@kadai0308 kadai0308 commented May 17, 2025

This PR enhances the App.template_filter decorator to support:

  • @app.template_filter (without parentheses)

It also ensures that the original usage remains fully supported, with no breaking changes:

  • @app.template_filter()
  • @app.template_filter(name="...")
  • @app.template_filter("...")

I’m not fully confident in the solution, so I’d like to confirm it first. If it’s good, I’ll apply the same change to template_global and template_test.


I considered the following implementation options:

  1. Rename the input parameter to func_or_name to better reflect the logic of how the input is interpreted:
def template_filter(self, func_or_name: t.Callable[..., t.Any] | str | None = None):

However, this would be a breaking change for calls like @app.template_filter(name="..."), so I decided to keep the original parameter name for backward compatibility.

  1. Make func_or_name a positional-only parameter and keep name as keyword-only:
def template_filter(self, func_or_name: t.Callable[..., t.Any] | str | None = None, /, *, name: str | None = None):

This approach is type-safe, but it introduces the awkward case where both func_or_name and name are provided at the same time. It could also be confusing for users reading the function signature.

So eventually, I decided to keep the original function parameter and distinguish the usage using if callable(name):. I’d like to discuss whether there’s a better approach, and I’m open to making improvements based on feedback and suggestions.


fixes #5729


  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.

@kadai0308 kadai0308 force-pushed the support-template-filter-no-parens branch from 4176ca0 to 0a52b6b Compare May 17, 2025 04:45
@kadai0308
Copy link
Author

Hi @RonnyPfannschmidt @davidism
This is a draft PR because I'm not entirely sure if this is the right approach.
I'd really appreciate it if you could take a look when you have time.
Please let me know if there's anything I should improve or change. Thank you!

@kadai0308
Copy link
Author

Sorry for didn't aware the contriubing rule, just close the PR.

@kadai0308 kadai0308 closed this May 22, 2025
@davidism davidism reopened this May 22, 2025
@davidism
Copy link
Member

You're fine, this is the direction I want to go and you were first anyway.

@kadai0308
Copy link
Author

You're fine, this is the direction I want to go and you were first anyway.

Thanks for sharing this; I'll go ahead and process the PR.

@kadai0308 kadai0308 force-pushed the support-template-filter-no-parens branch from a99fa25 to 1f027e4 Compare May 26, 2025 09:23
@kadai0308 kadai0308 marked this pull request as ready for review May 26, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The template_filter decorator doesn't work if you don't pass an argument
2 participants