You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot type Flask view functions return types in the natural way #7218
If you need to return more than one type from a given view function, mypy gives you an error (as of python/typeshed#3003). Here's a small reproducing case, which also shows why we might want to do this:
fromtypingimportUnionfromflaskimportFlask, redirect, render_templatefromwerkzeugimportResponseapp=Flask(__name__)
@app.route("/")defindex() ->Union[Response, str]:
ifTrue:
returnredirect("/foo") # is a Responsereturnrender_template("index.html") # is a str
When we run mypy, we see:
$ mypy flaskapp.py
flaskapp.py:9: error: Value of type variable "_VT" of function cannot be "Callable[[], Union[Response, str]]"
I've logged this here, as opposed to in typeshed, as I think that mypy ought to be able to figure out from the ViewFunc definition that my particular view function is one of two valid values there, but apparently it can't.
Reacted by edA-qa mort-ora-y, Raf, Victor Schröder, Garrett Reynolds and Gleb Ignatev
Activity
ilevkivskyi commentedon Jul 15, 2019
Duplicate of #7187