Title: Automatically use `Self` type even if it doesn't appear in method annotation · Issue #14075 · python/mypy · GitHub
Open Graph Title: Automatically use `Self` type even if it doesn't appear in method annotation · Issue #14075 · python/mypy
X Title: Automatically use `Self` type even if it doesn't appear in method annotation · Issue #14075 · python/mypy
Description: This is a follow up for #14041 This example gives an error: from typing import Self class C: x: Self def meth(self) -> None: self.x = self # E: Incompatible types in assignment (expression has type "C", variable has type "Self") Although...
Open Graph Description: This is a follow up for #14041 This example gives an error: from typing import Self class C: x: Self def meth(self) -> None: self.x = self # E: Incompatible types in assignment (expression has type...
X Description: This is a follow up for #14041 This example gives an error: from typing import Self class C: x: Self def meth(self) -> None: self.x = self # E: Incompatible types in assignment (expression has t...
Opengraph URL: https://github.com/python/mypy/issues/14075
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Automatically use `Self` type even if it doesn't appear in method annotation","articleBody":"This is a follow up for https://github.com/python/mypy/pull/14041\r\n\r\nThis example gives an error:\r\n```python\r\nfrom typing import Self\r\n\r\nclass C:\r\n x: Self\r\n def meth(self) -> None:\r\n self.x = self # E: Incompatible types in assignment (expression has type \"C\", variable has type \"Self\")\r\n```\r\nAlthough it is unfortunate, technically this is required by PEP 484, that says that if no annotation is given, `self` has a type of current class (note also we can't bind self type for `self.x` expression giving it type `C`, as this will open another unsafety). This can be avoided by using e.g. `self: Self` (or generally `Self` type anywhere in the signature).\r\n\r\nIt looks like there can be only two solutions:\r\n* Always infer `Self` type for `self`. This will cause a big performance penalty and may be surprising for users who are not familiar with `Self` (if they use `reveal_type(self)`).\r\n* Use some heuristic and only infer `Self` where otherwise we will have this problem. For example, infer `Self` if an attribute with type containing `Self` is used anywhere in a method.\r\n\r\nI am leaning towards the latter, but didn't want to do this until we get some more experience.\r\n\r\n","author":{"url":"https://github.com/ilevkivskyi","@type":"Person","name":"ilevkivskyi"},"datePublished":"2022-11-12T20:13:36Z","url":"https://github.com/python/mypy/issues/14075"}
| 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:06771322-bf87-4255-d7b6-aaecd35ef9e6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C13A:3B10DD:E04EF7:124934C:6AFFF937 |
| html-safe-nonce | 45cf8a9f2e7707d5275f0cb7bfcf52b77a7734f311e9d0311138602ba10138bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTNBOjNCMTBERDpFMDRFRjc6MTI0OTM0Qzo2QUZGRjkzNyIsInZpc2l0b3JfaWQiOiI3NDg5NjU2MTE1NzY0OTAyOTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c6cc7963d34260e6d2d7d3712c3577fe79eb65aa6a17ed13ac912a076f98cea8 |
| hovercard-subject-tag | issue:1446588545 |
| 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/14075/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7c1da04f884a7cd9fe5d87ac6207edf365bffb59f700616f35fbbff8640f498c/python/mypy/issues/14075 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7c1da04f884a7cd9fe5d87ac6207edf365bffb59f700616f35fbbff8640f498c/python/mypy/issues/14075 |
| og:image:alt | This is a follow up for #14041 This example gives an error: from typing import Self class C: x: Self def meth(self) -> None: self.x = self # E: Incompatible types in assignment (expression has type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ilevkivskyi |
| 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