Skip to content

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

Merged
merged 2 commits into from
Aug 19, 2025

Conversation

kadai0308
Copy link
Contributor

@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
Contributor 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
Contributor 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
Contributor 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
@davidism davidism force-pushed the support-template-filter-no-parens branch from 0abda4d to 4f84882 Compare August 19, 2025 19:30
@davidism davidism changed the title feat: support call template_filter without parens support call template_filter without parens Aug 19, 2025
@davidism davidism force-pushed the support-template-filter-no-parens branch from 4f84882 to edebd37 Compare August 19, 2025 19:33
@davidism
Copy link
Member

Thanks for working on this, you were really through with the typing, docs, and tests, as well as noticing the other methods and blueprints as well. I ended up pushing another commit that rewrote the docs entirely, used the callable types consistently, and made the code a bit more concise.

@davidism davidism added this to the 3.2.0 milestone Aug 19, 2025
@davidism davidism merged commit ed1c9e9 into pallets:main Aug 19, 2025
12 checks passed
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