Title: Invalid type inferred when overloading __call__ with generic self-type · Issue #8283 · python/mypy · GitHub
Open Graph Title: Invalid type inferred when overloading __call__ with generic self-type · Issue #8283 · python/mypy
X Title: Invalid type inferred when overloading __call__ with generic self-type · Issue #8283 · python/mypy
Description: Seems like mypy uses the first overloaded __call__ signature when using self-type. There is no problem with overloading another dunder method (I tried __get__) or with regular overload (__call__ but without self-type). If use another met...
Open Graph Description: Seems like mypy uses the first overloaded __call__ signature when using self-type. There is no problem with overloading another dunder method (I tried __get__) or with regular overload (__call__ bu...
X Description: Seems like mypy uses the first overloaded __call__ signature when using self-type. There is no problem with overloading another dunder method (I tried __get__) or with regular overload (__call__ bu...
Opengraph URL: https://github.com/python/mypy/issues/8283
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Invalid type inferred when overloading __call__ with generic self-type","articleBody":"Seems like mypy uses the first overloaded `__call__` signature when using self-type. There is no problem with overloading another dunder method (I tried `__get__`) or with regular overload (`__call__` but without self-type). If use another method name, for example `call`, then everything works fine.\r\n\r\nBelow is snippet to illustrate the problem:\r\n\r\n```python\r\nfrom typing import Awaitable, Callable, Generic, TypeVar, overload\r\n\r\nT = TypeVar(\"T\", \"Handler\", \"AsyncHandler\")\r\n\r\nHandler = Callable[[], \"Result\"]\r\nAsyncHandler = Callable[[], Awaitable[\"Result\"]]\r\n\r\n\r\nclass Result:\r\n pass\r\n\r\n\r\nclass Middleware(Generic[T]):\r\n def __init__(self, handler: T):\r\n self.handler: T = handler \r\n\r\n @overload\r\n def __call__(self: \"Middleware[Handler]\") -> Result:\r\n ...\r\n @overload\r\n def __call__(self: \"Middleware[AsyncHandler]\") -> Awaitable[Result]:\r\n ...\r\n\r\n def __call__(self):\r\n return self.handler()\r\n\r\n @overload\r\n def call(self: \"Middleware[Handler]\") -> Result:\r\n ...\r\n @overload\r\n def call(self: \"Middleware[AsyncHandler]\") -> Awaitable[Result]:\r\n ...\r\n\r\n def call(self):\r\n return self.handler()\r\n\r\n\r\ndef handler() -> Result:\r\n return Result()\r\n\r\n\r\nasync def async_handler() -> Result:\r\n return Result()\r\n\r\n\r\nreveal_type(Middleware(handler)) # OK, type is 'Middleware[def () -> Result]'\r\nreveal_type(Middleware(async_handler)) # OK, type is 'Middleware[def () -> Awaitable[Result]]'\r\nreveal_type(Middleware(handler)()) # OK, type is 'Result'\r\nreveal_type(Middleware(handler).call()) # OK, type is 'Result'\r\nreveal_type(Middleware(async_handler)()) # WRONG, type is 'Result', expected 'Awaitable[Result]'\r\nreveal_type(Middleware(async_handler).call()) # OK, type is 'Awaitable[Result]'\r\n```","author":{"url":"https://github.com/unmade","@type":"Person","name":"unmade"},"datePublished":"2020-01-15T17:08:25Z","url":"https://github.com/python/mypy/issues/8283"}
| 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:7a797e01-116c-8d7f-b549-6bfb71d4a3af |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8CCC:34BCDE:F329FC:13F6C2A:6AFFF65C |
| html-safe-nonce | bb3686e9d826caf4cfde9292429c0b7ba77347326bc6bbbe16d8955ea376220c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Q0NDOjM0QkNERTpGMzI5RkM6MTNGNkMyQTo2QUZGRjY1QyIsInZpc2l0b3JfaWQiOiI1NzAxMzYxNjY2MDg0NTY2NjIwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 32e79e4a11ecc7bf40c4a7e8187405807f095fd3be94f586262c3aa8a08b7733 |
| hovercard-subject-tag | issue:550317339 |
| 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/8283/issue_layout |
| twitter:image | https://opengraph.githubassets.com/657617a7330f386eb5dcbd89be4ec358f5ab6723b704d1cd8c6a6166fede3dbc/python/mypy/issues/8283 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/657617a7330f386eb5dcbd89be4ec358f5ab6723b704d1cd8c6a6166fede3dbc/python/mypy/issues/8283 |
| og:image:alt | Seems like mypy uses the first overloaded __call__ signature when using self-type. There is no problem with overloading another dunder method (I tried __get__) or with regular overload (__call__ bu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | unmade |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width