Title: Wrapped overloaded function drops all but the first overload · Issue #15737 · python/mypy · GitHub
Open Graph Title: Wrapped overloaded function drops all but the first overload · Issue #15737 · python/mypy
X Title: Wrapped overloaded function drops all but the first overload · Issue #15737 · python/mypy
Description: Bug Report When wrapping an overloaded function and using ParamSpec to preserve the call signature, only the first overload is preserved on the resulting function. To Reproduce See: https://gist.github.com/mypy-play/96895b9df7aa87c9e9f6c...
Open Graph Description: Bug Report When wrapping an overloaded function and using ParamSpec to preserve the call signature, only the first overload is preserved on the resulting function. To Reproduce See: https://gist.gi...
X Description: Bug Report When wrapping an overloaded function and using ParamSpec to preserve the call signature, only the first overload is preserved on the resulting function. To Reproduce See: https://gist.gi...
Opengraph URL: https://github.com/python/mypy/issues/15737
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Wrapped overloaded function drops all but the first overload","articleBody":"**Bug Report**\r\n\r\nWhen wrapping an overloaded function and using `ParamSpec` to preserve the call signature, only the first overload is preserved on the resulting function.\r\n\r\n**To Reproduce**\r\n\r\nSee:\r\n- https://gist.github.com/mypy-play/96895b9df7aa87c9e9f6cc59203d0751\r\n- https://mypy-play.net/?mypy=latest&python=3.11&gist=96895b9df7aa87c9e9f6cc59203d0751\r\n\r\n```python\r\nfrom typing import Callable, overload, TypeVar\r\nfrom typing_extensions import ParamSpec\r\n\r\nP = ParamSpec(\"P\")\r\nT = TypeVar(\"T\")\r\n\r\ndef transform(func: Callable[P, list[T]]) -> Callable[P, T]:\r\n def wrapped(*args: P.args, **kwargs: P.kwargs) -> T:\r\n return func(*args, **kwargs)[0]\r\n\r\n return wrapped\r\n\r\n@overload\r\ndef foo(x: int) -> list[float]:\r\n pass\r\n\r\n@overload\r\ndef foo(x: str) -> list[str]:\r\n pass\r\n\r\ndef foo(x: int | str) -> list[float] | list[str]:\r\n if isinstance(x, int):\r\n return [1 / x]\r\n return [x[::-1]]\r\n\r\nbar = transform(foo)\r\n\r\nassert bar(2) == 0.5\r\nassert bar(\"abc\") == \"cba\"\r\n\r\nreveal_type(foo)\r\nreveal_type(bar)\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nI would expect the overloads to be preserved on the decorated function, e.g.\r\n```\r\nmain.py:31: note: Revealed type is \"Overload(def (x: builtins.int) -> builtins.list[builtins.float], def (x: builtins.str) -> builtins.list[builtins.str])\"\r\nmain.py:32: note: Revealed type is Overload(def (x: builtins.int) -> builtins.float, def (x: builtins.str) -> builtins.str)\"\r\n```\r\n\r\n**Actual Behavior**\r\n\r\n```\r\nmain.py:29: error: Argument 1 has incompatible type \"str\"; expected \"int\" [arg-type]\r\nmain.py:31: note: Revealed type is \"Overload(def (x: builtins.int) -> builtins.list[builtins.float], def (x: builtins.str) -> builtins.list[builtins.str])\"\r\nmain.py:32: note: Revealed type is \"def (x: builtins.int) -> builtins.float\"\r\n```\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.4.1\r\n- Python version used: 3.10.10\r\n","author":{"url":"https://github.com/maffoo","@type":"Person","name":"maffoo"},"datePublished":"2023-07-22T05:32:49Z","url":"https://github.com/python/mypy/issues/15737"}
| 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:86d57c41-06b7-f801-c77a-737121e63a1e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D2BC:1A7566:3DFA5B:5393FD:6B002AB9 |
| html-safe-nonce | d63291ea8908b144ae74597b5cd9eca7ec10455f93b5c47a94895b5ef79b8ded |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMkJDOjFBNzU2NjozREZBNUI6NTM5M0ZEOjZCMDAyQUI5IiwidmlzaXRvcl9pZCI6IjQ3ODIzMTc1MDMxNjAyNjU0MDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 52b710f2e16a0429462beddabe98aa923fc33a2a58e4206270c45a9e06e8425e |
| hovercard-subject-tag | issue:1816632090 |
| 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/mypy/15737/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ebc5154b690f564543c69ac11329023fc1123c079d247a7c755cded4aef84acc/python/mypy/issues/15737 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ebc5154b690f564543c69ac11329023fc1123c079d247a7c755cded4aef84acc/python/mypy/issues/15737 |
| og:image:alt | Bug Report When wrapping an overloaded function and using ParamSpec to preserve the call signature, only the first overload is preserved on the resulting function. To Reproduce See: https://gist.gi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maffoo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width