Title: `ParamSpec` inferred overly wide from protocol · Issue #21384 · python/mypy · GitHub
Open Graph Title: `ParamSpec` inferred overly wide from protocol · Issue #21384 · python/mypy
X Title: `ParamSpec` inferred overly wide from protocol · Issue #21384 · python/mypy
Description: Bug Report When implementing a protocol generic wrt a ParamSpec type inference ends up inferring Any for the param spec in certain cases. Writing code around the way the type checker works we can force it to unify against a properly infe...
Open Graph Description: Bug Report When implementing a protocol generic wrt a ParamSpec type inference ends up inferring Any for the param spec in certain cases. Writing code around the way the type checker works we can f...
X Description: Bug Report When implementing a protocol generic wrt a ParamSpec type inference ends up inferring Any for the param spec in certain cases. Writing code around the way the type checker works we can f...
Opengraph URL: https://github.com/python/mypy/issues/21384
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`ParamSpec` inferred overly wide from protocol","articleBody":"**Bug Report**\n\nWhen implementing a protocol generic wrt a `ParamSpec` type inference ends up inferring `Any` for the param spec in certain cases. Writing code around the way the type checker works we can force it to unify against a properly inferred param spec, but this limits our ability to write nice APIs.\n\n**To Reproduce**\n\n```python\nfrom typing import Protocol, reveal_type\n\nclass Context: pass\n\nclass Namer[**P](Protocol):\n def name_for(self, *args: P.args, **kwargs: P.kwargs) -> str: ...\n\n def execute_on(\n self, ctx: Context, *args: P.args, **kwargs: P.kwargs\n ) -> None: ...\n\nclass Impl0:\n @staticmethod\n def name_for(x: int, y: str) -> str:\n return 'Test'\n @staticmethod\n def execute_on(ctx: Context, x: int, y: str):\n pass\n \nclass Impl1:\n @staticmethod\n def name_for(y: str) -> str:\n return 'Test'\n @staticmethod\n def execute_on(ctx: Context, x: int, y: str):\n pass\n\nclass UseImplFirst[**P, T]:\n def __init__(self, impl: T, *args: P.args, **kwargs: P.kwargs) -> None:\n self.impl = impl\n self.args = args\n self.kwargs = kwargs\n def __call__(self: UseImplFirst[P, Namer[P]]) -> None:\n pass\n\ndef useImplSecond[**P](impl: Namer[P], *args: P.args, **kwargs: P.kwargs):\n pass\n\ndef useImplThird[**P](wrapper: UseImplFirst[P, Namer[P]]) -> None:\n pass\n\n__testImpl0: Namer[[int, str]] = Impl0\nUseImplFirst(Impl0, 0, '0')()\nuseImplSecond(Impl0, 0, '0')\nuseImplThird(UseImplFirst(Impl0, 0, '0'))\n\n__testImpl1: Namer[[int, str]] = Impl1 # Should fail (Fails)\nUseImplFirst(Impl1, 1, '1')() # Should fail (Succeeds)\nuseImplSecond(Impl1, 1, '1') # Should fail (Succeeds)\nuseImplThird(UseImplFirst(Impl1, 1, '1')) # Should fail (Succeeds)\n```\n\n- [Gist](https://gist.github.com/mypy-play/c9554dfdd2f7bf77881b19a142416b99)\n- [Playground (1.12.0)](https://mypy-play.net/?mypy=1.12.0&python=3.12&gist=c9554dfdd2f7bf77881b19a142416b99)\n- [Playground (1.20.2)](https://mypy-play.net/?mypy=1.20.2&python=3.12&gist=c9554dfdd2f7bf77881b19a142416b99)\n\n**Expected Behavior / Actual Behavior**\n\nSee the snippet above.\n\n**Woraround**\nThe following forces mypy to infer the `ParamSpec` first and unify against the protocol implementation later:\n\n```python\nclass UseImpl[**P]:\n def __init__(self, *args: P.args, **kwargs: P.kwargs):\n self.args = args\n self.kwargs = kwargs\n def __call__(self, impl: Namer[P]) -> None:\n pass\n\n\n__testImpl0: Namer[[int, str]] = Impl0\nUseImpl(0, '0')(Impl0)\n\n__testImpl1: Namer[[int, str]] = Impl1 # Should fail (Fails)\nUseImpl(1, '1')(Impl1) # Should fail (fails)\n```\n- [gist](https://gist.github.com/mypy-play/a1f62b867c17ef1985744410d42d5c76)\n- [Playground (1.20.2)](https://mypy-play.net/?mypy=1.20.2&python=3.12&gist=a1f62b867c17ef1985744410d42d5c76)","author":{"url":"https://github.com/iglosiggio","@type":"Person","name":"iglosiggio"},"datePublished":"2026-04-30T16:29:28Z","url":"https://github.com/python/mypy/issues/21384"}
| 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:f925e9a9-aa30-f662-f59a-e8e25091acda |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF26:BACA4:2F2FCAC:3E83BFE:6B002D09 |
| html-safe-nonce | 3a0a2a0af8eaaf5b97899481744f41d11b56eed94343f415f2d970b0c834fba0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjI2OkJBQ0E0OjJGMkZDQUM6M0U4M0JGRTo2QjAwMkQwOSIsInZpc2l0b3JfaWQiOiI2NzEwNzcwMTE5ODUwNjAxNzM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | dbbaca742812b3ef9ca73c14f72a7e30d0002e333f02c58695e839451e400d24 |
| hovercard-subject-tag | issue:4359689159 |
| 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/21384/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dd65e358016288d509198118040e2c2a5fdd7cb9b0677e5b87694e87ca57d498/python/mypy/issues/21384 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dd65e358016288d509198118040e2c2a5fdd7cb9b0677e5b87694e87ca57d498/python/mypy/issues/21384 |
| og:image:alt | Bug Report When implementing a protocol generic wrt a ParamSpec type inference ends up inferring Any for the param spec in certain cases. Writing code around the way the type checker works we can f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iglosiggio |
| 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