Title: Cannot type Flask view functions return types in the natural way · Issue #7218 · python/mypy · GitHub
Open Graph Title: Cannot type Flask view functions return types in the natural way · Issue #7218 · python/mypy
X Title: Cannot type Flask view functions return types in the natural way · Issue #7218 · python/mypy
Description: Flask handles a variety of return types from view functions -- for instance, you can return a Response object, a str, or a tuple of str and int, etc. These are documented at https://github.com/python/typeshed/blob/master/third_party/2and...
Open Graph Description: Flask handles a variety of return types from view functions -- for instance, you can return a Response object, a str, or a tuple of str and int, etc. These are documented at https://github.com/pyth...
X Description: Flask handles a variety of return types from view functions -- for instance, you can return a Response object, a str, or a tuple of str and int, etc. These are documented at https://github.com/pyth...
Opengraph URL: https://github.com/python/mypy/issues/7218
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cannot type Flask view functions return types in the natural way","articleBody":"Flask handles a variety of return types from view functions -- for instance, you can return a `Response` object, a `str`, or a tuple of `str` and `int`, etc. These are documented at https://github.com/python/typeshed/blob/master/third_party/2and3/flask/app.pyi#L48-L58.\r\n\r\nIf 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:\r\n\r\n```python\r\nfrom typing import Union\r\nfrom flask import Flask, redirect, render_template\r\nfrom werkzeug import Response\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route(\"/\")\r\ndef index() -> Union[Response, str]:\r\n if True:\r\n return redirect(\"/foo\") # is a Response\r\n return render_template(\"index.html\") # is a str\r\n```\r\n\r\nWhen we run mypy, we see:\r\n\r\n```\r\n$ mypy flaskapp.py\r\nflaskapp.py:9: error: Value of type variable \"_VT\" of function cannot be \"Callable[[], Union[Response, str]]\"\r\n```\r\n\r\nI'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.\r\n","author":{"url":"https://github.com/dcrosta","@type":"Person","name":"dcrosta"},"datePublished":"2019-07-15T22:12:17Z","url":"https://github.com/python/mypy/issues/7218"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:96b74ab2-ce4f-c632-3b42-1a0def1075af |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9860:2B41BC:E3606C:1314856:6B00570D |
| html-safe-nonce | 6402820ad4d579b3241f381a38e7f28824b23bdf74dcd9ed55ba9e53ebcf5ac3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5ODYwOjJCNDFCQzpFMzYwNkM6MTMxNDg1Njo2QjAwNTcwRCIsInZpc2l0b3JfaWQiOiIxOTI0NDUyNzU3MzY5MjE4NzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a94d37c609bae2772b8b84b05c6988f3ebf694bf13ed5271a142e6112e341dfa |
| hovercard-subject-tag | issue:468358394 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/7218/issue_layout |
| twitter:image | https://opengraph.githubassets.com/887b29a832cd95bb036385151127b4be0955039552014c2ee383f5ab46c90f62/python/mypy/issues/7218 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/887b29a832cd95bb036385151127b4be0955039552014c2ee383f5ab46c90f62/python/mypy/issues/7218 |
| og:image:alt | Flask handles a variety of return types from view functions -- for instance, you can return a Response object, a str, or a tuple of str and int, etc. These are documented at https://github.com/pyth... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dcrosta |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width