Title: Type constraints: dict.get doesn't accept Union type as key · Issue #1472 · python/mypy · GitHub
Open Graph Title: Type constraints: dict.get doesn't accept Union type as key · Issue #1472 · python/mypy
X Title: Type constraints: dict.get doesn't accept Union type as key · Issue #1472 · python/mypy
Description: Passing an overly-general Union to dict.get, like this: from typing import Union x = 5 # type: Union[int, str] { 1: 1 }.get(x, 0) results in this error: test.py:4: error: Argument 1 to "get" of "dict" has incompatible type "Union[int, st...
Open Graph Description: Passing an overly-general Union to dict.get, like this: from typing import Union x = 5 # type: Union[int, str] { 1: 1 }.get(x, 0) results in this error: test.py:4: error: Argument 1 to "get" of "di...
X Description: Passing an overly-general Union to dict.get, like this: from typing import Union x = 5 # type: Union[int, str] { 1: 1 }.get(x, 0) results in this error: test.py:4: error: Argument 1 to "get&qu...
Opengraph URL: https://github.com/python/mypy/issues/1472
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type constraints: dict.get doesn't accept Union type as key","articleBody":"Passing an overly-general Union to dict.get, like this:\n\n``` python\nfrom typing import Union\n\nx = 5 # type: Union[int, str]\n{ 1: 1 }.get(x, 0)\n```\n\nresults in this error:\n\n```\ntest.py:4: error: Argument 1 to \"get\" of \"dict\" has incompatible type \"Union[int, str]\"; expected \"int\"\n```\n\nArguably, this should be allowed, because `get` provides a default if the item is not found (unlike `__getitem__`). But by that logic, potentially _any_ type should be a valid argument to `get`, which doesn't seem right.\n@gvanrossum: further thoughts?\n","author":{"url":"https://github.com/ddfisher","@type":"Person","name":"ddfisher"},"datePublished":"2016-05-03T19:23:58Z","url":"https://github.com/python/mypy/issues/1472"}
| 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:c1a254aa-de57-06e1-f5f3-82e88fff3c84 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 99D4:39E772:5D1F15:7EA201:6B007471 |
| html-safe-nonce | 717f73b992ccc5c60ea4d6c6859b7a9ce304560b97ce632e9f156eca587788b0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OUQ0OjM5RTc3Mjo1RDFGMTU6N0VBMjAxOjZCMDA3NDcxIiwidmlzaXRvcl9pZCI6Ijc1OTQ3MzYxNDI0MDcyMDE5MDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a57644b2fcd76d8b619459d8ee1b035aa0fb2c0914063b2b2c5627686bdcdcd8 |
| hovercard-subject-tag | issue:152854361 |
| 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/1472/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3949180bffa79c08f28e2d0c53a1b1c8f19eb5ae54cd3a181e729133b60a5d69/python/mypy/issues/1472 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3949180bffa79c08f28e2d0c53a1b1c8f19eb5ae54cd3a181e729133b60a5d69/python/mypy/issues/1472 |
| og:image:alt | Passing an overly-general Union to dict.get, like this: from typing import Union x = 5 # type: Union[int, str] { 1: 1 }.get(x, 0) results in this error: test.py:4: error: Argument 1 to "get" of "di... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ddfisher |
| hostname | github.com |
| expected-hostname | github.com |
| None | 75c2b65dd205f26961d0c29afed1a8d0bb7cca9f05780571dfd1ef93eb539d16 |
| 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 | 84e563eb4b55f8fa102f325f117533cee9e3df4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width