Title: Mixins with generic self and type bounds · Issue #7191 · python/mypy · GitHub
Open Graph Title: Mixins with generic self and type bounds · Issue #7191 · python/mypy
X Title: Mixins with generic self and type bounds · Issue #7191 · python/mypy
Description: Let's say I have a class class Foo: ... and a typevar with bound from typing import TypeVar T = TypeVar("T", bound=Foo) Now I'd like to define a class that will be used as a mixin: class MixIn: def foo(self: T) -> T: ... I want to expres...
Open Graph Description: Let's say I have a class class Foo: ... and a typevar with bound from typing import TypeVar T = TypeVar("T", bound=Foo) Now I'd like to define a class that will be used as a mixin: class MixIn: def...
X Description: Let's say I have a class class Foo: ... and a typevar with bound from typing import TypeVar T = TypeVar("T", bound=Foo) Now I'd like to define a class that will be used as a mixin...
Opengraph URL: https://github.com/python/mypy/issues/7191
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Mixins with generic self and type bounds","articleBody":"Let's say I have a class\r\n\r\n```python\r\nclass Foo: ...\r\n```\r\n\r\nand a typevar with bound\r\n\r\n```python\r\nfrom typing import TypeVar\r\n\r\nT = TypeVar(\"T\", bound=Foo)\r\n```\r\n\r\nNow I'd like to define a class that will be used as a mixin:\r\n\r\n```python\r\nclass MixIn:\r\n def foo(self: T) -> T: ...\r\n```\r\n\r\nI want to express two things here:\r\n\r\n- `MixIn` should be used only as a mixin with `Foo` (the idea is similar to Scala [self-type](https://docs.scala-lang.org/tour/self-types.html). In other words this should type check:\r\n\r\n ```python\r\n class Bar(Foo, MixIn): ...\r\n ```\r\n\r\n while this shouldn't:\r\n\r\n ```python\r\n class Baz(MixIn): ...\r\n ```\r\n\r\n- `MixIn.foo` returns the same type as `self` (at least `Foo` with `MixIn`).\r\n\r\n\r\nAdditionally it should address issues like #5868 or #5837 with zero boilerplate.\r\n\r\n\r\nRight now (0.720+dev.e479b6d55f927ed7b71d94e8632dc3921b983362) this fails:\r\n\r\n```python\r\nfrom typing import TypeVar\r\n\r\nclass Foo: ...\r\n\r\nT = TypeVar(\"T\", bound=Foo)\r\n\r\nclass MixIn:\r\n def foo(self: T) -> T: ...\r\n```\r\n\r\n```bash\r\nmypy --python-version 3.7 --strict-optional --no-site-packages --show-traceback --no-implicit-optional mixinwithgenericselfandbounds.py\r\nmixinwithgenericselfandbounds.py:8: error: The erased type of self \"mixinwithgenericselfandbounds.Foo\" is not a supertype of its class \"mixinwithgenericselfandbounds.MixIn\"\r\n```","author":{"url":"https://github.com/zero323","@type":"Person","name":"zero323"},"datePublished":"2019-07-10T11:15:53Z","url":"https://github.com/python/mypy/issues/7191"}
| 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:f9ff1399-feb3-7740-6cd8-07aa3e02f9f4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 82EE:2261EB:E29B1C:1266D38:6AFFEE58 |
| html-safe-nonce | 0aec8eadd66402da7e6cbad3b9ed644cde9ca0ab70cd860437f2a8b300f6dfeb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MkVFOjIyNjFFQjpFMjlCMUM6MTI2NkQzODo2QUZGRUU1OCIsInZpc2l0b3JfaWQiOiI2MjI0Mjk1MjYzMTgwMTU2NTA0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c137b16c459d4044294ed05d5694ed533cc4b78e02d1688dadc36373214d04ea |
| hovercard-subject-tag | issue:466250041 |
| 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/7191/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c6ae6e0458ba85d2f7b697a54f7745f7fc773139464996ceaaaa628c918c3175/python/mypy/issues/7191 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c6ae6e0458ba85d2f7b697a54f7745f7fc773139464996ceaaaa628c918c3175/python/mypy/issues/7191 |
| og:image:alt | Let's say I have a class class Foo: ... and a typevar with bound from typing import TypeVar T = TypeVar("T", bound=Foo) Now I'd like to define a class that will be used as a mixin: class MixIn: def... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zero323 |
| 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