Title: Cannot return concrete type within `isinstance` block of generic function · Issue #8354 · python/mypy · GitHub
Open Graph Title: Cannot return concrete type within `isinstance` block of generic function · Issue #8354 · python/mypy
X Title: Cannot return concrete type within `isinstance` block of generic function · Issue #8354 · python/mypy
Description: I'm not sure if this is a bug or a known limitation. I think this is different to the other similar generics vs isinstance issues, but please do point me at another if this is a duplicate. Given some function: from typing import TypeVar ...
Open Graph Description: I'm not sure if this is a bug or a known limitation. I think this is different to the other similar generics vs isinstance issues, but please do point me at another if this is a duplicate. Given so...
X Description: I'm not sure if this is a bug or a known limitation. I think this is different to the other similar generics vs isinstance issues, but please do point me at another if this is a duplicate. Give...
Opengraph URL: https://github.com/python/mypy/issues/8354
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cannot return concrete type within `isinstance` block of generic function","articleBody":"I'm not sure if this is a bug or a known limitation. I think this is different to the other similar generics vs `isinstance` issues, but please do point me at another if this is a duplicate.\r\n\r\nGiven some function:\r\n``` python\r\nfrom typing import TypeVar\r\n\r\nT = TypeVar('T')\r\n\r\ndef foo(val: T) -> T:\r\n if isinstance(val, str):\r\n return \"abc\"\r\n return val\r\n```\r\n\r\nI would hope that `mypy` can tell that, within the `isinstance` block, `T` can only be `str` (since that's what `val` is) and thus returning something which is explicitly `str` is fine.\r\n\r\nUnfortunately this case seems to error:\r\n```\r\nfoo.py:5: error: Incompatible return value type (got \"str\", expected \"T\")\r\n```\r\n\r\nI realise this construct looks a bit odd in the cut-down above -- my actual use-case involves walking nested list and dictionary structures to modify values, but ends up with what I believe is the same error. Just to give a sense of that a slightly larger but still very cut-down version would be:\r\n\r\n``` python\r\nfrom typing import Any, List, TypeVar, Union\r\n\r\nT = TypeVar('T', bound=Union[str, List[Any], int])\r\n\r\ndef do_expansion(value: T) -> T:\r\n if isinstance(value, str):\r\n return value.upper() # just using .upper() as an example\r\n elif isinstance(value, list):\r\n return [do_expansion(x) for x in value]\r\n else:\r\n return value\r\n```\r\n\r\nI realise I could use overloads to achieve a similar effect for consumers of the function, however I would also like to be able to type the implementation and get checking there.\r\n\r\nIn case it's useful, I'm using `mypy` version `0.761` (latest at the time of writing) on Python 3.5.2.","author":{"url":"https://github.com/PeterJCLaw","@type":"Person","name":"PeterJCLaw"},"datePublished":"2020-02-02T01:20:39Z","url":"https://github.com/python/mypy/issues/8354"}
| 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:948a8160-ad01-9b9d-bfe0-72a68083a905 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8822:2DCAE8:2407F6F:3123906:6B003450 |
| html-safe-nonce | d77e63f77f216ef76b756b6272db789f74686e0269b992d428674581a995c74d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4ODIyOjJEQ0FFODoyNDA3RjZGOjMxMjM5MDY6NkIwMDM0NTAiLCJ2aXNpdG9yX2lkIjoiNTYyMjIwMTg2MjU4NjM4MTM5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 73c71a32c3e211e4f20f3ddd081d2454b08332189f8c03eb389deb0df08320f3 |
| hovercard-subject-tag | issue:558625163 |
| 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/8354/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d9231692d89dbc27404a00e735510fe4c86b945d6ca7bab2b416abc7fed7c355/python/mypy/issues/8354 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d9231692d89dbc27404a00e735510fe4c86b945d6ca7bab2b416abc7fed7c355/python/mypy/issues/8354 |
| og:image:alt | I'm not sure if this is a bug or a known limitation. I think this is different to the other similar generics vs isinstance issues, but please do point me at another if this is a duplicate. Given so... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | PeterJCLaw |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width