Title: Type narrowing of 'TypeVar' causes '[return-value]' error · Issue #15151 · python/mypy · GitHub
Open Graph Title: Type narrowing of 'TypeVar' causes '[return-value]' error · Issue #15151 · python/mypy
X Title: Type narrowing of 'TypeVar' causes '[return-value]' error · Issue #15151 · python/mypy
Description: Bug Report mypy reports a type incompatibility error when the TypeVars are narrowed within a generic function but the generic arg is not changed in any way. To Reproduce from typing import TypeVar T = TypeVar("T") def foo(v: T) -> T: if ...
Open Graph Description: Bug Report mypy reports a type incompatibility error when the TypeVars are narrowed within a generic function but the generic arg is not changed in any way. To Reproduce from typing import TypeVar ...
X Description: Bug Report mypy reports a type incompatibility error when the TypeVars are narrowed within a generic function but the generic arg is not changed in any way. To Reproduce from typing import TypeVar ...
Opengraph URL: https://github.com/python/mypy/issues/15151
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type narrowing of 'TypeVar' causes '[return-value]' error","articleBody":"**Bug Report**\r\n\r\n`mypy` reports a type incompatibility error when the `TypeVar`s are narrowed within a generic function but the generic arg is not changed in any way.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom typing import TypeVar\r\n\r\nT = TypeVar(\"T\")\r\n\r\n\r\ndef foo(v: T) -> T:\r\n if isinstance(v, str):\r\n return v # error: Incompatible return value type (got \"str\", expected \"T\") [return-value]\r\n return v\r\n```\r\n\r\n…and a [mypy-play](https://mypy-play.net/?mypy=latest&python=3.10&gist=ce1a6c14cefdf41c3c5a372560a5e8c2) link.\r\n\r\n**Expected Behavior**\r\n\r\nNo errors are thrown since the variable wasn't patched in any way (e.g., it was not returned as `str(v)`).\r\n\r\n**Actual Behavior**\r\n\r\n```\r\nmain.py:8: error: Incompatible return value type (got \"str\", expected \"T\") [return-value]\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.2.0\r\n- Mypy command-line flags: None\r\n- Mypy configuration options from `mypy.ini` (and other config files): None\r\n- Python version used: 3.10\r\n","author":{"url":"https://github.com/nikitagashkov","@type":"Person","name":"nikitagashkov"},"datePublished":"2023-04-27T09:48:05Z","url":"https://github.com/python/mypy/issues/15151"}
| 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:37a4d13b-d72f-2ef8-244a-3cffefb78edf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B232:390AA2:23FE580:310CCD6:6B002FD6 |
| html-safe-nonce | 34d39d1bac54e52148a58b2e0242c00c3e86050250e95be6f53cc0a2767a8e6b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjMyOjM5MEFBMjoyM0ZFNTgwOjMxMENDRDY6NkIwMDJGRDYiLCJ2aXNpdG9yX2lkIjoiMzEzNDkxOTUxMDQzNjM2MDE1MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 22fed5832b207c569978c35af12cc4cfc9a6704c451fb3f69c52c4ccd10edf68 |
| hovercard-subject-tag | issue:1686504456 |
| 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/15151/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e949c25e48abd7bd026aa39816067a7482a06ae2f998ebc08faa258829829f9b/python/mypy/issues/15151 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e949c25e48abd7bd026aa39816067a7482a06ae2f998ebc08faa258829829f9b/python/mypy/issues/15151 |
| og:image:alt | Bug Report mypy reports a type incompatibility error when the TypeVars are narrowed within a generic function but the generic arg is not changed in any way. To Reproduce from typing import TypeVar ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nikitagashkov |
| 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