Replies: 3 comments 4 replies
-
Hi, Thanks for testing the typing! Overall we are likely looking for a way of making handler covariant (I think) on request/response, accepting request or any subclass, same for response. It seems we can use type vars for this. I will try looking how it would become |
Beta Was this translation helpful? Give feedback.
-
Thanks for implementing the typing! I'll try to explore this approach with typevar bound to Request and generic. IMHO the 99% of falcon typing would require no changes since MyRequest conforms to the Request interface expected by falcon core. Only the stored callbacks should be typed differently. The most important thing here is the context defined inside the Request subclass. In our case Context is quite rich (auth, session info etc) and fully typed - it was the main reason for using the custom request at all. |
Beta Was this translation helpful? Give feedback.
-
Thanks for reporting this @jkmnt! I will add a note for the release candidate, since we cannot really find any elegant solution or workaround for the time being. One can type Alternatively, you could do something along the below lines... def my_error_handler(req: falcon.Request, ...):
my_req = cast(MyRequest, req)
# Alternatively, cast only the context
context = cast(MyContext, req.context)
# Handle...
# <...> But this ain't pretty, and it requires synthetic effort on your side. Moreover, it can be plain wrong if you manage to get another type of request (you shoudn't if you use a single app though). I have otherwise created an issue [from this discussion] to investigate this more in depth: #2372, but as said, Falcon 4.0.0 will have to go out as-is wrt this aspect. 👿 |
Beta Was this translation helpful? Give feedback.
-
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.Beta Was this translation helpful? Give feedback.
All reactions