Title: mypy failed to return correct overloaded func type when decorate func accepts more than one parameter · Issue #18167 · python/mypy · GitHub
Open Graph Title: mypy failed to return correct overloaded func type when decorate func accepts more than one parameter · Issue #18167 · python/mypy
X Title: mypy failed to return correct overloaded func type when decorate func accepts more than one parameter · Issue #18167 · python/mypy
Description: Bug Report mypy failed to return the correct overloaded func type when decorate func accepts more than one parameter. To Reproduce from functools import wraps from typing import overload, Any, TypeVar, Callable, Type from typing_extensio...
Open Graph Description: Bug Report mypy failed to return the correct overloaded func type when decorate func accepts more than one parameter. To Reproduce from functools import wraps from typing import overload, Any, Type...
X Description: Bug Report mypy failed to return the correct overloaded func type when decorate func accepts more than one parameter. To Reproduce from functools import wraps from typing import overload, Any, Type...
Opengraph URL: https://github.com/python/mypy/issues/18167
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy failed to return correct overloaded func type when decorate func accepts more than one parameter","articleBody":"**Bug Report**\r\n\r\nmypy failed to return the correct overloaded func type when decorate func accepts more than one parameter.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom functools import wraps\r\nfrom typing import overload, Any, TypeVar, Callable, Type\r\n\r\nfrom typing_extensions import ParamSpec, reveal_type\r\n\r\n\r\n@overload\r\ndef foo(a: str, /) -> str:\r\n pass\r\n\r\n\r\n@overload\r\ndef foo(a: int, /) -> int:\r\n pass\r\n\r\n\r\ndef foo(a: Any) -> Any:\r\n return a\r\n\r\n\r\nP = ParamSpec(\"P\")\r\nT = TypeVar(\"T\")\r\nR = TypeVar(\"R\")\r\n\r\n\r\ndef forbid_return_type(\r\n func: Callable[P, T],\r\n tp: Type[Any],\r\n /,\r\n) -> Callable[P, T]:\r\n @wraps(func)\r\n def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:\r\n result = func(*args, **kwargs)\r\n\r\n if isinstance(result, tp):\r\n raise TypeError(f\"Return type {tp} is forbidden\")\r\n\r\n return result\r\n\r\n return wrapper\r\n\r\n\r\nfoo_as_int = forbid_return_type(foo, str)\r\n\r\nreveal_type(foo_as_int(1))\r\nreveal_type(foo_as_int(\"1\"))\r\n```\r\n\r\n**Expected Behavior**\r\n\r\n```py\r\nnote: Revealed type is \"builtins.int\"\r\nnote: Revealed type is \"builtins.str\"\r\n```\r\n\r\n**Actual Behavior**\r\n\r\n```py\r\nnote: Revealed type is \"builtins.str\"\r\nerror: Argument 1 has incompatible type \"int\"; expected \"str\" [arg-type]\r\nnote: Revealed type is \"builtins.str\"\r\n```\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: mypy 1.14.0+dev\r\n- Mypy command-line flags: -\r\n- Mypy configuration options from `mypy.ini` (and other config files): -\r\n- Python version used: 3.8.19\r\n\r\n\r\nBoth codes bellow work as expected:\r\n```py\r\nfrom functools import wraps\r\nfrom typing import overload, Any, TypeVar, Callable\r\n\r\nfrom typing_extensions import ParamSpec, reveal_type\r\n\r\n\r\n@overload\r\ndef foo(a: str, /) -> str:\r\n pass\r\n\r\n\r\n@overload\r\ndef foo(a: int, /) -> int:\r\n pass\r\n\r\n\r\ndef foo(a: Any) -> Any:\r\n return a\r\n\r\n\r\nP = ParamSpec(\"P\")\r\nT = TypeVar(\"T\")\r\nR = TypeVar(\"R\")\r\n\r\n\r\ndef forbid_return_type(\r\n func: Callable[P, T],\r\n) -> Callable[P, T]:\r\n @wraps(func)\r\n def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:\r\n result = func(*args, **kwargs)\r\n return result\r\n\r\n return wrapper\r\n\r\n\r\nfoo_as_int = forbid_return_type(foo)\r\n\r\nreveal_type(foo_as_int(1))\r\nreveal_type(foo_as_int(\"1\"))\r\n```\r\n\r\n```py\r\nfrom functools import wraps\r\nfrom typing import Any, TypeVar, Callable, Type, Union\r\n\r\nfrom typing_extensions import ParamSpec, reveal_type\r\n\r\n\r\ndef foo(a: Union[int, str]) -> Union[int, str]:\r\n return a\r\n\r\n\r\nP = ParamSpec(\"P\")\r\nT = TypeVar(\"T\")\r\nR = TypeVar(\"R\")\r\n\r\n\r\ndef forbid_return_type(\r\n func: Callable[P, T],\r\n tp: Type[Any],\r\n /,\r\n) -> Callable[P, T]:\r\n @wraps(func)\r\n def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:\r\n result = func(*args, **kwargs)\r\n\r\n if isinstance(result, tp):\r\n raise TypeError(f\"Return type {tp} is forbidden\")\r\n\r\n return result\r\n\r\n return wrapper\r\n\r\n\r\nfoo_as_int = forbid_return_type(foo, str)\r\n\r\nreveal_type(foo_as_int(1))\r\nreveal_type(foo_as_int(\"1\"))\r\n```\r\n\r\nSo it's not working only when the function accepts an overloaded function and another argument.\r\n\r\nI checked with `pyright` and it correctly handles code.\r\n\r\n```js\r\n /Users/yuriikarabas/projects/mypy/temp.py:45:13 - information: Type of \"foo_as_int(1)\" is \"int\"\r\n /Users/yuriikarabas/projects/mypy/temp.py:46:13 - information: Type of \"foo_as_int(\"1\")\" is \"str\"\r\n0 errors, 0 warnings, 2 informations \r\n```\r\n","author":{"url":"https://github.com/uriyyo","@type":"Person","name":"uriyyo"},"datePublished":"2024-11-19T09:53:58Z","url":"https://github.com/python/mypy/issues/18167"}
| 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:912fd1b0-12d5-529e-9f56-b277eecf5d57 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC28:77A5A:2DF0224:3CD5265:6B00264D |
| html-safe-nonce | 3d28b264cc4abd6d88c8b757fd37a185f2a69cc0515776d640b0a70b54e98811 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzI4Ojc3QTVBOjJERjAyMjQ6M0NENTI2NTo2QjAwMjY0RCIsInZpc2l0b3JfaWQiOiI3MzUxNTU2MTEzNjQzMjcxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6c81514301ec93c9ab921079c7c732515255c359ec648cfecc53fe6ecccb3a60 |
| hovercard-subject-tag | issue:2671543318 |
| 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/18167/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2036804d1348e0cfd92d11305b0564b54eb20136dc0ad4ce9178bcb024367753/python/mypy/issues/18167 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2036804d1348e0cfd92d11305b0564b54eb20136dc0ad4ce9178bcb024367753/python/mypy/issues/18167 |
| og:image:alt | Bug Report mypy failed to return the correct overloaded func type when decorate func accepts more than one parameter. To Reproduce from functools import wraps from typing import overload, Any, Type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | uriyyo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width