Repository navigation
docs(recipes): annotate with type hints - #2774
Conversation
Also return the result of start_response() from the wrapper, as required by PEP 3333 (the write callable was previously dropped).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2774 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 64 64
Lines 8121 8121
Branches 1131 1131
=========================================
Hits 8121 8121 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi @ericareg, and thanks for this PR. However, I'm inclined to close it for now due to multiple reasons:
|
|
Hi @vytas7, thanks for the feedback, and sorry about that. I used an AI assistant for the type hints and the description, and I forgot to disclose it, which is my fault. Understood about the template and the lack of automated checks. Thanks for taking the time to explain. |
No, worries, we can simply reopen this one. |
vytas7
left a comment
There was a problem hiding this comment.
What I wrote in the message prematurely closing this still applies.
It is great that we have added these annotations, but we need to extend our type checking gates to actually check these annotations, otherwise they can easily drift away from the reality.
In addition, probably we should not require from __future__ import annotations in these snippets, that adds noise.
No imports from _typeshed either, again, although type hints in the recipes are helpful, we don't want too much noise taking focus from what they illustrate.
|
Thanks for the review, @vytas7. I'll extend the type checking to cover examples/recipes/, and remove the future and _typeshed imports from the snippets. Sorry again for not disclosing the AI assistance earlier. I'll update the PR description with the full template. |
|
@vytas7 I've added a mypy check for |
vytas7
left a comment
There was a problem hiding this comment.
Thanks once again, this looks almost ready now. 👍
See a couple of minor issues that I would like to iron out before merging (commented inline).
| @@ -1,16 +1,19 @@ | |||
| import contextvars | |||
| from typing import Optional | |||
There was a problem hiding this comment.
Let's not use Optional anywhere here in the recipes. Simply write str | None.
| @@ -1,21 +1,29 @@ | |||
| import json | |||
| from typing import Optional | |||
There was a problem hiding this comment.
No Optional here either. Let's simply use str | None, etc.
| ["mypy", "tests", "--allow-untyped-calls", "--allow-untyped-defs", "--no-check-untyped-defs"], | ||
| # header_name_case_app.py is excluded since it intentionally contains a | ||
| # placeholder for CustomHeadersMiddleware (see the recipe's docs). | ||
| [ |
There was a problem hiding this comment.
Let's condense the command line to a single line, even though it is a bit suboptimal when the lines get very long.
| # placeholder for CustomHeadersMiddleware (see the recipe's docs). | ||
| [ | ||
| "mypy", "examples/recipes", | ||
| "--allow-untyped-calls", "--allow-untyped-defs", "--no-check-untyped-defs", |
There was a problem hiding this comment.
Why do we need --allow-untyped-calls & --allow-untyped-defs, if we already exempt specific files?
|
Something is messed up on GH's side, actions error out... hopefully when you push a new update it gets fixed. |
|
@vytas7 Thanks for the review! I've pushed the changes:
One question: without |
vytas7
left a comment
There was a problem hiding this comment.
Regarding test_recipes.py on 3.9 -- I think we can skip the classes in question if on 3.9. There is already precedent in the same file for the msgspec recipe which uses more modern annotations.
(We are planning to drop 3.9 support in the nearest releases.)
|
Thanks @vytas7! I’ve skipped those test classes on Python 3.9 with pytest.mark.skipif, following the pattern in tests/test_status_codes.py. I also had to skip the msgspec recipe tests, since this branch changed its Optional to str | None as well, so the existing skip didn’t cover 3.9 anymore. |
Summary of Changes
This adds type hints to the recipe examples in
examples/recipes/, which are not covered by #2709.The recipes are now type-checked with mypy in the
mypy_teststox environment, so the annotations can't drift.header_name_case_app.pyis excluded because it uses a placeholder class on purpose.While typing
header_name_case_mw.py, mypy showed thatstart_response_wrapperdid not return the result ofstart_response(), so the wrapper now returns it.I used Claude Code to help with the type hints and the description. I reviewed the changes and ran the tests locally.
Related Issues
Part of #1820
Pull Request Checklist
This is just a reminder about the most common mistakes. Please make sure that you tick all appropriate boxes. Reading our contribution guide at least once will save you a few review cycles!
If an item doesn't apply to your pull request, check it anyway to make it apparent that there's nothing to do.
tox.docs/.docs/.versionadded,versionchanged, ordeprecateddirectives.docs/_newsfragments/, with the file name format{issue_number}.{fragment_type}.rst. (Runtox -e docs, and inspectdocs/_build/html/changes/in the browser to ensure it renders correctly.)If you have any questions to any of the points above, just submit and ask! This checklist is here to help you, not to deter you from contributing!
PR template inspired by the attrs project.