Title: Alternative constructors with primitive types or NewTypes · Issue #415 · python/typing · GitHub
Open Graph Title: Alternative constructors with primitive types or NewTypes · Issue #415 · python/typing
X Title: Alternative constructors with primitive types or NewTypes · Issue #415 · python/typing
Description: Hi typing team, I've noticed that typing.cast and NewType essentially provide the exact same functionality. This makes NewType merely a type alias for convenience. I'd like NewType to provide the ability to either pass in a callable that...
Open Graph Description: Hi typing team, I've noticed that typing.cast and NewType essentially provide the exact same functionality. This makes NewType merely a type alias for convenience. I'd like NewType to provide the a...
X Description: Hi typing team, I've noticed that typing.cast and NewType essentially provide the exact same functionality. This makes NewType merely a type alias for convenience. I'd like NewType to provi...
Opengraph URL: https://github.com/python/typing/issues/415
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Alternative constructors with primitive types or NewTypes","articleBody":"Hi typing team,\r\n\r\nI've noticed that typing.cast and NewType essentially provide the exact same functionality. This makes NewType merely a type alias for convenience.\r\n\r\nI'd like NewType to provide the ability to either pass in a callable that serves as the constructor, or for the library to provide an alternative function called ValidatedType (or AlternativelyConstructedType or something like that), implemented in this way (or something similar):\r\n\r\n```python\r\ndef ValidatedType(\r\n name,\r\n parent_type,\r\n validate_and_initialize # this will be a Callable[[Any], parent_type]\r\n):\r\n '''Follows typing NewType and runs the validation function'''\r\n def validated_type(x):\r\n return validate_and_initialize(x)\r\n validated_type.__name__ = name\r\n validated_type.__supertype__ = parent_type\r\n return validated_type\r\n```\r\n\r\nThis may add some overhead, but it also adds more clarity to codebases and is worth considering.\r\n\r\nSee https://github.com/python/mypy/issues/3200 -- created this issue here since mypy might not be the right repo to direct this feature request to","author":{"url":"https://github.com/davidroeca","@type":"Person","name":"davidroeca"},"datePublished":"2017-04-25T18:55:56Z","url":"https://github.com/python/typing/issues/415"}
| 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:7e8a5cf4-b198-b991-9861-e4b94d79576f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A4CA:D064:20FFB26:2CC2704:6B001E52 |
| html-safe-nonce | bade0f69a3481f3f9ddd239766e4b55472e92975ed719639d5a7de4ff648a790 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNENBOkQwNjQ6MjBGRkIyNjoyQ0MyNzA0OjZCMDAxRTUyIiwidmlzaXRvcl9pZCI6IjgxNTU4MTY1OTEwNTM4MjM1NzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cf1c3a307d81cef0282cb48fcfc6d3b2d188aa4eb744a93c6beac18f03eaf78e |
| hovercard-subject-tag | issue:224238517 |
| 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/415/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c21e2816cadb249c060def7922dc37895135bb068e9fc5e9c3b218b075709063/python/typing/issues/415 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c21e2816cadb249c060def7922dc37895135bb068e9fc5e9c3b218b075709063/python/typing/issues/415 |
| og:image:alt | Hi typing team, I've noticed that typing.cast and NewType essentially provide the exact same functionality. This makes NewType merely a type alias for convenience. I'd like NewType to provide the a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | davidroeca |
| 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