Title: Seemingly valid classmethod in subclass of generic generates incompatible return type error · Issue #7401 · python/mypy · GitHub
Open Graph Title: Seemingly valid classmethod in subclass of generic generates incompatible return type error · Issue #7401 · python/mypy
X Title: Seemingly valid classmethod in subclass of generic generates incompatible return type error · Issue #7401 · python/mypy
Description: Are you reporting a bug, or opening a feature request? Encountered an unexpected behavior (bug?) when working with classmethods on generics. It is also possible that I have misunderstood how this is supposed to work. Please insert below ...
Open Graph Description: Are you reporting a bug, or opening a feature request? Encountered an unexpected behavior (bug?) when working with classmethods on generics. It is also possible that I have misunderstood how this i...
X Description: Are you reporting a bug, or opening a feature request? Encountered an unexpected behavior (bug?) when working with classmethods on generics. It is also possible that I have misunderstood how this i...
Opengraph URL: https://github.com/python/mypy/issues/7401
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Seemingly valid classmethod in subclass of generic generates incompatible return type error","articleBody":"*Are you reporting a bug, or opening a feature request?*\r\n\r\nEncountered an unexpected behavior (bug?) when working with classmethods on generics. It is also possible that I have misunderstood how this is supposed to work. \r\n\r\n*Please insert below the code you are checking with mypy,*\r\n\r\nHere is a minimal example:\r\n\r\n```python\r\nT = TypeVar('T')\r\nSelf = TypeVar('Self', bound='Foo')\r\n\r\nclass Foo(Generic[T]):\r\n a: T\r\n\r\n def __init__(self, a: T) -> None:\r\n self.a = a\r\n\r\n @classmethod\r\n def oof(cls: Type[Self], a: T) -> Self:\r\n return cls(a)\r\n\r\n\r\nclass Baz(Foo[str]):\r\n @classmethod\r\n def oof(cls, a: str) -> 'Baz': # error: Return type \"Baz\" of \"oof\" incompatible with return type \u003cnothing> in supertype \"Foo\"\r\n return cls(a)\r\n```\r\n\r\n*What is the actual behavior/output?*\r\n\r\nWith ``mypy==0.720``, this generates:\r\n\r\n```\r\nerror: Return type \"Baz\" of \"oof\" incompatible with return type \r\n```\r\n\r\n*What is the behavior/output you expect?*\r\n\r\nGiven that \r\n\r\n```python\r\nclass Baz(Foo[str]):\r\n pass\r\n\r\nreveal_type(Baz.oof('1')) # note: Revealed type is 'ack.Baz*' \r\n```\r\n\r\nI would expect there to be no error when I override the classmethod in the subclass, so long as it is not conflicting.\r\n\r\nOn the other hand, I can work around this by \r\n\r\n```python\r\nclass Baz(Foo[str]):\r\n @classmethod\r\n def oof(cls: Type[Self], a: str) -> Self:\r\n return cls(a)\r\n```\r\n\r\nHave been spending some time looking at https://github.com/python/mypy/pull/6418 and related issues.","author":{"url":"https://github.com/erickpeirson","@type":"Person","name":"erickpeirson"},"datePublished":"2019-08-28T17:22:34Z","url":"https://github.com/python/mypy/issues/7401"}
| 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:76a1957c-1f78-dfc6-a1d5-aef60932e7b1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 848E:43251:14D2C78:1BA341E:6B001F1F |
| html-safe-nonce | 78bc93ed8c6350543613f43d0ddc6f9dce77efd01790d1e8229d5030138cdc7f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDhFOjQzMjUxOjE0RDJDNzg6MUJBMzQxRTo2QjAwMUYxRiIsInZpc2l0b3JfaWQiOiIyMzIxNzI3MjYwMzU4MzUyNjcxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6f4467806ae1d539ad5e58d6e78eb3f96120fcdf4f2f6705b4f1bf760663bcb6 |
| hovercard-subject-tag | issue:486509553 |
| 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/7401/issue_layout |
| twitter:image | https://opengraph.githubassets.com/43f559d0424eef3f16e63d4e7aaa6e5dbe7c5e6b7682f1866a82159ff085a863/python/mypy/issues/7401 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/43f559d0424eef3f16e63d4e7aaa6e5dbe7c5e6b7682f1866a82159ff085a863/python/mypy/issues/7401 |
| og:image:alt | Are you reporting a bug, or opening a feature request? Encountered an unexpected behavior (bug?) when working with classmethods on generics. It is also possible that I have misunderstood how this i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | erickpeirson |
| 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