Title: Self type with multiple inheritance · Issue #18458 · python/mypy · GitHub
Open Graph Title: Self type with multiple inheritance · Issue #18458 · python/mypy
X Title: Self type with multiple inheritance · Issue #18458 · python/mypy
Description: Not sure whether this is actually a bug or not, but I'm getting a base class incompatibility error when inheriting from two classes defining a method with the same signature containing the typing.Self type. To Reproduce from typing impor...
Open Graph Description: Not sure whether this is actually a bug or not, but I'm getting a base class incompatibility error when inheriting from two classes defining a method with the same signature containing the typing.S...
X Description: Not sure whether this is actually a bug or not, but I'm getting a base class incompatibility error when inheriting from two classes defining a method with the same signature containing the typi...
Opengraph URL: https://github.com/python/mypy/issues/18458
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Self type with multiple inheritance","articleBody":"Not sure whether this is actually a bug or not, but I'm getting a base class incompatibility error when inheriting from two classes defining a method with the same signature containing the `typing.Self` type.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom typing import Self\r\n\r\nclass A:\r\n def a_method(self) -> None:\r\n pass\r\n def method(self: Self, other: Self) -> None:\r\n self.a_method()\r\n other.a_method()\r\n\r\nclass B:\r\n def b_method(self) -> None:\r\n pass\r\n def method(self: Self, other: Self) -> None:\r\n self.b_method()\r\n other.b_method()\r\n\r\nclass C(A, B):\r\n pass\r\n```\r\n\r\nRunning `mypy` gives this error:\r\n\r\n```\r\ntest.py:17: error: Definition of \"method\" in base class \"A\" is incompatible with definition in base class \"B\" [misc]\r\n```\r\n\r\nI expected `Self` to mean \"whatever this type happens to be\" (base class or subclass) but the error would suggest that it's interpreting `Self` as the specific class containing that method definition.\r\n\r\nNext I tried the following variation:\r\n\r\n```python\r\nfrom typing import TypeVar\r\n\r\nT = TypeVar('T')\r\n\r\nclass A:\r\n def a_method(self) -> None:\r\n pass\r\n def method(self: T, other: T) -> None:\r\n self.a_method()\r\n\r\nclass B:\r\n def b_method(self) -> None:\r\n pass\r\n def method(self: T, other: T) -> None:\r\n self.b_method()\r\n\r\nclass C(A, B):\r\n pass\r\n```\r\n\r\nThis fixes the incompatibility error but results in new errors:\r\n\r\n```\r\ntest.py:9: error: \"T\" has no attribute \"a_method\" [attr-defined]\r\ntest.py:15: error: \"T\" has no attribute \"b_method\" [attr-defined]\r\n```\r\n\r\nI also tried using two separate `TypeVar`s, one with `bound='A'` and the other with `bound='B'`, but this brought back the \"incompatible definition\" error from before.\r\n\r\nFinally (after reverting the types back to `Self`), I updated `C`'s definition to the following:\r\n\r\n```python\r\nclass C(A, B):\r\n def method(self: Self, other: Self) -> None:\r\n A.method(self, other)\r\n```\r\n\r\nThis ended up type-checking, but I'm wondering if `mypy` ought to be clever enough to figure it out without having to explicitly override `method`, or if there's some other alternative?\r\n\r\nAnd if not, perhaps the error message about the incompatible definition could elaborate a bit more in the case where the type signatures \"appear\" to match due to the use of `Self`?\r\n\r\n**Environment**\r\n\r\n- Python 3.11.11\r\n- `mypy` 1.14.1 (no flags or config file)\r\n","author":{"url":"https://github.com/jeremander","@type":"Person","name":"jeremander"},"datePublished":"2025-01-14T00:49:55Z","url":"https://github.com/python/mypy/issues/18458"}
| 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:64c5bb41-0436-985b-0bf2-5481b564c0d1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D680:31B434:146CC8F:1AABAA2:6B00095C |
| html-safe-nonce | d611406f1053e34de97e0f3715cf0841e55a6b14e2b966ebed941ebec621980b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENjgwOjMxQjQzNDoxNDZDQzhGOjFBQUJBQTI6NkIwMDA5NUMiLCJ2aXNpdG9yX2lkIjoiMTM3NTMzMDYxOTY4NDI5Mjk1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0263127485050b99ec7295d2514d07e9b61f4ead3aee289370fbeae94ee23047 |
| hovercard-subject-tag | issue:2785849799 |
| 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/18458/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2cc1b447ff6c658800087b3f9e467e6dded4a0a60cc37806c407b2efb7a4f510/python/mypy/issues/18458 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2cc1b447ff6c658800087b3f9e467e6dded4a0a60cc37806c407b2efb7a4f510/python/mypy/issues/18458 |
| og:image:alt | Not sure whether this is actually a bug or not, but I'm getting a base class incompatibility error when inheriting from two classes defining a method with the same signature containing the typing.S... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jeremander |
| 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