Title: Add a stdlib decorator that copies/applies the ParameterSpec from one function to another · Issue #107001 · python/cpython · GitHub
Open Graph Title: Add a stdlib decorator that copies/applies the ParameterSpec from one function to another · Issue #107001 · python/cpython
X Title: Add a stdlib decorator that copies/applies the ParameterSpec from one function to another · Issue #107001 · python/cpython
Description: Feature or enhancement Add an decorator that copies the parameter specs from one function onto another: Example implementation from collections.abc import Callable from typing import ParamSpec, TypeVar, cast, Any P = ParamSpec("P") T = T...
Open Graph Description: Feature or enhancement Add an decorator that copies the parameter specs from one function onto another: Example implementation from collections.abc import Callable from typing import ParamSpec, Typ...
X Description: Feature or enhancement Add an decorator that copies the parameter specs from one function onto another: Example implementation from collections.abc import Callable from typing import ParamSpec, Typ...
Opengraph URL: https://github.com/python/cpython/issues/107001
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add a stdlib decorator that copies/applies the ParameterSpec from one function to another","articleBody":"# Feature or enhancement\n\nAdd an decorator that copies the parameter specs from one function onto another:\n\n[Example implementation](https://stackoverflow.com/questions/71968447)\n```python\nfrom collections.abc import Callable\nfrom typing import ParamSpec, TypeVar, cast, Any\n\n\nP = ParamSpec(\"P\")\nT = TypeVar(\"T\")\n\n\ndef copy_kwargs(\n kwargs_call: Callable[P, Any]\n) -> Callable[[Callable[..., T]], Callable[P, T]]:\n \"\"\"Decorator does nothing but returning the casted original function\"\"\"\n\n @wraps(kwargs_call)\n def return_func(func: Callable[..., T]) -> Callable[P, T]:\n return cast(Callable[P, T], func)\n\n return return_func\n```\n\nAlternative names of `copy_kwargs` could be `apply_parameters`\n\n\n# Pitch\n\nA quite common pattern in Python is to create a new function `enhanced` that enhanced a given function `original` or method and passes arguments using `*args, **kwargs`.\nThis way the signature of `original` can be adopted without changing the signature of `enhanced`.\nThat is especially useful if you enhance a 3rd party function.\n\nA downside of this pattern is, that static type checkers (and IDE) are unable to detect if the correct parameters were used or give type/autocomplete hints.\n\nAdding this pattern allows static type checkers and IDE to give correct parameter hints and check them.\n\n# Previous discussion\nDiscussed with @ambv at a Sprint within Europython 2023\n\n# Specification\n\nThis function is very simple, so adding it every project that requires it would be very simple.\nA reason to add it to the standard library is that type checkers can/should check, that the applied parameter spec matches the one of the function.\n\nIn example:\n\n```python\n# Our test function for kwargs\ndef source_func(foo: str, bar: int, default: bool = True) -> str:\n if not default:\n return \"Not Default!\"\n return f\"{foo}_{bar}\"\n\n@copy_kwargs(source_func)\ndef kwargs_test(**kwargs) -> float:\n print(source_func(**kwargs))\n return 1.2\n\nkwargs_test(\"a\", 2) # raises a TypeError but does not produce any TypingError atm\n```\n\nBut if `source_func` would be defined as\n```python\ndef source_func(*, foo: str, bar: int, default: bool = True) -> str:\n ...\n```\n\nThe type checker would complain.\n\nSo I would suggest that type checkers check if the wrapped functions signature is compatible to the sourced function signature.\nWhich is separate discussion from including it into the stdlib.\n\nThe documentation should include a hint, that `*args` and `**kwargs` should be always added to the applied function.\n\n## Related Discourse Threads: \u003ca href=\"#sources\" id=\"sources\">#\u003c/a>\n\n- [Add a `same_definition_as_in`](https://discuss.python.org/t/add-a-same-definition-as-in/40135)\n- [Taking the argument signature from a different function](https://discuss.python.org/t/taking-the-argument-signature-from-a-different-function/42618)\n- [Proposal: Add `bound` to `ParamSpec`](https://discuss.python.org/t/proposal-add-bound-to-paramspec/39979)\n- [Precise typing of kwargs but without a TypedDict](https://discuss.python.org/t/precise-typing-of-kwargs-but-without-a-typeddict/49902)\n- [Extract kwargs types from a function signature](https://discuss.python.org/t/extract-kwargs-types-from-a-function-signature/34238)\n- [Dynamically building ParamSpecs from callables](https://discuss.python.org/t/dynamically-building-paramspecs-from-callables/17442)\n\n## Related Issues:\n\n- [How should we annotate functions that forward to their superclass?](https://github.com/python/typing/issues/1471) python/typing#1471 by @NeilGirdhar\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-121693\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/CarliJoy","@type":"Person","name":"CarliJoy"},"datePublished":"2023-07-22T10:45:44Z","url":"https://github.com/python/cpython/issues/107001"}
| 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:cf1a6009-e00c-b470-7ea7-5a365404adf6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8FB4:1390B0:25D6B:32467:6B00176B |
| html-safe-nonce | 5779d865f622205b9bbf27cf8863c7602db6e87c5db7364ac4b46e53c1a2af4a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RkI0OjEzOTBCMDoyNUQ2QjozMjQ2Nzo2QjAwMTc2QiIsInZpc2l0b3JfaWQiOiI2ODUwMDAzMDg5NzUyMDY5OTk1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a47160946d515d5acaf714a3884e19cae38235dc39f559b00f48caa8adecb4a8 |
| hovercard-subject-tag | issue:1816719344 |
| 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/cpython/107001/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bbb044749df176709967b5899e72ba80d9b88a2cb0113fb46e50afc4b1e27207/python/cpython/issues/107001 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bbb044749df176709967b5899e72ba80d9b88a2cb0113fb46e50afc4b1e27207/python/cpython/issues/107001 |
| og:image:alt | Feature or enhancement Add an decorator that copies the parameter specs from one function onto another: Example implementation from collections.abc import Callable from typing import ParamSpec, Typ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | CarliJoy |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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