Title: non-final frozen attributes (was: Improved read-only attributes of Protocol) · Issue #922 · python/typing · GitHub
Open Graph Title: non-final frozen attributes (was: Improved read-only attributes of Protocol) · Issue #922 · python/typing
X Title: non-final frozen attributes (was: Improved read-only attributes of Protocol) · Issue #922 · python/typing
Description: ☝️ NOTE I have since refined this issue, see #922 (comment) Below I have kept the original text Are there any ideas about making read-only attributes in Protocol easier to define? Immutable objects (e.g. datetime) and frozen dataclasses ...
Open Graph Description: ☝️ NOTE I have since refined this issue, see #922 (comment) Below I have kept the original text Are there any ideas about making read-only attributes in Protocol easier to define? Immutable objects...
X Description: ☝️ NOTE I have since refined this issue, see #922 (comment) Below I have kept the original text Are there any ideas about making read-only attributes in Protocol easier to define? Immutable objects...
Opengraph URL: https://github.com/python/typing/issues/922
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"non-final frozen attributes (was: Improved read-only attributes of Protocol)","articleBody":"---\r\n\r\n## ☝️ NOTE\r\n\r\n**I have since refined this issue, see https://github.com/python/typing/issues/922#issuecomment-979467433**\r\n\r\n_Below I have kept the original text_\r\n\r\n---\r\n\r\nAre there any ideas about making read-only attributes in `Protocol` easier to define? Immutable objects (e.g. `datetime`) and frozen dataclasses are great, but they are painful to integrate with `Protocol`. Which is a shame, because immutability and structural typing are both great!\r\n\r\n# The problem\r\n\r\n```python\r\n\r\nclass SomeProto(Protocol):\r\n a: int\r\n b: str\r\n\r\n@dataclass(frozen=True)\r\nclass SomeDClass:\r\n a: int = 1\r\n b: str = \"\"\r\n c: bool = False\r\n\r\n# not allowed as attributes must also be settable to satisfy protocol\r\nx: SomeProto = SomeDClass()\r\n\r\n# to allow non-settable attributes, we have to define this.\r\n# It's 3 times as long, and less readable\r\nclass SomeProto2(Protocol):\r\n\r\n @property\r\n def a(self) -> int:\r\n ...\r\n\r\n @property\r\n def b(self) -> str:\r\n ...\r\n\r\n# ...but it does work\r\ny: SomeProto2 = SomeDClass()\r\n```\r\n\r\n# Why do something about it\r\n\r\nIn my experience, the use case for purely read-only attributes is far more common than settable attributes. It feels strange that this requires such a more cumbersome and less readable workaround. This can be easily fixed without breaking existing behavior.\r\n\r\n# Proposed solution: allow using `Final` or another annotation\r\n\r\n```python\r\nclass AnotherProto(Protocol):\r\n d: Final[int] # read-only attribute\r\n e: str # read and writeable attribute\r\n```\r\n\r\nCurrently this gives an explicit error (`error: Protocol member cannot be final`). Understandable, because the semantics don't _quite_ match up.\r\n\r\nHowever, PEP591 does seem to leave the door open a bit:\r\n\r\n> The current typing module lacks a way to indicate that a variable will not be assigned to. This is a useful feature in several situations:\r\n> [...]\r\n> Creating a read-only attribute that may not be overridden by subclasses. (`@property` can make an attribute read-only but does not prevent overriding)\r\n\r\nAn alternative would be a separate `ReadOnly[...]` annotation or similar\r\nWhat are your thoughts?\r\n\r\n(possibly related: #920)\r\n","author":{"url":"https://github.com/ariebovenberg","@type":"Person","name":"ariebovenberg"},"datePublished":"2021-11-04T18:09:05Z","url":"https://github.com/python/typing/issues/922"}
| 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:cfdf1375-e4ab-57e1-49e7-f15a442350dd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 886C:1A1C04:22AD562:2F55B05:6B002BBF |
| html-safe-nonce | d73a806731d28ee8efbdc56d5893899c3510c471bebdd0dcc47bd58316f9bd13 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4ODZDOjFBMUMwNDoyMkFENTYyOjJGNTVCMDU6NkIwMDJCQkYiLCJ2aXNpdG9yX2lkIjoiNzIxOTY4NDg5MjE1MjQ0MTc5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d5ec9b544f4a9b8d4e16d9044b5ca71e4ca594c0001ada1b2ee96b821713c038 |
| hovercard-subject-tag | issue:1045053284 |
| 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/typing/922/issue_layout |
| twitter:image | https://opengraph.githubassets.com/12a8ad41f45c74a68371201dce8d662cb61a615314641a972f81c4a97cfd9986/python/typing/issues/922 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/12a8ad41f45c74a68371201dce8d662cb61a615314641a972f81c4a97cfd9986/python/typing/issues/922 |
| og:image:alt | ☝️ NOTE I have since refined this issue, see #922 (comment) Below I have kept the original text Are there any ideas about making read-only attributes in Protocol easier to define? Immutable objects... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ariebovenberg |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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