I'm working with flask and werkzeug, and trying to figure out how to improve some of my typing (mypy --disallow-untyped-defs). When I look at the types from typeshed for wsgiref, and how werkzeug handles this, I find things that are a bit confusing. This question is only somewhat flask/werkzeug specific, it mostly pertains to typeshed -- I'm happy to repost or cross-post in werkzeug if that seems better.
Here's something similar to what I have today, partially annotated:
def make_flask_app() -> flask.Flask: ...
def apply_middlewares(app: Flask):
profdir = os.getenv("WSGI_PROFILE_DIR")
if profdir:
return werkzeug.middleware.profiler.ProfilerMiddleware(app, profile_dir=profdir)
return app
def make_app():
app = make_flask_app()
return apply_middewares(app)
So what is the return type of apply_middlewares?
Looking around, it seems like this is _typeshed.wsgi.WSGIApplication. But that's in _typeshed -- is it safe to import it at type-checking time? Could it be renamed in the future? werkzeug already does this:
if TYPE_CHECKING:
from _typeshed.wsgi import WSGIApplication
Is this safe for me to do too? I'm going to probably do this for today but it would be great to either know that the name is stable or to find a better way.
Concretely, the type above is just Flask | ProfilerMiddleware today. But any other middleware will (spuriously) be flagged as changing the type if the code changes. So WSGIApplication is really much preferable.
I'd really like to see WSGIApplication (and some of the other stuff, like StartResponse) available as types that I can use at typing time. Does this require additions of some of these types as protocols to the stdlib? I'm more than happy to work on this, if it's not already in progress.
I'm working with
flaskandwerkzeug, and trying to figure out how to improve some of my typing (mypy --disallow-untyped-defs). When I look at the types from typeshed for wsgiref, and how werkzeug handles this, I find things that are a bit confusing. This question is only somewhat flask/werkzeug specific, it mostly pertains to typeshed -- I'm happy to repost or cross-post in werkzeug if that seems better.Here's something similar to what I have today, partially annotated:
So what is the return type of
apply_middlewares?Looking around, it seems like this is
_typeshed.wsgi.WSGIApplication. But that's in_typeshed-- is it safe to import it at type-checking time? Could it be renamed in the future?werkzeugalready does this:Is this safe for me to do too? I'm going to probably do this for today but it would be great to either know that the name is stable or to find a better way.
Concretely, the type above is just
Flask | ProfilerMiddlewaretoday. But any other middleware will (spuriously) be flagged as changing the type if the code changes. SoWSGIApplicationis really much preferable.I'd really like to see
WSGIApplication(and some of the other stuff, likeStartResponse) available as types that I can use at typing time. Does this require additions of some of these types as protocols to the stdlib? I'm more than happy to work on this, if it's not already in progress.