Skip to content

Add type annotation for filters#2141

Open
Gabgobie wants to merge 3 commits intopallets:stablefrom
Gabgobie:stable
Open

Add type annotation for filters#2141
Gabgobie wants to merge 3 commits intopallets:stablefrom
Gabgobie:stable

Conversation

@Gabgobie
Copy link
Copy Markdown

A minimal PR adding a type annotation to FILTERS as discussed in #2120

The change was tested in my venv and fixed my project's issues.
You should be aware that it causes the following Pylance warning in the jinja codebase:

Type variable "F" has no meaning in this context Pylance(reportGeneralTypeIssues)

Looking into this a little more, my understanding is that TypeVars should really only be used for generic function and class declarations. microsoft/pyright#9826 (reply in thread)

A type alias may be more appropriate in line 44.

F = t.TypeVar("F", bound=t.Callable[..., t.Any])

FilterFunction = t.Callable[..., t.Any]

Additionally, I was unable to find any use of F from line 44 and ran pytest with the line commented out. I am fairly certain that it was dead code until now.


#2135
#2139

The above PRs tried to address #2120 in a different way.

fixes #2120

Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.

I don't think I can test this without adding a Pylance action, which would fail anyways since jinja is not written with type checkers in mind.

Add or update relevant docs, in the docs folder and in code.

We could be more explicit about the requirements in Custom Filters but this doesn't really change anything about them. Python type annotations usually serve as documentation only.

Other considerations

Aside from annotating FILTERS, this same annotation could be applied to

self.filters = DEFAULT_FILTERS.copy()

During my search for use of line 44, I noticed that the same definition of F is declared five times. There may be a semantic difference between the otherwise identical declarations.

@Gabgobie
Copy link
Copy Markdown
Author

The error from Tests / 3.7 (pull_request) seems unrelated to me:

Error: The version '3.7' with architecture 'x64' was not found for Ubuntu 24.04.

I'll use the older t.Dict[str, F] annotation to appease typing and 3.8.

@Gabgobie
Copy link
Copy Markdown
Author

I think the typing error is basically what I discussed above about the use of TypeVar.

The 3.7 error I really don't get. Looking at the manifest, versions 3.7.1-17 are available.

@davidism
Copy link
Copy Markdown
Member

Don't worry about < 3.10.

You don't need to use the type var F, you're right that it doesn't make sense there. The other PRs were closed because they were LLM junk, not because they were using a particular solution vs your suggestion. I'm fine with whatever you end up figuring out the answer is.

@davidism
Copy link
Copy Markdown
Member

Also note that TESTS in tests.py needs the same fix.

@davidism davidism changed the title Add type annotation for FITLERS Add type annotation for filters Feb 25, 2026
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.

2 participants