Title: Should the `dict.get` overload with default value not be generic? · Issue #9155 · python/typeshed · GitHub
Open Graph Title: Should the `dict.get` overload with default value not be generic? · Issue #9155 · python/typeshed
X Title: Should the `dict.get` overload with default value not be generic? · Issue #9155 · python/typeshed
Description: When using dict.get with a default value, it is expected that the key may not be found. Most of the time it's because we're not dealing with literal types, so we can't ensure that a key is part of the dict. But other times, it's we expli...
Open Graph Description: When using dict.get with a default value, it is expected that the key may not be found. Most of the time it's because we're not dealing with literal types, so we can't ensure that a key is part of ...
X Description: When using dict.get with a default value, it is expected that the key may not be found. Most of the time it's because we're not dealing with literal types, so we can't ensure that a key...
Opengraph URL: https://github.com/python/typeshed/issues/9155
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Should the `dict.get` overload with default value not be generic?","articleBody":"When using `dict.get` with a default value, it is expected that the key may not be found. Most of the time it's because we're not dealing with literal types, so we can't ensure that a key is part of the dict. But other times, it's we explicitely use a value that has a type we know won't be in range (often None).\r\n\r\nSee the example below (it is of course minimal and abstracted from a real-world example, but should still show the use):\r\n```py\r\ncode_events = {\r\n 0: \"ZERO_EVENT\",\r\n 1: \"ONE_EVENT\",\r\n # ...\r\n 127: \"LAST_EVENT\",\r\n}\r\n\r\ndef parse_foo_code(self, code: int | None):\r\n code = code and (code \u0026 0x7f) # Some bitwise operation to skip bit 8, this just shows we can't use -1 as default. And who knows what other keys could be used in the dict\r\n event = code_events.get(code, \"SPECIAL_DEFAULT_FOO_EVENT\")\r\n # ... do more with the event\r\n \r\ndef parse_bar_code(self, code: int | None):\r\n code = code and (code \u0026 0x7f) # Some bitwise operation to skip bit 8\r\n event = code_events.get(code, \"SPECIAL_DEFAULT_BAR_EVENT\")\r\n # ... do more with the event\r\n```\r\n\r\nCurrently it has to be written as such (without a cast). Which adds redundancy checks and no type-safety:\r\n```py\r\ndef parse_foo_code(self, code: int | None):\r\n code = code and (code \u0026 0x7f) # Some bitwise operation to skip bit 8\r\n if isinstance(code, int): # You could imagine this could be more complex than just int. Maybe it can't even be easily expressed\r\n event = code_events.get(code, \"SPECIAL_DEFAULT_FOO_EVENT\")\r\n else:\r\n event = \"SPECIAL_DEFAULT_FOO_EVENT\"\r\n # ... do more with the event\r\n```\r\n\r\nWhat if this definition in dict:\r\n```py\r\n@overload\r\ndef get(self, __key: _KT) -\u003e _VT | None: ...\r\n@overload\r\ndef get(self, __key: _KT, __default: _VT | _T) -\u003e _VT | _T: ...\r\n```\r\n\r\nwas changed to:\r\n```py\r\n@overload\r\ndef get(self, __key: _KT) -\u003e _VT | None: ...\r\n@overload\r\ndef get(self, __key: object, __default: _VT | _T) -\u003e _VT | _T: ... # Either object, or Any if it causes variance issue.\r\n```\r\n\r\nWhat are your thoughts? Any reason this would be a bad idea? From what I can see there are legit sensible use-cases, it stays type-safe, and allows for more concise code. Or just use a cast and call it a day?","author":{"url":"https://github.com/Avasam","@type":"Person","name":"Avasam"},"datePublished":"2022-11-10T22:06:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/9155/typeshed/issues/9155"}
| 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:508fae80-d5b2-f599-4ad3-11f86c8c6ff9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A0D8:A8331:4115AC:584757:6A61766F |
| html-safe-nonce | 8555458038c06987bed20db9e074fb20e9b1823714f52e683eef7702acaaa266 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMEQ4OkE4MzMxOjQxMTVBQzo1ODQ3NTc6NkE2MTc2NkYiLCJ2aXNpdG9yX2lkIjoiNTUyNDk4MDIwNTQ3NDY0MTUxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a21c5f27d83116e8ab897ac83bb953213fa0204078d4207802f658654fd39219 |
| hovercard-subject-tag | issue:1444629441 |
| 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/typeshed/9155/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2614956d4e62742a06011217c6ae640b013d14409ab2403791bf3dbf40c183bd/python/typeshed/issues/9155 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2614956d4e62742a06011217c6ae640b013d14409ab2403791bf3dbf40c183bd/python/typeshed/issues/9155 |
| og:image:alt | When using dict.get with a default value, it is expected that the key may not be found. Most of the time it's because we're not dealing with literal types, so we can't ensure that a key is part of ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Avasam |
| hostname | github.com |
| expected-hostname | github.com |
| None | b05bb6314e7e93021a7b7dab54a85cbe48d821938e762c1dff815efd60699622 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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 | 94389bfdcd393b0a5a274aee94e8a9739d063fe5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width