Title: Wrong type inference with generics · Issue #10593 · python/mypy · GitHub
Open Graph Title: Wrong type inference with generics · Issue #10593 · python/mypy
X Title: Wrong type inference with generics · Issue #10593 · python/mypy
Description: Bug Report (A clear and concise description of what the bug is.) To Reproduce from typing import Any, Dict, List, TypedDict, TypeVar, Type, TYPE_CHECKING class User(TypedDict): id: int name: str users: List[Dict[str, Any]] = [{"id": 1, "...
Open Graph Description: Bug Report (A clear and concise description of what the bug is.) To Reproduce from typing import Any, Dict, List, TypedDict, TypeVar, Type, TYPE_CHECKING class User(TypedDict): id: int name: str us...
X Description: Bug Report (A clear and concise description of what the bug is.) To Reproduce from typing import Any, Dict, List, TypedDict, TypeVar, Type, TYPE_CHECKING class User(TypedDict): id: int name: str us...
Opengraph URL: https://github.com/python/mypy/issues/10593
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Wrong type inference with generics","articleBody":"**Bug Report**\r\n\r\n(A clear and concise description of what the bug is.)\r\n\r\n**To Reproduce**\r\n\r\n```python3\r\nfrom typing import Any, Dict, List, TypedDict, TypeVar, Type, TYPE_CHECKING\r\n\r\nclass User(TypedDict):\r\n id: int\r\n name: str\r\n\r\nusers: List[Dict[str, Any]] = [{\"id\": 1, \"name\": \"Alice\"}, {\"id\": 2, \"name\": \"Bob\"}]\r\n\r\nT = TypeVar(\"T\")\r\n\r\ndef try_cast(cls: Type[T], data: Any) -> T:\r\n return cls(**data) # type: ignore\r\n\r\nfor user in users:\r\n user_unannotated = try_cast(User, user)\r\n user_annotated: User = try_cast(User, user) # Incompatible types in assignment (expression has type \"User\", variable has type \"User\")\r\n\r\n if TYPE_CHECKING:\r\n reveal_type(try_cast) # def [T] (cls: Type[T`-1], data: Any) -> T`-1\r\n reveal_type(try_cast(User, {})) # typed.User*\r\n reveal_type(user) # builtins.dict*[builtins.str, Any]\r\n reveal_type(user_unannotated) # typed.User*\r\n reveal_type(user_unannotated[\"id\"]) # builtins.object*\r\n reveal_type(user_annotated) # TypedDict('typed.User', {'id': builtins.int, 'name': builtins.str})\r\n reveal_type(user_annotated[\"id\"]) # builtins.int\r\n```\r\n\r\nRun mypy one the above code. It will generate an error like `Incompatible types in assignment (expression has type \"User\", variable has type \"User\")`\r\n\r\n**Expected Behavior**\r\n\r\nMypy should accept both variants of the `try_cast` call and the type of `user_...[\"id\"]` should be `int` in both cases.\r\n\r\n**Actual Behavior**\r\n\r\nThe first invocation of `try_cast` works but the `id` attribute has the wrong type.\r\nThe second invocation of `try_cast` has an error however the `id` attribute has the correct 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 -->\r\n\r\n- Mypy version used: `mypy 0.812`\r\n- Mypy command-line flags: -\r\n- Mypy configuration options from `mypy.ini` (and other config files): -\r\n- Python version used: `Python 3.8.6`\r\n- Operating system and version: `Ubuntu 20.10`\r\n\r\n**Additional Details**\r\n\r\nPyright shows the expected behaviour here:\r\n\r\n```\r\n /tmp/septatrix-playground-2021-06-08T14-58-03-QGJ/typed.py:19:21 - info: Type of \"try_cast\" is \"(cls: Type[T@try_cast], data: Any) -> T@try_cast\"\r\n /tmp/septatrix-playground-2021-06-08T14-58-03-QGJ/typed.py:20:21 - info: Type of \"try_cast(User, { })\" is \"User\"\r\n /tmp/septatrix-playground-2021-06-08T14-58-03-QGJ/typed.py:21:21 - info: Type of \"user\" is \"Dict[str, Any]\"\r\n /tmp/septatrix-playground-2021-06-08T14-58-03-QGJ/typed.py:22:21 - info: Type of \"user_unannotated\" is \"User\"\r\n /tmp/septatrix-playground-2021-06-08T14-58-03-QGJ/typed.py:23:21 - info: Type of \"user_unannotated[\"id\"]\" is \"int\"\r\n /tmp/septatrix-playground-2021-06-08T14-58-03-QGJ/typed.py:24:21 - info: Type of \"user_annotated\" is \"User\"\r\n /tmp/septatrix-playground-2021-06-08T14-58-03-QGJ/typed.py:25:21 - info: Type of \"user_annotated[\"id\"]\" is \"int\"\r\n```","author":{"url":"https://github.com/septatrix","@type":"Person","name":"septatrix"},"datePublished":"2021-06-08T15:29:59Z","url":"https://github.com/python/mypy/issues/10593"}
| 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:9c07d389-6fe9-06ff-d948-e85a920a1fc3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D8C0:157BA1:C1A4BA:104289D:6B00506C |
| html-safe-nonce | d6e6b647ed132d1ed28325e2a0e4a40dfd424c6e4d9acf85ff0d9d11de6c4122 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOEMwOjE1N0JBMTpDMUE0QkE6MTA0Mjg5RDo2QjAwNTA2QyIsInZpc2l0b3JfaWQiOiIyMjE3ODkwMzM0NjkwNzE0NjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6913a5f5016dcf57cfa21db2552609144aa9c3d1786c973e8de98dd4d274670c |
| hovercard-subject-tag | issue:915177928 |
| 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/10593/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c0dd937ce438b6af62f165a7c5066bb2dba254592c91037e26defa4de165e41c/python/mypy/issues/10593 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c0dd937ce438b6af62f165a7c5066bb2dba254592c91037e26defa4de165e41c/python/mypy/issues/10593 |
| og:image:alt | Bug Report (A clear and concise description of what the bug is.) To Reproduce from typing import Any, Dict, List, TypedDict, TypeVar, Type, TYPE_CHECKING class User(TypedDict): id: int name: str us... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | septatrix |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width