Title: Make `builtins.property` generic · Issue #985 · python/typing · GitHub
Open Graph Title: Make `builtins.property` generic · Issue #985 · python/typing
X Title: Make `builtins.property` generic · Issue #985 · python/typing
Description: Hi! Here's how property type is defined right now from a typing perspective: from typing import Any, Callable class property(object): fget: Callable[[Any], Any] | None fset: Callable[[Any, Any], None] | None fdel: Callable[[Any], None] |...
Open Graph Description: Hi! Here's how property type is defined right now from a typing perspective: from typing import Any, Callable class property(object): fget: Callable[[Any], Any] | None fset: Callable[[Any, Any], No...
X Description: Hi! Here's how property type is defined right now from a typing perspective: from typing import Any, Callable class property(object): fget: Callable[[Any], Any] | None fset: Callable[[Any, Any]...
Opengraph URL: https://github.com/python/typing/issues/985
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Make `builtins.property` generic","articleBody":"Hi!\r\n\r\nHere's how `property` type is defined right now from a typing perspective: \r\n\r\n```python\r\nfrom typing import Any, Callable\r\n\r\nclass property(object):\r\n fget: Callable[[Any], Any] | None\r\n fset: Callable[[Any, Any], None] | None\r\n fdel: Callable[[Any], None] | None\r\n def __init__(\r\n self,\r\n fget: Callable[[Any], Any] | None = ...,\r\n fset: Callable[[Any, Any], None] | None = ...,\r\n fdel: Callable[[Any], None] | None = ...,\r\n doc: str | None = ...,\r\n ) -> None: ...\r\n def getter(self, __fget: Callable[[Any], Any]) -> property: ...\r\n def setter(self, __fset: Callable[[Any, Any], None]) -> property: ...\r\n def deleter(self, __fdel: Callable[[Any], None]) -> property: ...\r\n def __get__(self, __obj: Any, __type: type | None = ...) -> Any: ...\r\n def __set__(self, __obj: Any, __value: Any) -> None: ...\r\n def __delete__(self, __obj: Any) -> None: ...\r\n```\r\n\r\nSource: https://github.com/python/typeshed/blob/12b79f64d7241024447eae8ca5d52779cca94ee7/stdlib/builtins.pyi#L947\r\n\r\nNote, that it is not generic. But, this is a type that surely can be modeled as a generic type.\r\n\r\n## Motivation\r\n\r\nWhy? Because `@property` works with two major scenarios: getting some inner type and setting some extrenal type. This can be represented as `property[GetType, SetType]`. \r\n\r\nIt might not seem very important to end users, because of how `@property`s are used, but it quite importatnt for type-checkers and type stubs.\r\n\r\nHere are some problems that we have in `mypy` with properties:\r\n- Because `@property` is so special right now, `mypy` does not support decorated properties: https://github.com/python/mypy/issues/1362\r\n- Mypy does not support `@property` with different `@setter` type: https://github.com/python/mypy/issues/3004 and https://github.com/python/mypy/pull/11643\r\n\r\nYes, we can work around the fact we cannot simply express `property[GetType, SetType]`, but I don't think that missing this important detail is good for the typing system in the long run.\r\nWithout this change we cannot treat `@property` as a regular descriptor, which follows the same rules.\r\n\r\n## Reference implementation\r\n\r\nImplementing generic `@property` will require several changes.\r\n\r\n### CPython\r\n\r\nI guess we would need something similar as we did in https://www.python.org/dev/peps/pep-0585/\r\n\r\nSomething like adding:\r\n\r\n```c\r\n{\"__class_getitem__\", Py_GenericAlias, METH_O\\|METH_CLASS, PyDoc_STR(\"See PEP 585\")},\r\n```\r\n\r\nHere: https://github.com/python/cpython/blob/c8749b578324ad4089c8d014d9136bc42b065343/Objects/descrobject.c#L1567-L1568\r\n\r\n### typeshed\r\n\r\nI am sure that this code can be improved, but it does its job as a reference / demo:\r\n\r\n```python\r\nfrom typing import Any, Callable, Generic\r\n\r\nG = TypeVar('G') # think about variance\r\nS = TypeVar('S')\r\n\r\nclass property(Generic[G, S]):\r\n fget: Callable[[Any], G] | None\r\n fset: Callable[[Any, S], None] | None\r\n fdel: Callable[[Any], None] | None\r\n def __init__(\r\n self,\r\n fget: Callable[[Any], G] | None = ...,\r\n fset: Callable[[Any, S], None] | None = ...,\r\n fdel: Callable[[Any], None] | None = ...,\r\n doc: str | None = ...,\r\n ) -> None: ...\r\n def getter(self, __fget: Callable[[Any], G]) -> property: ...\r\n def setter(self, __fset: Callable[[Any, S], None]) -> property: ...\r\n def deleter(self, __fdel: Callable[[Any], None]) -> property: ...\r\n def __get__(self, __obj: Any, __type: type | None = ...) -> Any: ...\r\n def __set__(self, __obj: Any, __value: Any) -> None: ...\r\n def __delete__(self, __obj: Any) -> None: ...\r\n```\r\n\r\nPlayground: https://mypy-play.net/?mypy=latest&python=3.10&flags=strict%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators&gist=e7cec4565d416a0981d4a65e619f1be6\r\n\r\nIt almost works as-is, but `@property.setter` part requires some extra handling from type-checkers.\r\n\r\n## Backwards compatibility\r\n\r\nAs far as I understand CPython's development process, we can add this new feature to 3.11 only.\r\nI think that using `from __future__ import annotations` should also work for `@property` the same way [PEP585](https://www.python.org/dev/peps/pep-0585/#implementation) defines.\r\n\r\n## Next steps\r\n\r\nIf others agree with me that `property[GetType, SetType]` is a good thing to have, I can:\r\n- [x] Open new BPO issue for this feature: https://bugs.python.org/issue46162\r\n- [x] Add runtime support for `property.__class_getitem__` to CPython: https://github.com/python/cpython/pull/30238\r\n- [ ] Send a PR to `typeshed` with the typing stub\r\n- [ ] Maybe update PEP585?\r\n\r\nLooking forward to your feedback! 😊 \r\n\r\n## Related issues\r\n\r\nRefs:\r\n- https://github.com/python/typing/issues/594 (I think that adding `typing.Property` does not really make much sense, since `python>=3.9` already supports other builtin generic types).\r\n- https://github.com/python/typing/issues/758","author":{"url":"https://github.com/sobolevn","@type":"Person","name":"sobolevn"},"datePublished":"2021-12-19T18:20:52Z","url":"https://github.com/python/typing/issues/985"}
| 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:0e2c0297-e946-b582-2570-6d5f90200e22 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D844:2F2EB9:2D9CC2D:3C6484A:6B0023C0 |
| html-safe-nonce | 5dc7688941a8cd74d9a69fcf855db87402eb50a11e01a43b03406848039ac740 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODQ0OjJGMkVCOToyRDlDQzJEOjNDNjQ4NEE6NkIwMDIzQzAiLCJ2aXNpdG9yX2lkIjoiNDc0ODUxMzU3NjUxMDAzODk3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d1a2a8eb734884977c86980ab0805f5c172d682def314cde4899dc31e0fd8145 |
| hovercard-subject-tag | issue:1084160579 |
| 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/985/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1214b37a4fccc8b0a8b37d501b0e918455a74e1bc76849c5511c4a8ded690af7/python/typing/issues/985 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1214b37a4fccc8b0a8b37d501b0e918455a74e1bc76849c5511c4a8ded690af7/python/typing/issues/985 |
| og:image:alt | Hi! Here's how property type is defined right now from a typing perspective: from typing import Any, Callable class property(object): fget: Callable[[Any], Any] | None fset: Callable[[Any, Any], No... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sobolevn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width