Title: Classmethod doesn't pass generic type · Issue #616 · python/typing · GitHub
Open Graph Title: Classmethod doesn't pass generic type · Issue #616 · python/typing
X Title: Classmethod doesn't pass generic type · Issue #616 · python/typing
Description: This may be related to #123. PEP 484, says the type is assumed to be Type[type(cls(...))]. classmethod says a class method receives the class as implicit first argument. I'd assume that Obj[int] is it's own class, different from Obj. How...
Open Graph Description: This may be related to #123. PEP 484, says the type is assumed to be Type[type(cls(...))]. classmethod says a class method receives the class as implicit first argument. I'd assume that Obj[int] is...
X Description: This may be related to #123. PEP 484, says the type is assumed to be Type[type(cls(...))]. classmethod says a class method receives the class as implicit first argument. I'd assume that Obj[int...
Opengraph URL: https://github.com/python/typing/issues/616
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Classmethod doesn't pass generic type","articleBody":" - This may be related to #123.\r\n - [PEP 484, says the type is assumed to be `Type[type(cls(...))]`][0].\r\n - [`classmethod` says a class method receives the class as implicit first argument][1].\r\n\r\nI'd assume that `Obj[int]` is it's own class, different from `Obj`. However `classmethod` doesn't work this way. This means that I can't use `Obj[int].from_str()` to make an `Obj[int]`, much like I can use `Obj.from_str()` to make an `Obj`. This can be seen in the following example.\r\n\r\n```\r\nfrom typing import Generic, TypeVar, Type\r\n\r\nT = TypeVar('T')\r\n\r\n\r\nclass Obj(Generic[T]):\r\n value: T\r\n\r\n def __init__(self, value: T):\r\n self.value = value\r\n\r\n def __str__(self):\r\n name = getattr(self, '__orig_class__', type(self).__name__)\r\n return f'{name}(value={self.value!r})'\r\n\r\n @classmethod\r\n def from_str(cls, value: str):\r\n fn = getattr(cls, '__args__', [str])[0]\r\n return cls(fn(value))\r\n\r\n\r\ndef from_str(cls: Type[Obj[T]], value) -> Obj[T]:\r\n fn = getattr(cls, '__args__', [str])[0]\r\n return cls(fn(value))\r\n\r\n\r\nif __name__ == '__main__':\r\n # untyped\r\n print(Obj.from_str('1'))\r\n print(from_str(Obj, '1'))\r\n\r\n # typed\r\n print(Obj[int].from_str('1'))\r\n print(from_str(Obj[int], '1'))\r\n```\r\n\r\nWhich outputs:\r\n\r\n```\r\nObj(value='1')\r\nObj(value='1')\r\nObj(value='1')\r\n__main__.Obj[int](value=1)\r\n```\r\n\r\nIt seems like `classmethod` and typing agree that it shouldn't be passed the generic type. However I think passing the type as well to be better than having to make all `classmethods` outside the class to support the generic type. It also doesn't look like it'd destroy current code as `cls` seems to work the same way whether it's typed or not.\r\n\r\n [0]: https://www.python.org/dev/peps/pep-0484/#annotating-instance-and-class-methods\r\n [1]: https://docs.python.org/3/library/functions.html#classmethod","author":{"url":"https://github.com/Peilonrayz","@type":"Person","name":"Peilonrayz"},"datePublished":"2019-02-22T15:48:27Z","url":"https://github.com/python/typing/issues/616"}
| 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:0a384c96-6883-4c85-cebd-317f6c4feaaf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9AA2:1BAA33:219E553:2CAC93B:6B004A96 |
| html-safe-nonce | 7e9c3cd373795d59d57922a01fe3f3f12688809796997ba98845df9a2a1a31ef |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QUEyOjFCQUEzMzoyMTlFNTUzOjJDQUM5M0I6NkIwMDRBOTYiLCJ2aXNpdG9yX2lkIjoiNDgyOTgwNTU0Njc3OTYzNDE0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d186809c4a7c37e9f8b33a9a870bfaa4fd16d472ea5505810ced98b1c57962c9 |
| hovercard-subject-tag | issue:413462516 |
| 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/typing/616/issue_layout |
| twitter:image | https://opengraph.githubassets.com/30ae056344175b59d6f6e0ae4ade08293864a623fb1bef9d3a8cd839a8c1d52d/python/typing/issues/616 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/30ae056344175b59d6f6e0ae4ade08293864a623fb1bef9d3a8cd839a8c1d52d/python/typing/issues/616 |
| og:image:alt | This may be related to #123. PEP 484, says the type is assumed to be Type[type(cls(...))]. classmethod says a class method receives the class as implicit first argument. I'd assume that Obj[int] is... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Peilonrayz |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width