Skip to content

Enhance ASGI scope typing and add HTTP event types#2638

Closed
MustafaNazir11 wants to merge 2 commits into
falconry:masterfrom
MustafaNazir11:master
Closed

Enhance ASGI scope typing and add HTTP event types#2638
MustafaNazir11 wants to merge 2 commits into
falconry:masterfrom
MustafaNazir11:master

Conversation

@MustafaNazir11
Copy link
Copy Markdown

@MustafaNazir11 MustafaNazir11 commented Apr 26, 2026

Summary of Changes

Introduce TypedDict definitions for ASGI HTTP event messages.

  • Add TypedDicts for HTTP request, response start, response body, and disconnect events
  • Provide structured typing for ASGI event payloads while keeping existing aliases unchanged

This improves type clarity and lays the groundwork for stricter typing in the future without affecting runtime behavior.

Related Issues

Relates to #2628

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 towncrier, 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
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e9e1d4b) to head (3a5404e).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2638   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           64        64           
  Lines         7976      8009   +33     
  Branches      1102      1102           
=========================================
+ Hits          7976      8009   +33     

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Copy Markdown
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

Hi @MustafaNazir11, and thanks for this PR.

The main goal of these improved annotations was to get rid of typing ignores, however, you haven't removed any.
See also inline comments.

You've checked that you ran tox yourself, but that is obviously not true (see the failing CI).

Taking all these points into account, I believe it would be more efficient to restart this PR from scratch.

Comment thread falcon/asgi/app.py

req = self._request_type(
scope, receive, first_event=first_event, options=self.req_options
cast(dict[str, Any], scope), receive, first_event=first_event, options=self.req_options
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.

This is the hottest code path in Falcon, we simply cannot afford cast(...) here.

Comment thread falcon/asgi_spec.py
WS_DISCONNECT = 'websocket.disconnect'
WS_CLOSE = 'websocket.close'

class HTTPRequestEvent(TypedDict, total=False):
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.

I see how these could belong to asgi_spec.py too, but let's colocate these aliases in falcon/_typing.py for now.

@vytas7 vytas7 closed this Apr 27, 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