Skip to content

docs(recipes): annotate with type hints - #2774

Merged
vytas7 merged 18 commits into
falconry:masterfrom
ericareg:type-hints-recipes
Nov 20, 2026
Merged

vytas7 merged 18 commits into
falconry:masterfrom
ericareg:type-hints-recipes

Conversation

@ericareg

@ericareg ericareg commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

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_tests tox environment, so the annotations can't drift. header_name_case_app.py is excluded because it uses a placeholder class on purpose.

While typing header_name_case_mw.py, mypy showed that start_response_wrapper did not return the result of start_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.

  • Applied changes to both WSGI and ASGI code paths and interfaces (where applicable).
  • Added tests for changed code.
  • Performed automated tests and code quality checks by running tox.
  • Prefixed code comments with GitHub nick and an appropriate prefix.
  • Coding style is consistent with the rest of the framework.
  • Updated documentation for changed code.
    • Added docstrings for any new classes, functions, or modules.
    • Updated docstrings for any modifications to existing code.
    • Updated both WSGI and ASGI docs (where applicable).
    • Added references to new classes, functions, or modules to the relevant RST file under docs/.
    • Updated all relevant supporting documentation files under docs/.
    • A copyright notice is included at the top of any new modules (using your own name or the name of your organization).
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
  • Changes (and possible deprecations) have towncrier news fragments under docs/_newsfragments/, with the file name format {issue_number}.{fragment_type}.rst. (Run tox -e docs, and inspect docs/_build/html/changes/ in the browser to ensure it renders correctly.)
  • LLM output, if any, has been carefully reviewed and tested by a human developer. (See also: Use of LLMs ("AI").)

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.

@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (71751bb) to head (8e64c1b).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vytas7 vytas7 changed the title Type hints recipes docs(recipes): add type hints Nov 8, 2026
@vytas7 vytas7 changed the title docs(recipes): add type hints docs(recipes): annotate with type hints Nov 8, 2026
@vytas7

vytas7 commented Nov 8, 2026

Copy link
Copy Markdown
Member

Hi @ericareg, and thanks for this PR. However, I'm inclined to close it for now due to multiple reasons:

  • Undisclosed AI assistance (at least used to write the description... don't do that).
  • There is no automated checking of these typing improvements.
  • Rewritten pull request template, potentially by an LLM. Just check the relevant boxes in the unabridged template.

@vytas7 vytas7 closed this Nov 8, 2026
@ericareg

ericareg commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

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.

@vytas7

vytas7 commented Nov 8, 2026

Copy link
Copy Markdown
Member

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 vytas7 reopened this Nov 8, 2026

@vytas7 vytas7 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@ericareg

ericareg commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

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.

@ericareg

ericareg commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

@vytas7 I've added a mypy check for examples/recipes/ to the mypy_tests environment, and removed the __future__ and _typeshed imports from the recipes. I also updated the PR description with the full template.

@ericareg
ericareg requested a review from vytas7 November 9, 2026 12:13

@vytas7 vytas7 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

Comment thread examples/recipes/header_name_case_mw.py Resolved
Comment thread examples/recipes/request_id_context.py Outdated
@@ -1,16 +1,19 @@
import contextvars
from typing import Optional

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's not use Optional anywhere here in the recipes. Simply write str | None.

Comment thread examples/recipes/pretty_json_main.py Outdated
@@ -1,21 +1,29 @@
import json
from typing import Optional

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No Optional here either. Let's simply use str | None, etc.

Comment thread tox.toml Outdated
["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).
[

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's condense the command line to a single line, even though it is a bit suboptimal when the lines get very long.

Comment thread tox.toml Outdated
# placeholder for CustomHeadersMiddleware (see the recipe's docs).
[
"mypy", "examples/recipes",
"--allow-untyped-calls", "--allow-untyped-defs", "--no-check-untyped-defs",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need --allow-untyped-calls & --allow-untyped-defs, if we already exempt specific files?

@vytas7

vytas7 commented Nov 15, 2026

Copy link
Copy Markdown
Member

Something is messed up on GH's side, actions error out... hopefully when you push a new update it gets fixed.

@ericareg

Copy link
Copy Markdown
Contributor Author

@vytas7 Thanks for the review! I've pushed the changes:

  • Reverted header_name_case_mw.py except for the return fix, and excluded it from the check.
  • Replaced Optional with X | None in the recipes.
  • Condensed the mypy command to a single line.
  • About --allow-untyped-calls and --allow-untyped-defs: they're not needed. They were only there for the files without annotations, so I excluded those two (header_name_case_mw.py and plain_text_main.py) and dropped the flags. The other 18 recipes pass with the strict settings from pyproject.toml.

One question: without from __future__ import annotations, str | None in function signatures fails on Python 3.9 at import time. tests/test_recipes.py imports pretty_json_main.py and request_id_context.py, so the py39 job will likely fail. Would you prefer adding the __future__ import to just those two files, or keeping Optional there?

@ericareg
ericareg requested a review from vytas7 November 16, 2026 12:02

@vytas7 vytas7 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.)

@ericareg

Copy link
Copy Markdown
Contributor Author

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.

@vytas7 vytas7 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Obrigado, @ericareg. LGTM now.

@vytas7
vytas7 merged commit 4d0a8de into falconry:master Nov 20, 2026
36 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.

2 participants