encode / starlette

The little ASGI framework that shines. 🌟
https://www.starlette.io/
BSD 3-Clause "New" or "Revised" License
10.31k stars 948 forks source link

Make _MiddlewareFactory compatible with Callable #2768

Closed eltoder closed 3 days ago

eltoder commented 3 days ago

Summary

Make app a positional-only argument so that a Callable[[ASGIApp, ...], ASGIApp] can be passed in place of _MiddlewareFactory.

We can also consider making _MiddlewareFactory and moving it into starlette.types. This will make it easier to type arguments for add_middleware().

Checklist

adriangb commented 3 days ago

We can also consider making _MiddlewareFactory and moving it into starlette.types. This will make it easier to type arguments for add_middleware()

This part I did not understand

eltoder commented 3 days ago

@adriangb thank you for merging!

This part I did not understand

Sorry, missed a word:

We can also consider making _MiddlewareFactory public.

The rationale is that add_middleware() is a public function and occasionally I need to refer to the type of its argument. For example, I have a function that returns which middleware to use based on environment. I need to write its return type. With this PR I can write it as Callalble[], which is nice. But it may be even more clear and explicit to write it as MiddlewareFactory. However, currently the latter is not public. Here's a sketch of how this can be used: https://mypy-play.net/?mypy=1.13.0&python=3.13&gist=e0652be937c8cfc1d56a5d4dafa0bd89