Title: Type context not working for second argument of dict.get · Issue #2703 · python/mypy · GitHub
Open Graph Title: Type context not working for second argument of dict.get · Issue #2703 · python/mypy
X Title: Type context not working for second argument of dict.get · Issue #2703 · python/mypy
Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Open Graph Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
X Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Opengraph URL: https://github.com/python/mypy
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type context not working for second argument of dict.get","articleBody":"The new signature for `dict.get` introduced in https://github.com/python/typeshed/commit/6008b9dbb14bd947e219f655c66a36a63a91c42e causes wrong types to be inferred for some arguments. Here is an example that demonstrates the issue and doesn't rely on the typeshed commit:\r\n\r\n```\r\nfrom typing import Any, Generic, overload, Optional, TypeVar, Union\r\n\r\n_KT = TypeVar('_KT')\r\n_VT_co = TypeVar('_VT_co', covariant=True)\r\n_T = TypeVar('_T')\r\n\r\nclass D(Generic[_KT, _VT_co]): # Mimics Dict\r\n @overload\r\n def get(self, k: _KT) -> Optional[_VT_co]: ...\r\n @overload\r\n def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ...\r\n\r\ndef f(x: Any, y: D[int, D[int, Any]]) -> None:\r\n a = y.get(x, D())\r\n reveal_type(a) # Union[t.D[builtins.int, Any], t.D*[builtins.None, builtins.None]]\r\n```\r\n\r\nThe expected type is `t.D[builtins.int, Any]`.\r\n\r\nSimilar example using typeshed (note that the commit that introduced the problem is not yet used in mypy master):\r\n\r\n```\r\nfrom typing import Any, Dict\r\n\r\ndef f(x: Any, y: Dict[int, Dict[int, Any]]) -> None:\r\n a = y.get(x, {})\r\n reveal_type(a) # Union[builtins.dict[builtins.int, Any], builtins.dict*[builtins.None, builtins.None]]\r\n```\r\n\r\nThe reason for the behavior is that mypy doesn't realize that type context should propagate from the return type to the argument somehow. Actually, it's unclear whether type context should generally work like this -- perhaps we should modify the signature of `get` instead.","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2017-01-17T16:49:58Z","url":"https://github.com/python/mypy/issues/2703"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:6be267ed-3d01-0e42-671d-0825e992cfd2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D238:3AC034:0465:05F1:6B00191D |
| html-safe-nonce | 5aafe0c89db9a884fcadf02ad5d03a92f2c11e49a0f1f2ccf399dc0ca6fd991c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjM4OjNBQzAzNDowNDY1OjA1RjE6NkIwMDE5MUQiLCJ2aXNpdG9yX2lkIjoiMjcyMjEyOTI2MzAxOTk1NjUwOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2a51aae89ab2f8d06b7ac780b1a24ede3bd94ca76f7d269cb32ee769ca653135 |
| hovercard-subject-tag | repository:7053637 |
| 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/python/mypy/issues/2703 |
| twitter:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| og:image:alt | Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| turbo-cache-control | no-cache |
| 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