Title: Add typing.override decorator · Issue #101561 · python/cpython · GitHub
Open Graph Title: Add typing.override decorator · Issue #101561 · python/cpython
X Title: Add typing.override decorator · Issue #101561 · python/cpython
Description: Feature or enhancement See PEP 698 for details. The typing.override decorator should, at runtime, attempt to set the __override__ attribute on its argument to True and then return the argument. If it cannot set the __override__ flag it s...
Open Graph Description: Feature or enhancement See PEP 698 for details. The typing.override decorator should, at runtime, attempt to set the __override__ attribute on its argument to True and then return the argument. If ...
X Description: Feature or enhancement See PEP 698 for details. The typing.override decorator should, at runtime, attempt to set the __override__ attribute on its argument to True and then return the argument. If ...
Opengraph URL: https://github.com/python/cpython/issues/101561
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add typing.override decorator","articleBody":"# Feature or enhancement\r\n\r\nSee [PEP 698](https://peps.python.org/pep-0698/) for details.\r\n\r\nThe `typing.override` decorator should, at runtime, attempt to set the `__override__` attribute on its argument to `True` and then return the argument. If it cannot set the `__override__` flag it should return its argument unchanged.\r\n\r\n# Pitch\r\n\r\nThe purpose of `typing.override` is to inform static type checkers that we expect this method to override some attribute of an ancestor class. By having this decorator in place, a developer can ensure that static type checkers will warn them if a base class method name changes.\r\n\r\nTo quote the PEP consider a code change where we rename a parent class method.\r\n\r\nThe original code looks like this:\r\n```python\r\nclass Parent:\r\n def foo(self, x: int) -\u003e int:\r\n return x\r\n\r\nclass Child(Parent):\r\n def foo(self, x: int) -\u003e int:\r\n return x + 1\r\n\r\ndef parent_callsite(parent: Parent) -\u003e None:\r\n parent.foo(1)\r\n\r\ndef child_callsite(child: Child) -\u003e None:\r\n child.foo(1)\r\n```\r\nAnd after our rename it looks like this:\r\n```python\r\nclass Parent:\r\n # Rename this method\r\n def new_foo(self, x: int) -\u003e int:\r\n return x\r\n\r\nclass Child(Parent):\r\n # This (unchanged) method used to override `foo` but is unrelated to `new_foo`\r\n def foo(self, x: int) -\u003e int:\r\n return x + 1\r\n\r\ndef parent_callsite(parent: Parent) -\u003e None:\r\n # If we pass a Child instance we’ll now run Parent.new_foo - likely a bug\r\n parent.new_foo(1)\r\n\r\ndef child_callsite(child: Child) -\u003e None:\r\n # We probably wanted to invoke new_foo here. Instead, we forked the method\r\n child.foo(1)\r\n```\r\nIn the code snippet above, renaming `foo` to `new_foo` in `Parent` invalidated the override `foo` of `Child`. But type checkers have no way of knowing this, because they only see a snapshot of the code.\r\n\r\nIf we mark `Child.foo` as an override, then static type checkers will catch the mistake when we rename only `Parent.foo`:\r\n```py\r\nfrom typing import override\r\n\r\nclass Parent:\r\n def new_foo(self) -\u003e int:\r\n return 1\r\n\r\n def bar(self, x: str) -\u003e str:\r\n return x\r\n\r\nclass Child(Parent):\r\n @override\r\n def foo(self) -\u003e int: # Type Error: foo does not override an attribute of any ancestor\r\n return 2\r\n```\r\n\r\n# Previous discussion\r\n\r\n[PEP 698](https://peps.python.org/pep-0698/) has details about the proposal itself.\r\n\r\nDiscussion on this proposal has happened on [typing-sig](https://mail.python.org/archives/list/typing-sig@python.org/thread/V23I4D6DEOFW4BBPWBMYTHZUOMKR7KQE/#MZMBDDKOOH2KUE2IRRMQLOEN6MJ2WHCN) and on [discuss.python.org)[https://discuss.python.org/t/pep-698-a-typing-override-decorator/20839].\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-101564\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/stroxler","@type":"Person","name":"stroxler"},"datePublished":"2023-02-04T16:09:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/101561/cpython/issues/101561"}
| 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:4dee7f2c-e080-8238-d17c-a63e0d498881 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DE46:1610AC:FE3EE1:1550AAB:696991E2 |
| html-safe-nonce | 0d71eff4fefc9ab76ebbd59ae26f1161c3100312a4e7f9aa4a57b0566374c85a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERTQ2OjE2MTBBQzpGRTNFRTE6MTU1MEFBQjo2OTY5OTFFMiIsInZpc2l0b3JfaWQiOiIyMDUzNjYzOTcwNTc0NzA5NDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c788d023e799a8d4851b7bde107e5a99ddf75969c2ba0d2acb28fdb8bd2085d0 |
| hovercard-subject-tag | issue:1571009590 |
| 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/cpython/101561/issue_layout |
| twitter:image | https://opengraph.githubassets.com/09577d089d735bce2ba7c2e7157190c418452a1106c9077990f7e65f4e863fee/python/cpython/issues/101561 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/09577d089d735bce2ba7c2e7157190c418452a1106c9077990f7e65f4e863fee/python/cpython/issues/101561 |
| og:image:alt | Feature or enhancement See PEP 698 for details. The typing.override decorator should, at runtime, attempt to set the __override__ attribute on its argument to True and then return the argument. If ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stroxler |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3542e147982176a7ebaa23dfb559c8af16f721c03ec560c68c56b64a0f35e751 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | af80af7cc9e3de9c336f18b208a600950a3c187c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width