Skip to content
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

Ignore mypy issue on jinja2.contextfunction function #1554

Merged
merged 4 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pytest==7.0.1
trio==0.19.0

# Documentation
mkdocs==1.2.3
mkdocs==1.2.4
mkdocs-material==8.1.3
mkautodoc==0.1.0

Expand Down
2 changes: 1 addition & 1 deletion starlette/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
if hasattr(jinja2, "pass_context"):
pass_context = jinja2.pass_context
else: # pragma: nocover
pass_context = jinja2.contextfunction
pass_context = jinja2.contextfunction # type: ignore[attr-defined]
Copy link
Member

@adriangb adriangb Mar 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify why this is necessary? Is Jinja2 untyped / the types are wrong? I'm going to run this locally also, going to take me a sec tho

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attribute is not defined on the latest jinja2 (which is the one installed for Python 3.7+).

The conditional there is because Python 3.6 will have a previous jinja2 version installed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. It's just missing in newer versions of Jinja. And so MyPy complains that it's missing. Seems like a reasonable ignore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use sys.version_info instead?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could... I've added a note on the PR that removes Python 3.6 (#1357) to remove this logic anyway.

Copy link
Member

@adriangb adriangb Mar 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that but it seems like it could lead to issues because the coupling between the Python version and Jinja2 version is coming from our requirements file, so it's pretty much an implementation detail. If there were a sys.version_info but for jinja that is what we would want to do

except ImportError: # pragma: nocover
jinja2 = None # type: ignore

Expand Down