Title: Annotate decorated functions · Issue #412 · python/typing · GitHub
Open Graph Title: Annotate decorated functions · Issue #412 · python/typing
X Title: Annotate decorated functions · Issue #412 · python/typing
Description: When you declare a function, your signature annotation serves three purposes. It allows a program to typecheck the body of the function, finding human errors It allows a program to typecheck the call sites of the function, finding more h...
Open Graph Description: When you declare a function, your signature annotation serves three purposes. It allows a program to typecheck the body of the function, finding human errors It allows a program to typecheck the ca...
X Description: When you declare a function, your signature annotation serves three purposes. It allows a program to typecheck the body of the function, finding human errors It allows a program to typecheck the ca...
Opengraph URL: https://github.com/python/typing/issues/412
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Annotate decorated functions","articleBody":"When you declare a function, your signature annotation serves three purposes.\r\n * It allows a program to typecheck the body of the function, finding human errors\r\n * It allows a program to typecheck the call sites of the function, finding more human errors\r\n * **It allows a human to quickly understand the usage of the function, preventing human errors.**\r\n\r\nPlease excuse my poorly-motivated example below.\r\n\r\n```python\r\nfrom typing import TypeVar, Callable, Dict, Any\r\n\r\nT = TypeVar('T')\r\n\r\ndef reprify(f: Callable[[T], Any]) -> Callable[[T], str]:\r\n def res(__x: T) -> str:\r\n return repr(f(__x))\r\n return res\r\n\r\n@reprify\r\ndef nonsense(i: int) -> Dict[str, int]:\r\n return {\"nonsense\": i}\r\n\r\ns = nonsense(3)\r\nreveal_type(s) # E: Revealed type is 'builtins.str'\r\n```\r\n\r\nThe first two purposes are served. The computer can typecheck the body of the function, understanding that it is to return a dictionary inside. The computer can also typecheck the call site of the function, understanding that the decorator causes the function to return a string.\r\n\r\nThe last purpose is not served. To understand how to use the `nonsense` function, a human must look at the signature of `nonsense`, then remember to look at the signature of `reprify`, and then perform a type calculation in their head combining the two. \r\n\r\nI would like a way to optionally make a description of the type of `nonsense` a call site sees, so the humans do not get confused. We can then tell a computer to check against the decorators.\r\n\r\nOne possibility, provided for its straw-person properties:\r\n\r\n```python\r\n...\r\n\r\n@reprify # type: (int) -> str\r\ndef nonsense(i: int) -> Dict[str, int]:\r\n return {\"nonsense\": i}\r\n\r\n...\r\n```","author":{"url":"https://github.com/sixolet","@type":"Person","name":"sixolet"},"datePublished":"2017-04-12T19:01:12Z","url":"https://github.com/python/typing/issues/412"}
| 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:ed75a404-1c46-df11-38ef-a0331b785c6e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 812C:1DD784:589C94:76D1A6:6B003209 |
| html-safe-nonce | 0f3f2d83b0c14a7c936833a77e819f33c3851c00a07934d3f08ebabe119e4483 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTJDOjFERDc4NDo1ODlDOTQ6NzZEMUE2OjZCMDAzMjA5IiwidmlzaXRvcl9pZCI6Ijg2MDc5OTk0ODM5NDAxODg2ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a8755865a076b3d32ccf559723505040f4e8d2b19be09934ca4eb12d5905aa02 |
| hovercard-subject-tag | issue:221359175 |
| 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/412/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7088fe2c02223a6c4e2f8b50af3aaf9f2bf9420a72bd0dc9830f29d54345c216/python/typing/issues/412 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7088fe2c02223a6c4e2f8b50af3aaf9f2bf9420a72bd0dc9830f29d54345c216/python/typing/issues/412 |
| og:image:alt | When you declare a function, your signature annotation serves three purposes. It allows a program to typecheck the body of the function, finding human errors It allows a program to typecheck the ca... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sixolet |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width