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

typing: add middleware protocols #2390

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jkmnt
Copy link

@jkmnt jkmnt commented Oct 26, 2024

Summary of Changes

Typed the middleware classes with union of protocols.
Middleware class may conform to any protocol of the union to pass the type check

It's not perfect due the nature of python's static duck typing, but better than the plain object ) Class may have correct signature for process_request(), but wrong signature for process_response(). It would be accepted since MiddlewareWithProcessRequest says nothing about the process_response.

But if user explicitly states implemented protocols, then the typecheker will ensure strict conformance.

class MyMiddleware(MiddlewareWithProcessRequest, MiddlewareWithProcessResponse):
    def process_request(...): ... # ok, pass
    def process_responce(...):  # won't pass (typo). process_response is required 

The protocols definitions are in the private falcon._typing module now. Should they be moved to the public falcon.typing to support this usecase ?

The protocols names are long and noisy. Use some abbreviations ?

Related Issues

#2372

Copy link

codecov bot commented Oct 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (6d7a45b) to head (bd497af).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2390   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           64        64           
  Lines         7726      7730    +4     
  Branches      1071      1071           
=========================================
+ Hits          7726      7730    +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jkmnt jkmnt marked this pull request as ready for review October 29, 2024 11:11
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.

1 participant