Title: SelfType or another way to spell "type of self" (or, How to define a copy() function) · Issue #1212 · python/mypy · GitHub
Open Graph Title: SelfType or another way to spell "type of self" (or, How to define a copy() function) · Issue #1212 · python/mypy
X Title: SelfType or another way to spell "type of self" (or, How to define a copy() function) · Issue #1212 · python/mypy
Description: A colleague and I were wondering how to define a copy() method in a base class so that when called on an instance of a subclass it is known that it returns an instance of that subclass. We found the following solution: T = TypeVar('T') c...
Open Graph Description: A colleague and I were wondering how to define a copy() method in a base class so that when called on an instance of a subclass it is known that it returns an instance of that subclass. We found th...
X Description: A colleague and I were wondering how to define a copy() method in a base class so that when called on an instance of a subclass it is known that it returns an instance of that subclass. We found th...
Opengraph URL: https://github.com/python/mypy/issues/1212
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SelfType or another way to spell \"type of self\" (or, How to define a copy() function)","articleBody":"A colleague and I were wondering how to define a copy() method in a base class so that when called on an instance of a subclass it is known that it returns an instance of that subclass. We found the following solution:\n\n```\nT = TypeVar('T')\nclass Copyable:\n def copy(self: T) -> T:\n return self.__class__() # type: ignore\nclass X(Copyable):\n def foo(self):\n pass\nx = X()\nx.foo()\nxx = x.copy()\nxx.foo() # OK\n```\n\nNote that the `# type: ignore` is required for two reasons: you can't call `self.__class__()`, _and_ it doesn't like the return type. Try e.g. `return self` and you get\n\n```\nx.py:5: error: Incompatible return value type: expected T`-1, got x.Copyable\n```\n\nIt works in `--py2` mode too:\n\n```\n def copy(self):\n # type: (T) -> T\n return self.__class__() # type: ignore\n```\n\n**UPDATE: This doesn't actually work. :-( See next comment.**\n","author":{"url":"https://github.com/gvanrossum","@type":"Person","name":"gvanrossum"},"datePublished":"2016-02-10T03:44:23Z","url":"https://github.com/python/mypy/issues/1212"}
| 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:7d6eb4e8-efd6-2bc0-6e81-b9a8b391c0df |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9FEE:157BA1:EE8F4:144FDD:6B001D9A |
| html-safe-nonce | 0cbda0e0ef4cf3ab432ff6ca8ba337bebf6e398bd7b709c27bc77ead24ef4161 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkVFOjE1N0JBMTpFRThGNDoxNDRGREQ6NkIwMDFEOUEiLCJ2aXNpdG9yX2lkIjoiODYzNzQwMzU4NTk2Mjg0MzU0NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 133b0f0effec418be7a6fe6e081209b4ba508e61eeb2c22389e3ae8ddeb74969 |
| hovercard-subject-tag | issue:132599978 |
| 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/1212/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b092ca4309372583ae5629ac9d0eb4ecd30d31a1c479c73f879a19a5db3df8ce/python/mypy/issues/1212 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b092ca4309372583ae5629ac9d0eb4ecd30d31a1c479c73f879a19a5db3df8ce/python/mypy/issues/1212 |
| og:image:alt | A colleague and I were wondering how to define a copy() method in a base class so that when called on an instance of a subclass it is known that it returns an instance of that subclass. We found th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gvanrossum |
| 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