Title: Generic type inferred wrong; curious difference betweer `aiter(f(x))` and `c = f(x); aiter(c)` · Issue #16376 · python/mypy · GitHub
Open Graph Title: Generic type inferred wrong; curious difference betweer `aiter(f(x))` and `c = f(x); aiter(c)` · Issue #16376 · python/mypy
X Title: Generic type inferred wrong; curious difference betweer `aiter(f(x))` and `c = f(x); aiter(c)` · Issue #16376 · python/mypy
Description: Bug Report Consider this code (playground): from typing import TypeVar, Generic, Callable, Coroutine, Any, AsyncIterator _T = TypeVar("_T") class StartedToken: pass class ProducerContext(Generic[_T]): pass async def coro_to_aiter( corout...
Open Graph Description: Bug Report Consider this code (playground): from typing import TypeVar, Generic, Callable, Coroutine, Any, AsyncIterator _T = TypeVar("_T") class StartedToken: pass class ProducerContext(Generic[_T...
X Description: Bug Report Consider this code (playground): from typing import TypeVar, Generic, Callable, Coroutine, Any, AsyncIterator _T = TypeVar("_T") class StartedToken: pass class ProducerContext(...
Opengraph URL: https://github.com/python/mypy/issues/16376
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic type inferred wrong; curious difference betweer `aiter(f(x))` and `c = f(x); aiter(c)`","articleBody":"**Bug Report**\r\n\r\nConsider this code ([playground](https://mypy-play.net/?mypy=latest&python=3.11&gist=226eb4b8855f13ea1886317daa7a5ede)):\r\n\r\n```python\r\nfrom typing import TypeVar, Generic, Callable, Coroutine, Any, AsyncIterator\r\n\r\n_T = TypeVar(\"_T\")\r\n\r\nclass StartedToken:\r\n pass\r\n\r\nclass ProducerContext(Generic[_T]):\r\n pass\r\n\r\nasync def coro_to_aiter(\r\n coroutine: Callable[[ProducerContext[_T]], Coroutine[Any, Any, None]]\r\n) -> AsyncIterator[_T | StartedToken]:\r\n yield StartedToken()\r\n\r\nasync def coro(ctx: ProducerContext[int]) -> None:\r\n pass\r\n\r\n# Split into two lines, no error:\r\nc = coro_to_aiter(coro) # no error\r\nit = aiter(c) # no error\r\n\r\n# The same on one line -> mypy error:\r\nit2 = aiter(coro_to_aiter(coro)) # error: Argument 1 to \"coro_to_aiter\" has incompatible type ...\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nI would expect mypy to not error on the it2 definition line. Moreover, regardless of whether mypy can handle these definitions, I would expect to see the same behavior (error or absence of error) in the \"Split into two lines\" case and the \"on one line\" case.\r\n\r\n**Actual Behavior**\r\n\r\nI get this error on the `it2` line, and no errors on other lines:\r\n\r\n`main.py:24: error: Argument 1 to \"coro_to_aiter\" has incompatible type \"Callable[[ProducerContext[int]], Coroutine[Any, Any, None]]\"; expected \"Callable[[ProducerContext[int | StartedToken]], Coroutine[Any, Any, None]]\" [arg-type]`\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.6.1 (playground), current master\r\n- Python version used: 3.11, 3.12\r\n","author":{"url":"https://github.com/sliedes","@type":"Person","name":"sliedes"},"datePublished":"2023-10-31T09:43:28Z","url":"https://github.com/python/mypy/issues/16376"}
| 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:248a7fa8-5ab2-c858-eb35-1d2c4df3a804 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF5A:3DFA13:835A6:B192F:6B001906 |
| html-safe-nonce | fee199eb4ec7bc9dbce90e820aca77b2a0a994d8ead0185b1b843a33e1c564b6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjVBOjNERkExMzo4MzVBNjpCMTkyRjo2QjAwMTkwNiIsInZpc2l0b3JfaWQiOiIyMTM0MzEzMzk4ODY5OTU2ODcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7c44410ad5a78de59146884383302bd62f1d2fc69761b39a50bbde9a48f5af62 |
| hovercard-subject-tag | issue:1970044584 |
| 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/16376/issue_layout |
| twitter:image | https://opengraph.githubassets.com/256d4884b487d8d67430f224ff5cd1c9275b79fe187fc68f4dcd636d19e6b31c/python/mypy/issues/16376 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/256d4884b487d8d67430f224ff5cd1c9275b79fe187fc68f4dcd636d19e6b31c/python/mypy/issues/16376 |
| og:image:alt | Bug Report Consider this code (playground): from typing import TypeVar, Generic, Callable, Coroutine, Any, AsyncIterator _T = TypeVar("_T") class StartedToken: pass class ProducerContext(Generic[_T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sliedes |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| 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 | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width