Title: Type for heterogeneous dictionaries with string keys · Issue #28 · python/typing · GitHub
Open Graph Title: Type for heterogeneous dictionaries with string keys · Issue #28 · python/typing
X Title: Type for heterogeneous dictionaries with string keys · Issue #28 · python/typing
Description: I've recently been reading Python code where heterogeneous dictionary objects are used a lot. I mean cases like this: foo({'x': 1, 'y': 'z'}) The value for key 'x' must be an integer and the value for key 'z' must be a string. Currently ...
Open Graph Description: I've recently been reading Python code where heterogeneous dictionary objects are used a lot. I mean cases like this: foo({'x': 1, 'y': 'z'}) The value for key 'x' must be an integer and the value ...
X Description: I've recently been reading Python code where heterogeneous dictionary objects are used a lot. I mean cases like this: foo({'x': 1, 'y': 'z'}) The value for key 'x...
Opengraph URL: https://github.com/python/typing/issues/28
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type for heterogeneous dictionaries with string keys","articleBody":"I've recently been reading Python code where heterogeneous dictionary objects are used a lot. I mean cases like this:\n\n``` python\nfoo({'x': 1, 'y': 'z'})\n```\n\nThe value for key `'x'` must be an integer and the value for key `'z'` must be a string. Currently there is no precise way of specifying the type of the argument to `foo` in the above example. In general, we'd have to fall back to `Dict[str, Any]`, `Mapping[str, Union[int, str]]` or similar. This loses a lot of information.\n\nHowever, we could support such types. Here is a potential syntax:\n\n``` python\ndef foo(arg: Dict[{'x': int, 'y': str}]) -> ...: ...\n```\n\nOf course, we could also allow `Dict[dict(x=int, y=str)]` as an equivalent. I don't really love either syntax, though.\n\nAlternatively, we could omit `Dict[...]` as redundant:\n\n``` python\ndef f(arg: dict(x=int, y=str)) -> ...\n```\n\nUsing type aliases would often be preferred:\n\n``` python\nArgType = Dict[{'x': int, 'y': str}]\n\ndef f(arg: ArgType) -> ...\n```\n\nThese types would use structural subtyping, and missing keys could plausibly be okay. So `Dict[dict(x=int, y=str)]` could be a subtype of `Dict[dict(x=int)]`, and vice versa (!).\n\nMaybe there should also be a way of deriving subtypes of heterogeneous dictionary types (similar to inheritance) to avoid repetition.\n\nMaybe we'd also want to support `Mapping[...]` variants (for read-only access and covariance).\n\nSome existing languages have types resembling these (at least Hack and TypeScript, I think).\n","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2014-11-21T07:25:56Z","url":"https://github.com/python/typing/issues/28"}
| 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:53ea94a3-2145-4207-7c42-f17c6037830c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DC38:356FBF:2298E7F:2ED3611:6B002339 |
| html-safe-nonce | fcc4b3c061b976237de6e2b1c69d882fb0bbcedc8e2c5208f1755bacee603a7f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzM4OjM1NkZCRjoyMjk4RTdGOjJFRDM2MTE6NkIwMDIzMzkiLCJ2aXNpdG9yX2lkIjoiMjU3MTA4MjcwMjEwMjk5NTc2OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5d652d7feb8b9672ef08c20fee09ddb71c50a1a7f0d7b58ba4e49a60d8b33103 |
| hovercard-subject-tag | issue:49666610 |
| 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/typing/28/issue_layout |
| twitter:image | https://opengraph.githubassets.com/12cb78eb7137ae08bbe4a4699390d107c1f9424ef8328be0f3d5da9f0437191e/python/typing/issues/28 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/12cb78eb7137ae08bbe4a4699390d107c1f9424ef8328be0f3d5da9f0437191e/python/typing/issues/28 |
| og:image:alt | I've recently been reading Python code where heterogeneous dictionary objects are used a lot. I mean cases like this: foo({'x': 1, 'y': 'z'}) The value for key 'x' must be an integer and the value ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JukkaL |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width