Title: Protocols with property descriptors require all compliant symbols to use the same implementation. · Issue #9202 · python/mypy · GitHub
Open Graph Title: Protocols with property descriptors require all compliant symbols to use the same implementation. · Issue #9202 · python/mypy
X Title: Protocols with property descriptors require all compliant symbols to use the same implementation. · Issue #9202 · 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. Co...
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 s...
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 s...
Opengraph URL: https://github.com/python/mypy/issues/9202
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Protocols with property descriptors require all compliant symbols to use the same implementation.","articleBody":"\r\n* Are you reporting a bug, or opening a feature request?\r\n\r\nBug.\r\n\r\n* Please insert below the code you are checking with mypy,\r\n or a mock-up repro if the source is private. We would appreciate\r\n if you try to simplify your case to a minimal repro.\r\n\r\nCode snippet attached at end of report.\r\nIt appears to be not possible to define a Protocol describing an object that has an attribute named \"prop1\" with type \"int\".\r\nAny attempt to do so binds the resulting protocol to the implementation chosen in defining the protocol (i.e. must use the same property/descriptor type).\r\n\r\n* What is the actual behavior/output?\r\n\r\n```\r\n % mypy /tmp/test.py\r\n/tmp/test.py:72: error: Incompatible types in assignment (expression has type \"Foo3\", variable has type \"Proto1\")\r\n/tmp/test.py:72: note: Following member(s) of \"Foo3\" have conflicts:\r\n/tmp/test.py:72: note: prop1: expected \"int\", got \"cached_property[int]\"\r\n/tmp/test.py:73: error: Incompatible types in assignment (expression has type \"Foo4\", variable has type \"Proto1\")\r\n/tmp/test.py:73: note: Following member(s) of \"Foo4\" have conflicts:\r\n/tmp/test.py:73: note: prop1: expected \"int\", got \"Property[Foo4, int]\"\r\n/tmp/test.py:75: note: Revealed type is 'def (self: test.Foo1) -> builtins.int'\r\n/tmp/test.py:76: note: Revealed type is 'builtins.int'\r\n/tmp/test.py:77: note: Revealed type is 'functools.cached_property[builtins.int*]'\r\n/tmp/test.py:78: note: Revealed type is 'test.Property[test.Foo4*, builtins.int*]'\r\nFound 2 errors in 1 file (checked 1 source file)\r\n```\r\n\r\n* What is the behavior/output you expect?\r\n\r\nNo errors should be reported.\r\n\r\n* What are the versions of mypy and Python you are using?\r\n\r\nmypy == 0.770 , Python=3.8.4.\r\n\r\n* What are the mypy flags you are using? (For example --strict-optional)\r\n\r\nNone.\r\n\r\n```\r\nimport functools\r\nfrom typing import Callable, cast, Generic, overload, Protocol, Type, TypeVar, Optional, TYPE_CHECKING, Union\r\n\r\nT = TypeVar('T')\r\nV = TypeVar('V')\r\n\r\n\r\n# Arbitrary descriptor implementation.\r\nclass Property(Generic[T, V]):\r\n def __init__(self, fn: Callable[[T], V]):\r\n self.fn = fn\r\n\r\n @overload\r\n def __get__(self, obj: T, owner: Type[T]) -> V: ...\r\n\r\n @overload\r\n def __get__(self, obj: None, owner: Type[T]) -> Property[T, V]: ... \r\n \r\n \r\n def __get__(self, obj: Optional[T], owner: Type[T]) -> Union[Property[T, V], V]:\r\n if obj is None:\r\n return self\r\n return self.fn(obj)\r\n\r\n\r\n\r\n# Protocols that define pretty much the same thing.\r\nclass Proto1(Protocol):\r\n @property\r\n def prop1(self) -> int:\r\n ...\r\n\r\nclass Proto2(Protocol):\r\n prop1: int\r\n\r\nclass Proto3(Protocol):\r\n @functools.cached_property\r\n def prop1(self) -> int:\r\n ...\r\n\r\nclass Proto4(Protocol):\r\n @Property\r\n def prop1(self) -> int:\r\n ... \r\n\r\n# Implementation of each protocol above\r\nclass Foo1:\r\n @property\r\n def prop1(self) -> int:\r\n return 1\r\n\r\nclass Foo2:\r\n def __init__(self, x: int):\r\n self.prop1 = x\r\n \r\nclass Foo3:\r\n @functools.cached_property\r\n def prop1(self) -> int:\r\n return 3\r\n\r\nclass Foo4:\r\n @Property\r\n def prop1(self) -> int:\r\n return 4 \r\n\r\n\r\n# All 4 statements below have RHS values that conform to the behavior\r\n# defined by 'Proto1': objects that have a 'prop1' attribute that is an 'int',\r\n# and that is not settable.\r\n# No errors should be reported.\r\n# It does not appear possible to define a common protocol that all 4 classes\r\n# conform to, even though in code they all have valid common behavior:\r\n# \"intVal: int = x.prop1\"\r\n\r\nx : Proto1 = Foo1()\r\ny : Proto1 = Foo2(2)\r\nz : Proto1 = Foo3()\r\na : Proto1 = Foo4()\r\n\r\nif TYPE_CHECKING:\r\n reveal_type(Foo1.prop1)\r\n reveal_type(Foo2.prop1)\r\n reveal_type(Foo3.prop1)\r\n reveal_type(Foo4.prop1) \r\n\r\n```\r\n","author":{"url":"https://github.com/mostrows2","@type":"Person","name":"mostrows2"},"datePublished":"2020-07-24T22:07:52Z","url":"https://github.com/python/mypy/issues/9202"}
| 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:c5cbc2b4-5979-fb89-c9dd-bc62986dc32e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A750:1334EF:C91681:106C73A:6AFFF669 |
| html-safe-nonce | 1295a6c57fee690a7753fcc3591c4e85705330f4470b54d5b4e8b0955176147a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzUwOjEzMzRFRjpDOTE2ODE6MTA2QzczQTo2QUZGRjY2OSIsInZpc2l0b3JfaWQiOiI0MzIzMzgyNDQ0OTQzMjEwMDkwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 90961e44011ebc8cad5496fd6bd4f8d8e2f3aa6fa6566fe9479ad9f057e434b4 |
| hovercard-subject-tag | issue:665442981 |
| 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/9202/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b8dc898f949d4c7547da3a5ab3f8328f81aabdfcd75dfc795b9667b6d54da7aa/python/mypy/issues/9202 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b8dc898f949d4c7547da3a5ab3f8328f81aabdfcd75dfc795b9667b6d54da7aa/python/mypy/issues/9202 |
| 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 s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mostrows2 |
| 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