Title: `TypeVarTuple` and Cartesian Product of functions · python/typing · Discussion #1545 · GitHub
Open Graph Title: `TypeVarTuple` and Cartesian Product of functions · python/typing · Discussion #1545
X Title: `TypeVarTuple` and Cartesian Product of functions · python/typing · Discussion #1545
Description: `TypeVarTuple` and Cartesian Product of functions
Open Graph Description: Consider the Cartesian product of functions def prod_fn(*funcs): r"""Cartesian Product of Functions. It is assumed every function takes a single positional argument. """ def __prod_fn(args, /): """...
X Description: Consider the Cartesian product of functions def prod_fn(*funcs): r"""Cartesian Product of Functions. It is assumed every function takes a single positional argument. ""&quo...
Opengraph URL: https://github.com/python/typing/discussions/1545
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"`TypeVarTuple` and Cartesian Product of functions","text":"\u003cp dir=\"auto\"\u003eConsider the \u003ca href=\"https://en.wikipedia.org/wiki/Cartesian_product#Cartesian_product_of_functions\" rel=\"nofollow\"\u003eCartesian product of functions\u003c/a\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"def prod_fn(*funcs):\n r\u0026quot;\u0026quot;\u0026quot;Cartesian Product of Functions.\n\n It is assumed every function takes a single positional argument.\n \u0026quot;\u0026quot;\u0026quot;\n\n def __prod_fn(args, /):\n \u0026quot;\u0026quot;\u0026quot;Argument is a tuple with the input for each function.\u0026quot;\u0026quot;\u0026quot;\n return tuple(f(arg) for f, arg in zip(funcs, args))\n\n return __prod_fn\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eprod_fn\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003efuncs\u003c/span\u003e):\n \u003cspan class=\"pl-s\"\u003er\"\"\"Cartesian Product of Functions.\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e It is assumed every function takes a single positional argument.\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e \"\"\"\u003c/span\u003e\n\n \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003e__prod_fn\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eargs\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e/\u003c/span\u003e):\n \u003cspan class=\"pl-s\"\u003e\"\"\"Argument is a tuple with the input for each function.\"\"\"\u003c/span\u003e\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-en\"\u003etuple\u003c/span\u003e(\u003cspan class=\"pl-en\"\u003ef\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003earg\u003c/span\u003e) \u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ef\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003earg\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003ein\u003c/span\u003e \u003cspan class=\"pl-en\"\u003ezip\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003efuncs\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eargs\u003c/span\u003e))\n\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e__prod_fn\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eHow to type hint it? It should be something along the lines of\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"def prod_fn(*funcs: *tuple[???]) -\u0026gt; Callable[[tuple[*U]], tuple[*V]]:\n r\u0026quot;\u0026quot;\u0026quot;Cartesian Product of Functions.\n\n It is assumed every function takes a single positional argument.\n \u0026quot;\u0026quot;\u0026quot;\n\n def __prod_fn(args: tuple[*U], /) -\u0026gt; tuple[*V]:\n \u0026quot;\u0026quot;\u0026quot;Argument is a tuple with the input for each function.\u0026quot;\u0026quot;\u0026quot;\n return tuple(f(arg) for f, arg in zip(funcs, args))\n\n return __prod_fn\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eprod_fn\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003efuncs\u003c/span\u003e: \u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003etuple\u003c/span\u003e[???]) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eCallable\u003c/span\u003e[[\u003cspan class=\"pl-s1\"\u003etuple\u003c/span\u003e[\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eU\u003c/span\u003e]], \u003cspan class=\"pl-s1\"\u003etuple\u003c/span\u003e[\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eV\u003c/span\u003e]]:\n \u003cspan class=\"pl-s\"\u003er\"\"\"Cartesian Product of Functions.\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e It is assumed every function takes a single positional argument.\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e \"\"\"\u003c/span\u003e\n\n \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003e__prod_fn\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eargs\u003c/span\u003e: \u003cspan class=\"pl-s1\"\u003etuple\u003c/span\u003e[\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eU\u003c/span\u003e], \u003cspan class=\"pl-c1\"\u003e/\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etuple\u003c/span\u003e[\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eV\u003c/span\u003e]:\n \u003cspan class=\"pl-s\"\u003e\"\"\"Argument is a tuple with the input for each function.\"\"\"\u003c/span\u003e\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-en\"\u003etuple\u003c/span\u003e(\u003cspan class=\"pl-en\"\u003ef\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003earg\u003c/span\u003e) \u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ef\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003earg\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003ein\u003c/span\u003e \u003cspan class=\"pl-en\"\u003ezip\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003efuncs\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eargs\u003c/span\u003e))\n\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e__prod_fn\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eWhere \u003ccode class=\"notranslate\"\u003eU\u003c/code\u003e and \u003ccode class=\"notranslate\"\u003eV\u003c/code\u003e are \u003ccode class=\"notranslate\"\u003eTypeVarTuples\u003c/code\u003e for the inputs and outputs of each individual function. But I fail to see how to express the concept that \u003ccode class=\"notranslate\"\u003efuncs\u003c/code\u003e is the variadic tuple \u003ccode class=\"notranslate\"\u003etuple[Callable[[U₁], V₁]], Callable[[U₂], V₂]], ..., Callable[[Uₙ], Vₙ]]]\u003c/code\u003e\u003c/p\u003e","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"\u003cp dir=\"auto\"\u003eThis just isn't possible yet, this would require something like a \u003ccode class=\"notranslate\"\u003eMap\u003c/code\u003e construct to apply a type to each element of the \u003ccode class=\"notranslate\"\u003eTypeVarTuple\u003c/code\u003e, (although this case is even more complex since there's two \u003ccode class=\"notranslate\"\u003eTypeVarTuple\u003c/code\u003e that both need to be mapped at the same time)\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eRight now you can only unpack a \u003ccode class=\"notranslate\"\u003eTypeVarTuple\u003c/code\u003e directly into other variadic generics (i.e. \u003ccode class=\"notranslate\"\u003etuple\u003c/code\u003e, \u003ccode class=\"notranslate\"\u003eCallable\u003c/code\u003e, \u003ccode class=\"notranslate\"\u003eUnion\u003c/code\u003e and any user-defined generics that take a \u003ccode class=\"notranslate\"\u003eTypeVarTuple\u003c/code\u003e).\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eP.S. Wouldn't you need to use varargs for it to actually be the cartesian product of functions? The definition to me looks like the resulting function is supposed to have as many arguments as input functions, not one argument that's a tuple of all the inputs.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"def prod_fn(*funcs: *Map[(U, V), Callable[[U], V]]]) -\u0026gt; Callable[[*U], tuple[*V]]:\n r\u0026quot;\u0026quot;\u0026quot;Cartesian Product of Functions.\n\n It is assumed every function takes a single positional argument.\n \u0026quot;\u0026quot;\u0026quot;\n\n def __prod_fn(*args: *U) -\u0026gt; tuple[*V]:\n \u0026quot;\u0026quot;\u0026quot;Argument is a tuple with the input for each function.\u0026quot;\u0026quot;\u0026quot;\n return tuple(f(arg) for f, arg in zip(funcs, args))\n\n return __prod_fn\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eprod_fn\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003efuncs\u003c/span\u003e: \u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-v\"\u003eMap\u003c/span\u003e[(\u003cspan class=\"pl-c1\"\u003eU\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003eV\u003c/span\u003e), \u003cspan class=\"pl-v\"\u003eCallable\u003c/span\u003e[[\u003cspan class=\"pl-c1\"\u003eU\u003c/span\u003e], \u003cspan class=\"pl-c1\"\u003eV\u003c/span\u003e]]]) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eCallable\u003c/span\u003e[[\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eU\u003c/span\u003e], \u003cspan class=\"pl-s1\"\u003etuple\u003c/span\u003e[\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eV\u003c/span\u003e]]:\n \u003cspan class=\"pl-s\"\u003er\"\"\"Cartesian Product of Functions.\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e It is assumed every function takes a single positional argument.\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e \"\"\"\u003c/span\u003e\n\n \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003e__prod_fn\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eargs\u003c/span\u003e: \u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eU\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etuple\u003c/span\u003e[\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eV\u003c/span\u003e]:\n \u003cspan class=\"pl-s\"\u003e\"\"\"Argument is a tuple with the input for each function.\"\"\"\u003c/span\u003e\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-en\"\u003etuple\u003c/span\u003e(\u003cspan class=\"pl-en\"\u003ef\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003earg\u003c/span\u003e) \u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ef\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003earg\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003ein\u003c/span\u003e \u003cspan class=\"pl-en\"\u003ezip\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003efuncs\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eargs\u003c/span\u003e))\n\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e__prod_fn\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e","upvoteCount":2,"url":"https://github.com/python/typing/discussions/1545#discussioncomment-7868224"}}}
| 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/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:f8053ca3-6380-08e2-834e-a4dba998ad59 |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | 8052:3204C8:2FA3729:3EEAF52:6B00287A |
| html-safe-nonce | d9b3b33470a73b21a05f6864cbf06a8ad5b7f527da2ae48f65b70075383662ed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDUyOjMyMDRDODoyRkEzNzI5OjNFRUFGNTI6NkIwMDI4N0EiLCJ2aXNpdG9yX2lkIjoiODM1MjA2MDU1MjA5MjE2MTIyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5821feeb2b330838cb34ae3837e20f83822bbc244e665b903a2948fe9566b6dc |
| hovercard-subject-tag | discussion:5964770 |
| github-keyboard-shortcuts | repository,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/Voltron::DiscussionsFragmentsController/show/python/typing/1545/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/b205f074664f9cbf55c674ddcb2c73d2f19110375c4f10b2fe9fb392a8210053/python/typing/discussions/1545 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b205f074664f9cbf55c674ddcb2c73d2f19110375c4f10b2fe9fb392a8210053/python/typing/discussions/1545 |
| og:image:alt | Consider the Cartesian product of functions def prod_fn(*funcs): r"""Cartesian Product of Functions. It is assumed every function takes a single positional argument. """ def __prod_fn(args, /): """... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| 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 |
Links:
Viewport: width=device-width