Title: classmethod and selftype: incompatible with supertype · Issue #2511 · python/mypy · GitHub
Open Graph Title: classmethod and selftype: incompatible with supertype · Issue #2511 · python/mypy
X Title: classmethod and selftype: incompatible with supertype · Issue #2511 · python/mypy
Description: I am defining some classes capable of returning an (optional) instance of themselves based on a configuration (a dictionary). In the base class I use the new SelfType capability to define the generic classmethod. I wanted to use a metacl...
Open Graph Description: I am defining some classes capable of returning an (optional) instance of themselves based on a configuration (a dictionary). In the base class I use the new SelfType capability to define the gener...
X Description: I am defining some classes capable of returning an (optional) instance of themselves based on a configuration (a dictionary). In the base class I use the new SelfType capability to define the gener...
Opengraph URL: https://github.com/python/mypy/issues/2511
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"classmethod and selftype: incompatible with supertype","articleBody":"I am defining some classes capable of returning an (optional) instance of themselves based on a configuration (a dictionary).\r\n\r\nIn the base class I use the new SelfType capability to define the generic classmethod. I wanted to use a metaclass with abc.abstractclassmethod but mypy doesn't seem to support that.\r\n\r\nIn the subclass I now use the narrower type for cls and the return value but mypy gives me an error:\r\n\r\n```sh\r\n$ ./virtualenv/bin/python3 -m mypy --version\r\nmypy 0.4.6\r\n$ ./virtualenv/bin/python3 -m mypy repro.py \r\nrepro.py: note: In class \"B\":\r\nrepro.py:15: error: Return type of \"from_config\" incompatible with supertype \"A\"\r\n```\r\n\r\nrepro.py:\r\n```Python\r\nfrom typing import TypeVar, Type, Optional\r\n\r\nAT = TypeVar('AT', bound='A')\r\n\r\nclass A:\r\n @classmethod \r\n def from_config(cls: Type[AT], config: dict) -> Optional[AT]:\r\n return None\r\n\r\nclass B(A):\r\n @classmethod\r\n def from_config(cls: Type[B], config: dict) -> Optional[B]:\r\n return B()\r\n```\r\n\r\nI could only make it work by defining B like this which looks quite ugly and requires passing around the type variable:\r\n```Python\r\nfrom typing import cast\r\n\r\nclass B(A):\r\n @classmethod\r\n def from_config(cls: Type[AT], config: dict) -> Optional[AT]:\r\n return cast(AT, B())\r\n```\r\n\r\nIs that the intended result?","author":{"url":"https://github.com/thomascellerier","@type":"Person","name":"thomascellerier"},"datePublished":"2016-11-30T10:45:41Z","url":"https://github.com/python/mypy/issues/2511"}
| 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:c86b9f62-9f53-afd6-0b83-0e6e00f88a76 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E1F0:22E601:C937F:10FDCA:6B001C9F |
| html-safe-nonce | 6480465dacb0cb4c25c2746703e8d5c6b99a9a4245d78e12192b2d23f7cf17d2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMUYwOjIyRTYwMTpDOTM3RjoxMEZEQ0E6NkIwMDFDOUYiLCJ2aXNpdG9yX2lkIjoiOTIxMDYxNjAxMDUyNzI4NDc5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8f7d499634ce5a7c7f83345c8837718e3cdc7c2efb9248fd99e23ad21a2d6c2d |
| hovercard-subject-tag | issue:192532838 |
| 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/2511/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9eb55d99bcd12cd62c72b2a65b5e1978bb985bff91a9b6ad7d53acb483c460de/python/mypy/issues/2511 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9eb55d99bcd12cd62c72b2a65b5e1978bb985bff91a9b6ad7d53acb483c460de/python/mypy/issues/2511 |
| og:image:alt | I am defining some classes capable of returning an (optional) instance of themselves based on a configuration (a dictionary). In the base class I use the new SelfType capability to define the gener... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thomascellerier |
| 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