Title: Extending a Protocol leads to unsafe behavior · Issue #8005 · python/mypy · GitHub
Open Graph Title: Extending a Protocol leads to unsafe behavior · Issue #8005 · python/mypy
X Title: Extending a Protocol leads to unsafe behavior · Issue #8005 · python/mypy
Description: Are you reporting a bug, or opening a feature request? bug Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro. fro...
Open Graph Description: Are you reporting a bug, or opening a feature request? bug Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to si...
X Description: Are you reporting a bug, or opening a feature request? bug Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to si...
Opengraph URL: https://github.com/python/mypy/issues/8005
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Extending a Protocol leads to unsafe behavior","articleBody":"* Are you reporting a bug, or opening a feature request?\r\nbug\r\n\r\n* Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro.\r\n```\r\nfrom typing import Protocol\r\n\r\nclass P(Protocol):\r\n def foo(self) -> int: ...\r\n\r\nclass A(P): # mypy does not flag this\r\n pass\r\n\r\nclass B:\r\n pass\r\n\r\ntest: P = A() # mypy does not flag this\r\ntest = B() # mypy flags this\r\n\r\nprint(A().foo() + 5) # both error at runtime\r\nprint(B().foo() + 5)\r\n```\r\n\r\n* What is the actual behavior/output?\r\n`A().foo() + 5` crashes at runtime with `TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'`\r\n`B` is correctly type checked and rejected.\r\n\r\n* What is the behavior/output you expect?\r\nAccording to https://mypy.readthedocs.io/en/latest/protocols.html#defining-subprotocols-and-subclassing-protocols, \"Explicitly including a protocol as a base class ... forces mypy to verify that your class implementation is actually compatible with the protocol.\"\r\nI would expect extending a Protocol to be similar to implementing an interface in Java.\r\nIn this particular example, mypy should ideally detect that `A` does not fill in the stub method from `P`, like it does for `B`. Alternatively, if that is not possible, perhaps extending a protocol should not be allowed.\r\n\r\n* What are the versions of mypy and Python you are using? Do you see the same issue after installing mypy from Git master?\r\nPython 3.8.0\r\nmypy 0.750+dev.d9dea5f3ad30bb449e4167b3e8476684ded0482e\r\n\r\n* What are the mypy flags you are using? (For example --strict-optional)\r\n`mypy --strict test.py`","author":{"url":"https://github.com/jingw","@type":"Person","name":"jingw"},"datePublished":"2019-11-23T02:12:25Z","url":"https://github.com/python/mypy/issues/8005"}
| 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:cb22039a-314d-ec6e-029f-ecaaaba968fd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D41E:2A087:4B1770:648C5A:6B002F29 |
| html-safe-nonce | c7c72e2435ce075f38a4f1a2a7cd4352b28661a9db399cccb275a16cb7e99f3e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENDFFOjJBMDg3OjRCMTc3MDo2NDhDNUE6NkIwMDJGMjkiLCJ2aXNpdG9yX2lkIjoiNjQ2NTEzMDg5ODU1ODgyNDIzMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3ca50f319e8b35ecd1eac6686c473973debd6846a361de2edea09127e9600a49 |
| hovercard-subject-tag | issue:527495312 |
| 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/8005/issue_layout |
| twitter:image | https://opengraph.githubassets.com/71b1306e739b321e388e7b43d186ed5a478378ee49c60b2e23bf5211cd7bb6c8/python/mypy/issues/8005 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/71b1306e739b321e388e7b43d186ed5a478378ee49c60b2e23bf5211cd7bb6c8/python/mypy/issues/8005 |
| og:image:alt | Are you reporting a bug, or opening a feature request? bug Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to si... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jingw |
| 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