Title: Custom property/descriptor and base classes · Issue #5803 · python/mypy · GitHub
Open Graph Title: Custom property/descriptor and base classes · Issue #5803 · python/mypy
X Title: Custom property/descriptor and base classes · Issue #5803 · python/mypy
Description: I am trying to get this code working: from abc import ABC, abstractmethod from typing import Any class DataProperty: def __get__(self, instance: Any, owner: Any) -> str: return "X" class BadBase1(ABC): @property @abstractmethod def id(se...
Open Graph Description: I am trying to get this code working: from abc import ABC, abstractmethod from typing import Any class DataProperty: def __get__(self, instance: Any, owner: Any) -> str: return "X" class BadBase1(A...
X Description: I am trying to get this code working: from abc import ABC, abstractmethod from typing import Any class DataProperty: def __get__(self, instance: Any, owner: Any) -> str: return "X" cla...
Opengraph URL: https://github.com/python/mypy/issues/5803
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Custom property/descriptor and base classes","articleBody":"I am trying to get this code working:\r\n\r\n```python\r\nfrom abc import ABC, abstractmethod\r\nfrom typing import Any\r\n\r\n\r\nclass DataProperty:\r\n def __get__(self, instance: Any, owner: Any) -> str:\r\n return \"X\"\r\n\r\n\r\nclass BadBase1(ABC):\r\n @property\r\n @abstractmethod\r\n def id(self) -> str:\r\n raise NotImplementedError()\r\n\r\n\r\nclass BadBase2(ABC):\r\n id: str\r\n\r\n\r\nclass GoodBase(ABC):\r\n pass\r\n\r\n\r\nclass BadDerived1(BadBase1):\r\n id = DataProperty() # Incompatible types in assignment (expression has type \"DataProperty\", base class \"BadBase1\" defined the type as \"str\")\r\n\r\n\r\nclass BadDerived2(BadBase2):\r\n id = DataProperty() # Incompatible types in assignment (expression has type \"DataProperty\", base class \"BadBase2\" defined the type as \"str\")\r\n\r\n\r\nclass GoodDerived(GoodBase):\r\n id = DataProperty()\r\n\r\n\r\nreveal_type(BadDerived1().id) # Revealed type is 'Any'\r\nreveal_type(BadDerived2().id) # Revealed type is 'Any'\r\nreveal_type(GoodDerived().id) # Revealed type is 'builtins.str'\r\n```\r\n\r\nI run mypy without any flags, i.e. \"mypy test.py\". The mypy version is 0.641, using Python 3.6.5.\r\n\r\nBasically I want to have some kind of base class that says \"any subclasses will have a member called `id` and the type of this member is `str`.\r\n\r\nThe two cases labelled with \"Bad\" result in the errors shown in the comments (\"incompatible types in assignment\" and \"revealed type is 'Any'\". \r\n\r\nThe only case that works is the \"Good\" one, but it only works because the base class does not define anything.\r\n\r\nAm I doing something wrong or is this a bug/limitation in mypy?","author":{"url":"https://github.com/mm-matthias","@type":"Person","name":"mm-matthias"},"datePublished":"2018-10-19T11:51:54Z","url":"https://github.com/python/mypy/issues/5803"}
| 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:2423db0b-fbcf-f900-97ff-d71c2a10d05b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A0DE:14B08B:FD9AF0:14B0481:6AFFF6B4 |
| html-safe-nonce | c4408dff2e0eef10d716b0f6a16c25f498345597f931093bc648c0d5c67f64a7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMERFOjE0QjA4QjpGRDlBRjA6MTRCMDQ4MTo2QUZGRjZCNCIsInZpc2l0b3JfaWQiOiI0NDQwNzg0OTcyMjUyNDQ0MzQwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c9474ce5d582e5bf393d72a9cca847835f4132280852f0d25322a06d6e455051 |
| hovercard-subject-tag | issue:371924217 |
| 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/5803/issue_layout |
| twitter:image | https://opengraph.githubassets.com/148dcd8aa41b6aef58db3d2854e280700b4d5b06d464b670b96a753044f15f82/python/mypy/issues/5803 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/148dcd8aa41b6aef58db3d2854e280700b4d5b06d464b670b96a753044f15f82/python/mypy/issues/5803 |
| og:image:alt | I am trying to get this code working: from abc import ABC, abstractmethod from typing import Any class DataProperty: def __get__(self, instance: Any, owner: Any) -> str: return "X" class BadBase1(A... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mm-matthias |
| 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