Title: Can't overload __add__ for subclass · Issue #1987 · python/mypy · GitHub
Open Graph Title: Can't overload __add__ for subclass · Issue #1987 · python/mypy
X Title: Can't overload __add__ for subclass · Issue #1987 · python/mypy
Description: Consider the following working code: from typing import overload class S: @overload def __add__(self, arg: 'A') -> 'S': ... @overload def __add__(self, arg: 'S') -> 'S': ... class A(S): @overload def __add__(self, arg: 'A') -> 'A': ... @...
Open Graph Description: Consider the following working code: from typing import overload class S: @overload def __add__(self, arg: 'A') -> 'S': ... @overload def __add__(self, arg: 'S') -> 'S': ... class A(S): @overload d...
X Description: Consider the following working code: from typing import overload class S: @overload def __add__(self, arg: 'A') -> 'S': ... @overload def __add__(self, arg: 'S') -> ...
Opengraph URL: https://github.com/python/mypy/issues/1987
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Can't overload __add__ for subclass","articleBody":"Consider the following working code:\n\n``` python\nfrom typing import overload\n\nclass S:\n @overload\n def __add__(self, arg: 'A') -> 'S': ...\n @overload\n def __add__(self, arg: 'S') -> 'S': ...\n\nclass A(S):\n @overload\n def __add__(self, arg: 'A') -> 'A': ...\n @overload\n def __add__(self, arg: 'S') -> 'S': ...\n```\n\nThink of 'S' as 'str' and 'A' as 'ascii', a subclass used only for literals containing only ASCII characters. The idea is that adding ASCII to ASCII produces ASCII, but all other combinations produce just strings.\n\nThe subclass 'A' redefines the method `__add__` to express this idea. This version of the code works. However, the base class contains redundant overloads: 'A' is a subclass of 'S' so the base class is equivalent to the following, which doesn't use overloads:\n\n``` python\nclass S:\n def __add__(self, arg: 'S') -> 'S': ...\n```\n\nAnd here's the rub: with this definition of 'S', the definition of `__add__` in 'A' (still using overloads) is rejected:\n\n```\n__tmp__.py:10: error: Signature of \"__add__\" incompatible with supertype \"S\"\n```\n\n(The error points to the first `@overload`, but it seems to apply to the entire set of overloads.)\n\nNow here's the _real_ rub: if I change `__add__` consistently to `method`, the error goes away!\n\n[**UPDATE** I can't repro this for `__radd__` -- it seems unique to `__add__`.]\n","author":{"url":"https://github.com/gvanrossum","@type":"Person","name":"gvanrossum"},"datePublished":"2016-08-05T15:31:28Z","url":"https://github.com/python/mypy/issues/1987"}
| 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:5d3bb216-3507-8515-ae49-8e7608c2075e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C262:3C2A9A:11355EA:167B1BA:6AFFFB67 |
| html-safe-nonce | 5ebd01b39ba0ee02e3ad0333091e5b65105eaad013e93dae2ae266af80730ce2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMjYyOjNDMkE5QToxMTM1NUVBOjE2N0IxQkE6NkFGRkZCNjciLCJ2aXNpdG9yX2lkIjoiMzU3NDIwNzk1MzcyMjQwNzc4MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 58e72c716d3cbdce62eb55bab00401e3ffd9e96eca0aa60942d3ff04b5dc5408 |
| hovercard-subject-tag | issue:169633996 |
| 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/1987/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b4038787f39b84d1f99057befb0c8c78c46316c520237de2c228c0b377e8a1a2/python/mypy/issues/1987 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b4038787f39b84d1f99057befb0c8c78c46316c520237de2c228c0b377e8a1a2/python/mypy/issues/1987 |
| og:image:alt | Consider the following working code: from typing import overload class S: @overload def __add__(self, arg: 'A') -> 'S': ... @overload def __add__(self, arg: 'S') -> 'S': ... class A(S): @overload d... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gvanrossum |
| 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