-
-
Notifications
You must be signed in to change notification settings - Fork 946
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
Improve typing of custom req/resp type #2372
Comments
Hi. My POC on the issue here: https://github.com/jkmnt/falcon/tree/generics_poc I typed wsgi App class with generics. The concrete types are seems to be correctly inferred. I agree it's should be deferred for 4.1+. Too much changes for 4.0 ) BTW, there was some (missed by mypy) errors reported by pyright. I think I'll try to fix some and submit a separate PR. |
that would be great. Feel free to also open an issue when you do that. |
I have just created an issue to track the Pyright gate: #2373. And echoing @CaselIT, re PR, your help is really appreciated, because we are no huge experts of typing (OK, @CaselIT maybe is, but I am definitely not 😅). Edit: and as to typing extensions, I don't want Falcon to depend on any 3rd party runtime packages for the majority of use cases. We can add it as a conditional requirement for Python versions older than 3. |
Another thing related to the typing: I suggest something like class MwWithReqHandler(Protocol):
def process_request(self, req: Request, resp: Response) -> None: ...
class MwWithRespHandler(Protocol):
def process_response(self, req: Request, resp: Response, resource: object, req_succeeded: bool) -> None: ...
class MwWithProcessHandler(Protocol):
def process_resource(self, req: Request, resp: Response, resource: object, params: dict[str, Any]) -> None: ...
Middleware = Union[MwWithReqHandler, MwWithRespHandler, MwWithProcessHandler] This is not perfect but better than nothing. The bad thing is that it's enough to have one conforming (with others nonconformin) methods to pass the type check. The good thing: the user may inherit it's middleware class from these protocols (kind of abc but w/o runtime overhead) and get full typesafety. class MyMiddleware(MwWithReqHandler, MwWithRespHandler):
def process_req(...)
def process_resp(...) |
I thought about it while doing that part, but if we type it as If not then that's for sure an option! |
No afaik its enough to satisfy just one protocol of the union. |
Seems like a good option then! |
it probably makes sense to open a new issue with it. If you can work on a PR it would be great! |
Yes, let's divide and conquer these typing improvements. Like improvement of the middleware protocols could be one issue/PR 🙂 |
Ok, I'll draft the PR then. |
I managed to type this all up at #2397 before looking for this, but looks like I came to the same conclusions 🙈 Thanks! |
Discussed in #2370
Originally posted by jkmnt October 14, 2024
Testing f4 beta with the our falcon-powered app. No runtime issues so far, but a few errors from the typechecker.
The
app.add_error_handler
andapp.add_error_handler
expects the callables with signature of [falcon.Request, falcon.Response, ...etc]. Our app is using the custom Request class:Maybe the request/response types should be typed as generics bound to the base type ?
Something like
The same issue is with
before
/after
hook adders . Yes, it's way harder to type since they know nothing about the app class.The text was updated successfully, but these errors were encountered: