Title: Function's argument type inference is affected by the functions return type when in a comprehension · Issue #17025 · python/mypy · GitHub
Open Graph Title: Function's argument type inference is affected by the functions return type when in a comprehension · Issue #17025 · python/mypy
X Title: Function's argument type inference is affected by the functions return type when in a comprehension · Issue #17025 · python/mypy
Description: Bug Report MyPy seems to be getting confused if function that receives a generic class returns a union type with that generic is used in a comprehension. If the functions is used in a for loop or the function returns just the generic T, ...
Open Graph Description: Bug Report MyPy seems to be getting confused if function that receives a generic class returns a union type with that generic is used in a comprehension. If the functions is used in a for loop or t...
X Description: Bug Report MyPy seems to be getting confused if function that receives a generic class returns a union type with that generic is used in a comprehension. If the functions is used in a for loop or t...
Opengraph URL: https://github.com/python/mypy/issues/17025
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Function's argument type inference is affected by the functions return type when in a comprehension","articleBody":"**Bug Report**\r\n\r\nMyPy seems to be getting confused if function that receives a generic class returns a union type with that generic is used in a comprehension. If the functions is used in a for loop or the function returns just the generic T, then MyPy doesn't show any errors.\r\n\r\n**To Reproduce**\r\n```python\r\nimport dataclasses\r\nfrom typing import Callable, TypeVar, Generic\r\n\r\nT = TypeVar('T')\r\n\r\n\r\n@dataclasses.dataclass(frozen=True, slots=True)\r\nclass Capsule(Generic[T]):\r\n builder: Callable[[], T]\r\n\r\n\r\ndef process(argument: Capsule[T]) -\u003e T | RuntimeError:\r\n if isinstance((value := argument.builder()), int) and value % 2 != 0:\r\n return RuntimeError('Not an even number')\r\n\r\n return argument.builder()\r\n\r\n\r\nencapsulated_ints = tuple(Capsule(builder=lambda: item) for item in range(10))\r\n\r\nrequests: list[int | RuntimeError] = []\r\nfor encapsulated_int in encapsulated_ints:\r\n request = process(encapsulated_int)\r\n requests.append(request)\r\nrequests = list(process(encapsulated_int) for encapsulated_int in encapsulated_ints)\r\n```\r\nError:\r\n```\r\nmain.py:25: error: Argument 1 to \"process\" has incompatible type \"Capsule[int]\"; expected \"Capsule[int | RuntimeError]\" [arg-type]\r\n```\r\nhttps://mypy-play.net/?mypy=latest\u0026python=3.12\u0026gist=ffe7822cc564a6f347c2b8d6fd98c47f\r\n\r\n**Expected Behavior**\r\n\r\nThe list comprehension line should be interpreted exactly the same as the for loop.\r\n\r\n**Actual Behavior**\r\n\r\nMyPy somehow relates the function return to the generic type.\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in --\u003e\r\n\r\n- Mypy version used: 1.9.0\r\n- Mypy command-line flags: no flags\r\n- Mypy configuration options from `mypy.ini` (and other config files): no configs\r\n- Python version used: 3.12\r\n\r\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. --\u003e\r\n","author":{"url":"https://github.com/ar-qun","@type":"Person","name":"ar-qun"},"datePublished":"2024-03-14T13:15:16.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/17025/mypy/issues/17025"}
| 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:c6d74116-a9c3-116d-83bc-c4ea751a94cb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ACBC:12D7DC:4EF88E5:673908A:6A5D7A7D |
| html-safe-nonce | e432af6ccfc2391131a0d209350afa2037bc79e183d58e286b9d42ac7d515add |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQ0JDOjEyRDdEQzo0RUY4OEU1OjY3MzkwOEE6NkE1RDdBN0QiLCJ2aXNpdG9yX2lkIjoiNjM3OTEwMDEzOTUxMzgwNTQzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4cc5f16a4d8a6498652ce0ca6f297a0e39c35ff7d6aef4480f57ded327988eda |
| hovercard-subject-tag | issue:2186361813 |
| 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/17025/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3dafd956a4bbc9a70140f306ddbcdc05a406638fdb194ad59cd53a9013f5c9ce/python/mypy/issues/17025 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3dafd956a4bbc9a70140f306ddbcdc05a406638fdb194ad59cd53a9013f5c9ce/python/mypy/issues/17025 |
| og:image:alt | Bug Report MyPy seems to be getting confused if function that receives a generic class returns a union type with that generic is used in a comprehension. If the functions is used in a for loop or t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ar-qun |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width