Add type annotation for filters#2141
Conversation
|
The error from
I'll use the older |
|
I think the typing error is basically what I discussed above about the use of The 3.7 error I really don't get. Looking at the manifest, versions 3.7.1-17 are available. |
|
Don't worry about < 3.10. You don't need to use the type var |
|
Also note that |
|
This PR is getting to be too unwieldy for a tiny typing change. Please don't update every single requirement here. Make a PR branching off main, and just make the annotation change. I'll deal with the rest. |
|
Agreed though main wouldn't help, see #2141 (comment) Let's roll the latest changes back and go with |
A minimal PR adding a type annotation to
FILTERSas discussed in #2120The 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:
Looking into this a little more, my understanding is that
TypeVars should really only be used for genericfunctionandclassdeclarations. microsoft/pyright#9826 (reply in thread)A type alias may be more appropriate in line 44.
jinja/src/jinja2/filters.py
Line 44 in 5ef7011
Additionally, I was unable to find any use of
Ffrom line 44 and ranpytestwith 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
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.
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 tojinja/src/jinja2/environment.py
Line 352 in 5ef7011
During my search for use of line 44, I noticed that the same definition of
Fis declared five times. There may be a semantic difference between the otherwise identical declarations.