Title: Treat methods on protocols with empty bodies as abstract · Issue #8395 · python/mypy · GitHub
Open Graph Title: Treat methods on protocols with empty bodies as abstract · Issue #8395 · python/mypy
X Title: Treat methods on protocols with empty bodies as abstract · Issue #8395 · python/mypy
Description: When declaring a protocol it is suggested in the documentation that method that have to be defined in implementations of that protocol can be marked with ... and I find this syntax quite pretty. However, currently the ... is treated as a...
Open Graph Description: When declaring a protocol it is suggested in the documentation that method that have to be defined in implementations of that protocol can be marked with ... and I find this syntax quite pretty. Ho...
X Description: When declaring a protocol it is suggested in the documentation that method that have to be defined in implementations of that protocol can be marked with ... and I find this syntax quite pretty. Ho...
Opengraph URL: https://github.com/python/mypy/issues/8395
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Treat methods on protocols with empty bodies as abstract ","articleBody":"When declaring a protocol it is suggested in the [documentation](https://mypy.readthedocs.io/en/stable/protocols.html#simple-user-defined-protocols) that method that have to be defined in implementations of that protocol can be marked with `...` and I find this syntax quite pretty. However, currently the `...` is treated as a default implementation. Which can lead to confusing behavior. An example is the following:\r\n\r\n```python\r\nfrom typing_extensions import Protocol\r\n\r\n\r\nclass SupportsAdding(Protocol):\r\n def add(self, x: int, y: int) -> int: ...\r\n\r\n\r\nclass Adder(SupportsAdding):\r\n def subtract(self, x: int, y: int) -> int:\r\n return x + y\r\n\r\n\r\nif __name__ == '__main__':\r\n adder: SupportsAdding = Adder()\r\n z = adder.add(1, 2)\r\n print(type(z))\r\n reveal_type(z)\r\n```\r\n\r\nGiven the rather drastic misspelling of `add` as `subtract`, mypy still infers the protocol to be correctly implemented. The result of `reveal_type(z)` is `int`, while it is actually `NoneType`. [PEP 544](https://www.python.org/dev/peps/pep-0544/#explicitly-declaring-implementation) recommends to decorate non-default implementations with `@abstractmethod`. However, this leads to less elegant code and additional runtime checks. Since it seems kind of obvious that `...` is never a default implementation of a method, I would suggest to treat it the same way as protocol methods decorated with `@abstractmethod`.\r\n\r\nI am using mypy version `0.761`.\r\n","author":{"url":"https://github.com/JarnoRFB","@type":"Person","name":"JarnoRFB"},"datePublished":"2020-02-12T17:23:02Z","url":"https://github.com/python/mypy/issues/8395"}
| 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:a2467bba-12dd-eaae-991c-c8fe94126e62 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ADA6:07D0:1E2C5A8:27ECB09:6B004508 |
| html-safe-nonce | e9f0f804764d622ab8ad7c7fedba27303cb5bab4916f3a01aabe22a88e263403 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBREE2OjA3RDA6MUUyQzVBODoyN0VDQjA5OjZCMDA0NTA4IiwidmlzaXRvcl9pZCI6IjQ3NjI2Njg1NjAzNTcwMTY4NDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bc6dd755bcc0baca9de8d3aac7c41f41eff1cf7d6024899d47089613a5d83d41 |
| hovercard-subject-tag | issue:564149652 |
| 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/8395/issue_layout |
| twitter:image | https://opengraph.githubassets.com/15964dd1d262b3a9a5b622feb3ca3fa6714ca1eb53a3e55a90bd0a00f6a0825d/python/mypy/issues/8395 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/15964dd1d262b3a9a5b622feb3ca3fa6714ca1eb53a3e55a90bd0a00f6a0825d/python/mypy/issues/8395 |
| og:image:alt | When declaring a protocol it is suggested in the documentation that method that have to be defined in implementations of that protocol can be marked with ... and I find this syntax quite pretty. Ho... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JarnoRFB |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width