Title: Overloads for property getter (instance vs. through owner) · Issue #5093 · python/mypy · GitHub
Open Graph Title: Overloads for property getter (instance vs. through owner) · Issue #5093 · python/mypy
X Title: Overloads for property getter (instance vs. through owner) · Issue #5093 · python/mypy
Description: The docs for object.__get__ specify that: object.__get__(self, instance, owner) ... owner is always the owner class, while instance is the instance that the attribute was accessed through, or None when the attribute is accessed through t...
Open Graph Description: The docs for object.__get__ specify that: object.__get__(self, instance, owner) ... owner is always the owner class, while instance is the instance that the attribute was accessed through, or None ...
X Description: The docs for object.__get__ specify that: object.__get__(self, instance, owner) ... owner is always the owner class, while instance is the instance that the attribute was accessed through, or None ...
Opengraph URL: https://github.com/python/mypy/issues/5093
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Overloads for property getter (instance vs. through owner)","articleBody":"The [docs for `object.__get__`](https://docs.python.org/3/reference/datamodel.html#object.__get__) specify that:\r\n> `object.__get__(self, instance, owner)`\r\n>\r\n> ... _owner_ is always the owner class, while _instance_ is the instance that the attribute was accessed through, or None when the attribute is accessed through the _owner_.\r\n\r\nThe [typeshed for pynamodb](https://github.com/python/typeshed/blob/master/third_party/2and3/pynamodb/attributes.pyi) has:\r\n```py\r\nclass NumberAttribute(Attribute[Number]):\r\n def __get__(self, instance: Any, owner: Any) -> Number: ...\r\n```\r\n\r\nThis allowed us to use PynamoDB idiomatically so that `MyModel().my_number` is considered a `Union[int, float]` rather than an instance of `NumberAttribute`. The underlying implementation might not use properties, but superficially it works ;)\r\n\r\nRecent versions of PynamoDB added a conditional query syntax like this:\r\n```py\r\nfor my_model in MyModel.query(range_key_condition=MyModel.my_number > 42):\r\n ...\r\n```\r\n\r\nI've tried representing it like this:\r\n```py\r\nclass NumberAttribute(Attribute[Number]):\r\n @overload\r\n def __get__(self, instance: object, owner: Any) -> Number: ...\r\n @overload\r\n def __get__(self, instance: None, owner: Any) -> NumberAttribute: ...\r\n```\r\nbut mypy seems to ignore the latter override.\r\n\r\nTest code:\r\n```py\r\nfrom pynamodb.attributes import NumberAttribute, ListAttribute\r\nfrom pynamodb.models import Model\r\n\r\n\r\nclass MyModel(Model):\r\n my_number = NumberAttribute(hash_key=True)\r\n\r\n\r\nreveal_type(MyModel.my_number)\r\n\r\nm1 = MyModel()\r\nreveal_type(m1.my_number)\r\n```\r\n\r\nResult:\r\n```\r\npynamodb.py:9: error: Revealed type is 'Union[builtins.int, builtins.float]'\r\npynamodb.py:12: error: Revealed type is 'Union[builtins.int, builtins.float]'\r\n```","author":{"url":"https://github.com/ikonst","@type":"Person","name":"ikonst"},"datePublished":"2018-05-22T03:52:20Z","url":"https://github.com/python/mypy/issues/5093"}
| 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:794b04cd-5e23-f502-5da3-2adbb0997fc2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C20E:30DBED:30C09F6:4033555:6B002575 |
| html-safe-nonce | 299fd7ef1e6680ea205320972b64432d2fced5ea3edebc6964ac3c434dff2a80 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMjBFOjMwREJFRDozMEMwOUY2OjQwMzM1NTU6NkIwMDI1NzUiLCJ2aXNpdG9yX2lkIjoiNTA4MzgzNTIxMTA0NTY3NjQwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2a7f95e8d28a3aef57b7a5df7f4b5a79b83168bbbe6e08337453152cc4e1fd55 |
| hovercard-subject-tag | issue:325131952 |
| 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/5093/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3d87d72f4554d52e9a3598d433a990aa6ebe55811a6c55ddaacd25824a178efc/python/mypy/issues/5093 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3d87d72f4554d52e9a3598d433a990aa6ebe55811a6c55ddaacd25824a178efc/python/mypy/issues/5093 |
| og:image:alt | The docs for object.__get__ specify that: object.__get__(self, instance, owner) ... owner is always the owner class, while instance is the instance that the attribute was accessed through, or None ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ikonst |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width