Title: Request: an AssertingTypeGuard type for TypeGuard-like semantics · Issue #930 · python/typing · GitHub
Open Graph Title: Request: an AssertingTypeGuard type for TypeGuard-like semantics · Issue #930 · python/typing
X Title: Request: an AssertingTypeGuard type for TypeGuard-like semantics · Issue #930 · python/typing
Description: There is a frequent pattern of TypeGuard-like functions which assert or otherwise raise an exception if a type constraint is not met. For example, microsoft/pyright#2007 points to a case in which unittest provides assertIsNotNone, but a ...
Open Graph Description: There is a frequent pattern of TypeGuard-like functions which assert or otherwise raise an exception if a type constraint is not met. For example, microsoft/pyright#2007 points to a case in which u...
X Description: There is a frequent pattern of TypeGuard-like functions which assert or otherwise raise an exception if a type constraint is not met. For example, microsoft/pyright#2007 points to a case in which u...
Opengraph URL: https://github.com/python/typing/issues/930
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Request: an AssertingTypeGuard type for TypeGuard-like semantics","articleBody":"There is a frequent pattern of TypeGuard-like functions which `assert` or otherwise raise an exception if a type constraint is not met.\r\n\r\nFor example, https://github.com/microsoft/pyright/issues/2007 points to a case in which `unittest` provides `assertIsNotNone`, but a type-checker cannot infer that type narrowing has occurred. Arguably, [the popular typeguard library](https://github.com/agronholm/typeguard) is based around an implementation of \"asserting\" type guards. (One which deduces what assertions should be made from the annotations.)\r\n\r\nTypeGuards allow for semantics like\r\n```python\r\ny: str\r\nassert is_list_of_str(x)\r\nassert len(x) > 0\r\ny = x[0]\r\n```\r\n\r\nAn AssertingTypeGuard would allow for\r\n```python\r\ny: str\r\nassert_is_nonempty_list_of_str(x) # note, this encodes another runtime check, the len check\r\ny = x[0]\r\n```\r\n\r\nThis becomes especially valuable if we consider that you might not want to do this all with `assert`. I may, as an author, prefer my own custom exceptions, e.g.\r\n```python\r\ndef assert_is_nonempty_list_of_str(x) -> AssertingTypeGuard[list[str]]:\r\n if not isinstance(x, list):\r\n raise ExpectedListError(x)\r\n if not x:\r\n raise EmptyContainerError(x)\r\n if not all(isinstance(y, str) for y in x):\r\n raise ContainedInvalidTypeError(x, str)\r\n return x\r\n```\r\n\r\n(Apologies if this repo is the wrong place to submit this request/idea. I'm happy to go through another process if necessary.)","author":{"url":"https://github.com/sirosen","@type":"Person","name":"sirosen"},"datePublished":"2021-11-09T22:12:36Z","url":"https://github.com/python/typing/issues/930"}
| 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:a15dfbbd-0f30-bc8d-e42d-c4adefa54f76 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D6DE:1AD4B2:73A68:9D683:6B0018D9 |
| html-safe-nonce | 866cc81b35057f5af9363bd2a0332d0fd846bb681e715bca148ce9d941cc5d87 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENkRFOjFBRDRCMjo3M0E2ODo5RDY4Mzo2QjAwMThEOSIsInZpc2l0b3JfaWQiOiIxODU1NDIxOTczODM2ODYzNzA1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9be621b93815dfc7522a59120b8227366a41ebea8c24bc05be27d44cd05f93f9 |
| hovercard-subject-tag | issue:1049170809 |
| 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/930/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3e5573be4098c3d5b6199b332a0f1a75e0f2e1e9862ce1270924c222c4c853ee/python/typing/issues/930 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3e5573be4098c3d5b6199b332a0f1a75e0f2e1e9862ce1270924c222c4c853ee/python/typing/issues/930 |
| og:image:alt | There is a frequent pattern of TypeGuard-like functions which assert or otherwise raise an exception if a type constraint is not met. For example, microsoft/pyright#2007 points to a case in which u... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sirosen |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| 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 | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width