Title: Collapsed Optionals in TypeVar cause weird error messages · Issue #8478 · python/mypy · GitHub
Open Graph Title: Collapsed Optionals in TypeVar cause weird error messages · Issue #8478 · python/mypy
X Title: Collapsed Optionals in TypeVar cause weird error messages · Issue #8478 · python/mypy
Description: At a minimum, I'm reporting a bug in an error message. I have a function, run_tasks, which takes a list of tasks and a worker function, and executes the worker function on almost all the tasks. For some of them, run_tasks will decide to ...
Open Graph Description: At a minimum, I'm reporting a bug in an error message. I have a function, run_tasks, which takes a list of tasks and a worker function, and executes the worker function on almost all the tasks. For...
X Description: At a minimum, I'm reporting a bug in an error message. I have a function, run_tasks, which takes a list of tasks and a worker function, and executes the worker function on almost all the tasks....
Opengraph URL: https://github.com/python/mypy/issues/8478
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Collapsed Optionals in TypeVar cause weird error messages","articleBody":"At a minimum, I'm reporting a bug in an error message. \r\n\r\nI have a function, `run_tasks`, which takes a list of tasks and a worker function, and executes the worker function on almost all the tasks. For some of them, `run_tasks` will decide to not execute the task.\r\n\r\n`run_tasks` returns `List[Optional[Result]]` where `Result` is a TypeVar.\r\n\r\nThis works until the worker function's returns `Optional[x]` where `Result` is now `Optional[x]` and `run_tasks` is asked to return `List[Optional[Optional[x]]]`.\r\n\r\nExample code:\r\n\r\n```python\r\nfrom typing import (\r\n TypeVar,\r\n List,\r\n Callable,\r\n Optional,\r\n)\r\n\r\nTask = TypeVar(\"Task\")\r\nResult = TypeVar(\"Result\")\r\n\r\n\r\ndef run_tasks(\r\n tasks: List[Task], worker_fun: Callable[[Task], Result]\r\n) -> List[Optional[Result]]:\r\n results: List[Optional[Result]] = []\r\n\r\n toggle: bool = True\r\n todo: Task\r\n for todo in tasks:\r\n toggle = not toggle\r\n\r\n if toggle:\r\n work_result: Result\r\n work_result = worker_fun(todo)\r\n results.append(work_result)\r\n else:\r\n results.append(None)\r\n\r\n return results\r\n\r\n\r\ndef test() -> None:\r\n def worker(d: int) -> Optional[int]:\r\n if d % 2 == 0:\r\n return None\r\n else:\r\n return d + 1\r\n\r\n output: List[Optional[Optional[int]]] = run_tasks(\r\n [1, 2, 2, 1], worker,\r\n )\r\n from pprint import pprint\r\n\r\n pprint(output)\r\n assert output == [None, None, None, 2]\r\n\r\n\r\ntest()\r\n```\r\n\r\nI get the error message: `error: Argument 2 to \"run_tasks\" has incompatible type \"Callable[[int], Optional[int]]\"; expected \"Callable[[int], int]\"`\r\n\r\nI understand Python can't really represent `Optional[Optional[int]]`. The problem is in the error message, I think. This code does work correctly, and validates:\r\n\r\n```python\r\ndef test2() -> None:\r\n def worker(d: int) -> int:\r\n return d + 1\r\n\r\n output: List[Optional[int]] = run_tasks(\r\n [1, 2, 2, 1], worker,\r\n )\r\n from pprint import pprint\r\n\r\n pprint(output)\r\n assert output == [None, 3, None, 2]\r\n\r\n\r\ntest2()\r\n```\r\n\r\nand so someone unfamiliar with the intricacies and details may be confused by the type signature being correctly updated in 2 places, but not validating.\r\n\r\nInstead, I sort of expected it to either (a) work, or (b) have an error specific to the case of nested Optional.\r\n\r\n```\r\n$ mypy --version\r\nmypy 0.761\r\n\r\n$ python --version\r\nPython 3.7.6\r\n```\r\n\r\nflags: none.\r\n\r\nMy apologies, but I did not test with mypy from Git master.\r\n","author":{"url":"https://github.com/grahamc","@type":"Person","name":"grahamc"},"datePublished":"2020-03-02T22:26:30Z","url":"https://github.com/python/mypy/issues/8478"}
| 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:f1014d36-3554-658c-7e98-96e9bad20c14 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 82D4:194E10:1C139DE:25F4872:6B0005A5 |
| html-safe-nonce | 7c80d22f72bbe800108f92d18592d402f4d37c561418121891c5eb32b2fb0228 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MkQ0OjE5NEUxMDoxQzEzOURFOjI1RjQ4NzI6NkIwMDA1QTUiLCJ2aXNpdG9yX2lkIjoiNDg4OTI0NTIwMjIzNTAwMDIyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b456a5421257db5b6b70f878168f0e43f1a0fcb7f2a57e13841d0222367d762b |
| hovercard-subject-tag | issue:574289546 |
| 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/8478/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b58c60585ced669d149f95971eaac22f08b7f69ab215dd80567e3e18345f7238/python/mypy/issues/8478 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b58c60585ced669d149f95971eaac22f08b7f69ab215dd80567e3e18345f7238/python/mypy/issues/8478 |
| og:image:alt | At a minimum, I'm reporting a bug in an error message. I have a function, run_tasks, which takes a list of tasks and a worker function, and executes the worker function on almost all the tasks. For... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | grahamc |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width