Title: Type annotation erasure at compile time · Issue #400 · python/typing · GitHub
Open Graph Title: Type annotation erasure at compile time · Issue #400 · python/typing
X Title: Type annotation erasure at compile time · Issue #400 · python/typing
Description: Problem definition Current usage of type hints shows the following patterns: users often use "Dict", "List", "Set" without adding the required imports from typing; this is especially possible in case of type comments which don't fail at ...
Open Graph Description: Problem definition Current usage of type hints shows the following patterns: users often use "Dict", "List", "Set" without adding the required imports from typing; this is especially possible in ca...
X Description: Problem definition Current usage of type hints shows the following patterns: users often use "Dict", "List", "Set" without adding the required imports from typing; thi...
Opengraph URL: https://github.com/python/typing/issues/400
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type annotation erasure at compile time","articleBody":"## Problem definition\r\n\r\nCurrent usage of type hints shows the following patterns:\r\n* users often use \"Dict\", \"List\", \"Set\" **without adding the required imports** from typing; this is especially possible in case of type comments which don't fail at runtime;\r\n* users **try to put generics on built-in collections**, which again mostly happens in type comments which don't fail at runtime;\r\n* users get confused as to **why there is List but there's no Str**.\r\n\r\nOn top of this, I noticed the following tricky typing situation (generalization of actual code at FB):\r\n```py3\r\nif TYPE_CHECKING:\r\n from expensive_unrelated_module import SomeType # noqa\r\n from dependency_cycle import CycleType # noqa\r\n\r\ndef function(arg: 'SomeType') -\u003e 'CycleType':\r\n from dependency_cycle import CycleType # noqa\r\n return CycleType(arg)\r\n```\r\n\r\nYou cannot simply write `SomeType` or `CycleType` because it would fail at runtime. So you wrap it in strings. But when you wrap, linters start reporting the imports as unused, or shadowing previous unused imports. So you need to additionally add silencing comments. The resulting code is pretty hideous.\r\n\r\n## Solution\r\n\r\nI'm proposing revisiting the PEP 484 suggestion to make all annotations evaluate to strings at runtime with Python 3.7.\r\n\r\nRationale:\r\n* this lets us **seamlessly integrate generics on builtin collections** without the need to modify C code to support indexing;\r\n* this provides **seamless forward reference support**;\r\n* this gives the user **seamless access to imports made in the `if TYPE_CHECKING:` block**;\r\n* this **accelerates import time** and decreases memory usage at runtime.\r\n\r\nDetails:\r\n* access to an evaluated expression is still possible via `typing.get_type_hints()`;\r\n* the expression is still expected to be syntactically valid and using known names;\r\n* for 3.7 a future import would be required, tentatively called `static_annotations` (2 characters longer than `absolute_imports`, 4 characters than `with_statement` and `print_function`)\r\n* no other behavioral changes are expected.\r\n\r\nSumming up, I think this greatly improves the user experience.","author":{"url":"https://github.com/ambv","@type":"Person","name":"ambv"},"datePublished":"2017-03-10T10:28:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":27},"url":"https://github.com/400/typing/issues/400"}
| 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:793d96d5-7073-2dd0-333b-a32ab23a9edd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BFC2:33D2D8:913C38:CAF3C2:6A4C97EB |
| html-safe-nonce | c80a8b85bc335c7c010cdfe8c0d55c927cc18ff141ddc76147e0479a5b0b322c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRkMyOjMzRDJEODo5MTNDMzg6Q0FGM0MyOjZBNEM5N0VCIiwidmlzaXRvcl9pZCI6Ijg3NDMzNzUxMzA4MTY0NTI1ODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 76ebc00f08b37e8e708979adfb1ce56174561807cab92558bcfc67c66a66a01c |
| hovercard-subject-tag | issue:213302923 |
| 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/400/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2230d6a9a3aabcdaa9c6365326e28b8d28bccd37bdec0a885cc82ab2a49d3e71/python/typing/issues/400 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2230d6a9a3aabcdaa9c6365326e28b8d28bccd37bdec0a885cc82ab2a49d3e71/python/typing/issues/400 |
| og:image:alt | Problem definition Current usage of type hints shows the following patterns: users often use "Dict", "List", "Set" without adding the required imports from typing; this is especially possible in ca... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ambv |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c |
| 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 | 7a2dede45637df6b92ddcfe2a557e67d4b5854ae |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width