Title: Metaclass type/instance difficulties · Issue #3625 · python/mypy · GitHub
Open Graph Title: Metaclass type/instance difficulties · Issue #3625 · python/mypy
X Title: Metaclass type/instance difficulties · Issue #3625 · python/mypy
Description: Consider the following code from my hypothetical ORM that has three different ways of building an object from typing import TypeVar, Type T = TypeVar('T') class ModelMeta(type): def build(cls: Type[T]) -> T: return cls() def build_other(...
Open Graph Description: Consider the following code from my hypothetical ORM that has three different ways of building an object from typing import TypeVar, Type T = TypeVar('T') class ModelMeta(type): def build(cls: Type...
X Description: Consider the following code from my hypothetical ORM that has three different ways of building an object from typing import TypeVar, Type T = TypeVar('T') class ModelMeta(type): def build(c...
Opengraph URL: https://github.com/python/mypy/issues/3625
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Metaclass type/instance difficulties","articleBody":"Consider the following code from my hypothetical ORM that has three different ways of building an object\r\n\r\n```\r\nfrom typing import TypeVar, Type\r\n\r\nT = TypeVar('T')\r\n\r\n\r\nclass ModelMeta(type):\r\n def build(cls: Type[T]) -> T:\r\n return cls()\r\n\r\n def build_other(cls):\r\n return cls()\r\n\r\n\r\nclass Model(metaclass=ModelMeta):\r\n\r\n @classmethod\r\n def new(cls: Type[T]) -> T:\r\n return cls()\r\n```\r\n\r\nI'm having trouble defining `build` in a way that type checks. In this example, I get an error on `build` about how `Type[T]` is not a supertype of the class `ModelMeta`. \r\n\r\n`build_other` passes the type checker, but usages don't seem to catch anything (implicit Any perhaps?)\r\n\r\n```\r\nclass Dog(Model):\r\n def __init__(self):\r\n print(\"Built Dog\")\r\n\r\n\r\nclass Cat(Model):\r\n def __init__(self):\r\n print(\"Built Cat\")\r\n\r\n\r\nc: Cat = Dog.new() # type check fails\r\nc2: Cat = Dog.build() # type check fails\r\nc3: Cat = Dog.build_other() # passes\r\n```\r\n\r\nConsidering class methods work, it _feels_ like metaclass methods should equally be able to \"unwrap\" the instance type for a class, but there might be subtleties I'm missing here.\r\n\r\nMy wish list here would be:\r\n - have the inverse of `Type` (something like `Instance`) so I could write something like `def build(cls: T) -> Instance[T]`. This would be (I think) a _bit_ clearer.\r\n - Have an intersection type, with which I could declare something like \r\n `def build(cls: (Type[T] & ModelMeta)) -> T`. This would help with a lot of other problems as well. This seems like it would be a useful shortcut to writing subclasses \r\n - Have a magic hook in `subtypes.is_subtype` that would let me write a custom subtype check in some types\r\n \r\nFull context: I'm writing some stubs for Django's ORM, and was hitting issues around the type of `Model.objects.create`. So in my case I end up needing a \"class property\" (hence doing things through the metaclass), which is why the simple `new()` call strategy hasn't been working for me.\r\n\r\nI think this is related to #3438, but I'm having a hard time identifying the link.","author":{"url":"https://github.com/rtpg","@type":"Person","name":"rtpg"},"datePublished":"2017-06-28T02:43:58Z","url":"https://github.com/python/mypy/issues/3625"}
| 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:9aaf6321-be24-b2ac-745e-09880c48136c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E2EC:2E6021:2297FA4:2D01EAD:6AFFEAC1 |
| html-safe-nonce | 5076008c3f87ff0e9221a1e53173e2e6cdb533cb3c32a497e8d702266548c3bd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMkVDOjJFNjAyMToyMjk3RkE0OjJEMDFFQUQ6NkFGRkVBQzEiLCJ2aXNpdG9yX2lkIjoiNjg0MjE5MTE2MjcxNTkyNTE4NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a8b1eb39cf5a97667212e45779d464026d824175d2f22ccc6baa91d4d0c204ff |
| hovercard-subject-tag | issue:239037734 |
| 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/3625/issue_layout |
| twitter:image | https://opengraph.githubassets.com/75086d6d374dbfb4c9b28581ca6ad1186ac8ea9d0887f5e296e70fe464ed4c29/python/mypy/issues/3625 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/75086d6d374dbfb4c9b28581ca6ad1186ac8ea9d0887f5e296e70fe464ed4c29/python/mypy/issues/3625 |
| og:image:alt | Consider the following code from my hypothetical ORM that has three different ways of building an object from typing import TypeVar, Type T = TypeVar('T') class ModelMeta(type): def build(cls: Type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rtpg |
| 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