Title: Map with bind: match Tuple of parameterized types with parameters from TypeVarTuple · Issue #1383 · python/typing · GitHub
Open Graph Title: Map with bind: match Tuple of parameterized types with parameters from TypeVarTuple · Issue #1383 · python/typing
X Title: Map with bind: match Tuple of parameterized types with parameters from TypeVarTuple · Issue #1383 · python/typing
Description: Prior work in PEP-646 on Map was dropped to simplify it, with a promise it would appear in a future PEP. Here is a reference to the dropped Map feature: #193 (comment) Here is an discussion of a potential usage of this Map feature for ma...
Open Graph Description: Prior work in PEP-646 on Map was dropped to simplify it, with a promise it would appear in a future PEP. Here is a reference to the dropped Map feature: #193 (comment) Here is an discussion of a po...
X Description: Prior work in PEP-646 on Map was dropped to simplify it, with a promise it would appear in a future PEP. Here is a reference to the dropped Map feature: #193 (comment) Here is an discussion of a po...
Opengraph URL: https://github.com/python/typing/issues/1383
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Map with bind: match Tuple of parameterized types with parameters from TypeVarTuple","articleBody":"Prior work in PEP-646 on Map was dropped to simplify it, with a promise it would appear in a future PEP. Here is a reference to the dropped Map feature: https://github.com/python/typing/issues/193#issuecomment-782689251\r\n\r\nHere is an discussion of a potential usage of this Map feature for matching argument types for a length-n Tuple of unary Callables with a length-n Tuple parameterized by a TypeVarTuple: https://groups.google.com/g/dev-python/c/SbPOxIEvI60?pli=1\r\n\r\nHere is a draft spec of PEP-646 that included discussion of Map: https://github.com/python/peps/blob/bf897f8c839d1b4d4534ab1fa223a210e2cacf06/pep-0646.rst\r\n\r\nI would like to propose an extension of Map with a \"bind\" argument to allow disambiguation in case the parameterized type has multiple generic parameters. To give an example:\r\n\r\n```python\r\n###########\r\n## Converting from multiple domains to a common codomain\r\n############\r\n\r\nDomain = TypeVar('Domain')\r\nCodomain = TypeVar('Codomain')\r\n\r\nclass Detector(Generic(Domain)):\r\n def __init__(self, detector_fn: Callable[[Any], Optional[Domain]):\r\n self.detector_fn = detector_fn\r\n\r\n def detect(arg: Any) -> Optional[Domain]\r\n # returns `arg` if `arg` belongs to Domain, otherwise returns None\r\n return self.detector_fn(arg)\r\n\r\nclass Converter(Generic[Domain, Codomain]):\r\n def __init__(self, converter_fn: Callable[[Domain], Codomain]):\r\n self.converter_fn = converter_fn\r\n\r\n def convert(arg: Domain) -> Codomain:\r\n return self.converter_fn(arg)\r\n\r\nDomains = TypeVarTuple('Domains')\r\n\r\nclass ConverterCollection(Generic[*Domains, Codomain]):\r\n def __init__(self, detectors: Tuple[*Map[Detector, Domains]], converters: Tuple[*Map[Converter, Domains, bind=Domain]]):\r\n self.detectors = detectors\r\n self.converters = converters\r\n\r\n def convert(self, obj_to_convert: Union[*Domains]) -> Codomain:\r\n for detector, converter in zip(self.detectors, self.converters):\r\n detected_object = detector.detect(obj_to_convert)\r\n if not (detected_object is None):\r\n return converter.convert(object_to_convert)\r\n raise ValueError('No converter for object.')\r\n\r\n### Usage\r\n\r\nint_detector = Detector[int](lambda x: x if isinstance(x, int) else None)\r\nint_converter = Converter[int, str](lambda x: json.dumps(x))\r\n\r\nfloat_detector = Detector[float](lambda x: x if isinstance(x, float) else None)\r\nfloat_converter = Converter[float, str](lambda x: json.dumps(x))\r\n\r\ncc = ConverterCollection[int, float, str](\r\n (int_detector, float_detector),\r\n (int_converter, float_converter)\r\n)\r\n\r\ncc.convert(2) # works\r\ncc.convert(2.0) # also works\r\ncc.convert('hi') # type error\r\n\r\n```\r\n\r\nThe `bind=Domain` argument here would be because Converter class is generic in both Domain and Codomain, so there is ambiguity about which generic parameter to Map. This is not necessary for Detector because, because it is generic only over Domain, so there is no ambiguity what to map over.\r\n","author":{"url":"https://github.com/jmsmdy","@type":"Person","name":"jmsmdy"},"datePublished":"2023-04-03T03:18:31Z","url":"https://github.com/python/typing/issues/1383"}
| 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:ec811ff3-bdd1-6e0b-1b15-ed24e4faa8c6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DEFA:12476C:1839644:1FD0BBB:6B0019DE |
| html-safe-nonce | e145b747227f505d2fb0319a08b1c2672140fdc5d966a682d7ed23df200ab913 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERUZBOjEyNDc2QzoxODM5NjQ0OjFGRDBCQkI6NkIwMDE5REUiLCJ2aXNpdG9yX2lkIjoiMzk2NjYzMTY1NTE5OTc0MjQzMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0a0182bca1be3869baf82cb30cd8630be1ab14d8b396e47cddcbbd11c846317b |
| hovercard-subject-tag | issue:1651262274 |
| 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/1383/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ff432a87526be25b7f2d5782e0e6be24015f7357a82e5a37a03f04fe719d5d21/python/typing/issues/1383 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ff432a87526be25b7f2d5782e0e6be24015f7357a82e5a37a03f04fe719d5d21/python/typing/issues/1383 |
| og:image:alt | Prior work in PEP-646 on Map was dropped to simplify it, with a promise it would appear in a future PEP. Here is a reference to the dropped Map feature: #193 (comment) Here is an discussion of a po... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jmsmdy |
| 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