Title: Spurious error with `partial` and `ParamSpec` · Issue #15215 · python/mypy · GitHub
Open Graph Title: Spurious error with `partial` and `ParamSpec` · Issue #15215 · python/mypy
X Title: Spurious error with `partial` and `ParamSpec` · Issue #15215 · python/mypy
Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Open Graph Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
X Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Opengraph URL: https://github.com/python/mypy
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Spurious error with `partial` and `ParamSpec`","articleBody":"**Bug Report**\r\n\r\nWhen type checking the code below `mypy` produces a bunch of weird errors, most interesting one being\r\n\r\n```\r\nArgument 1 to \"partial\" has incompatible type \"Callable[[Callable[_P, _T], NamedArg(str, 'name')], Task[_P, _T]]\"; expected \"Callable[..., Task[_P, _T]]\" [arg-type]\r\n```\r\n\r\nI'm not sure, but isn't `Callable[[Something], Task[_P, _T]]` *always* compatible with `Callable[..., Task[_P, _T]]`?\r\n\r\n**To Reproduce**\r\n\r\nYou can also see this [in playground](https://mypy-play.net/?mypy=master&python=3.10&flags=strict&gist=414c88ef9fe3f0804d59dc59a43b7009).\r\n\r\n```python\r\nfrom __future__ import annotations\r\n\r\nfrom dataclasses import dataclass\r\nfrom functools import partial\r\nfrom typing import Any, TypeVar, ParamSpec, Generic, Protocol, Callable\r\n\r\n\r\n_T = TypeVar(\"_T\")\r\n_P = ParamSpec(\"_P\")\r\n\r\n\r\n@dataclass(kw_only=True, repr=True)\r\nclass Task(Generic[_P, _T]):\r\n name: str\r\n function: Callable[_P, _T]\r\n\r\n\r\nclass _TaskRegistrator(Protocol):\r\n def __call__(self, func: Callable[_P, _T], /, *, name: str = ...) -> Task[_P, _T]:\r\n ...\r\n\r\n\r\nclass TaskQueue:\r\n def _register_task(self, func: Callable[_P, _T], *, name: str) -> Task[_P, _T]:\r\n # Actual implementation omitted\r\n return Task(name=name, function=func)\r\n\r\n def task(self, name: str) -> _TaskRegistrator:\r\n return partial(self._register_task, name=name) # E: see below (L29)\r\n \r\n def task2(self, name: str) -> Callable[[Callable[_P, _T]], Task[_P, _T]]:\r\n return partial(self._register_task, name=name) # E: see below (L32)\r\n\r\n\r\n# And now we see that the problem is even unrelated to `self`:\r\n\r\ndef _register_task(func: Callable[_P, _T], *, name: str) -> Task[_P, _T]:\r\n # Actual implementation omitted\r\n return Task(name=name, function=func)\r\n\r\ndef task(name: str) -> _TaskRegistrator:\r\n return partial(_register_task, name=name) # E: see below (L42)\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nFrom my understanding, no `mypy` errors should be pointed out. `pyright` agrees with me (just to test on another typechecker) - all green - but I'm not ready to switch from convenient `mypy` to `pyright` (I personally dislike that project for being non-python in python ecosystem, plus my current project relies heavily on Django plugin).\r\n\r\n**Actual Behavior**\r\n\r\nSeveral errors, all located on lines with `partial(...)` calls.\r\n\r\n```\r\nmain.py:29: error: Argument 1 to \"partial\" has incompatible type \"Callable[[Callable[_P, _T], NamedArg(str, 'name')], Task[_P, _T]]\"; expected \"Callable[..., Task[_P, _T]]\" [arg-type]\r\nmain.py:32: error: Incompatible return value type (got \"partial[Task[_P, _T]]\", expected \"Callable[[Callable[_P, _T]], Task[_P, _T]]\") [return-value]\r\nmain.py:32: note: \"partial[Task[_P, _T]].__call__\" has type \"Callable[[VarArg(Any), KwArg(Any)], Task[_P, _T]]\"\r\nmain.py:32: error: Argument 1 to \"partial\" has incompatible type \"Callable[[Callable[_P, _T], NamedArg(str, 'name')], Task[_P, _T]]\"; expected \"Callable[..., Task[_P, _T]]\" [arg-type]\r\nmain.py:42: error: Argument 1 to \"partial\" has incompatible type \"Callable[[Callable[_P, _T], NamedArg(str, 'name')], Task[_P, _T]]\"; expected \"Callable[..., Task[_P, _T]]\" [arg-type]\r\nFound 4 errors in 1 file (checked 1 source file)\r\n```\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.2.0, then master and all versions since 0.990.\r\n- Mypy command-line flags: none (with and without `--strict` result is the same)\r\n- Mypy configuration options from `mypy.ini` (and other config files): none\r\n- Python version used: 3.10\r\n","author":{"url":"https://github.com/sterliakov","@type":"Person","name":"sterliakov"},"datePublished":"2023-05-10T11:45:57Z","url":"https://github.com/python/mypy/issues/15215"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:9559afb0-756a-f812-b1d5-e4df6cbf3572 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9196:102136:105234F:15E0053:6B005DFC |
| html-safe-nonce | 3107ed8bc4b5f6a5a7e70f1cf23228e1375e34dc3ba35afacd6e14bebe0af20e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MTk2OjEwMjEzNjoxMDUyMzRGOjE1RTAwNTM6NkIwMDVERkMiLCJ2aXNpdG9yX2lkIjoiNDM0ODQwMjc2ODMzNjE1MDAxMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b62501173c2e0317e4896eb9b8115e90347196dae9c76fdf70bb2ab77c607081 |
| hovercard-subject-tag | repository:7053637 |
| 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/python/mypy/issues/15215 |
| twitter:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| og:image:alt | Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| turbo-cache-control | no-cache |
| 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 | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width