Title: mypyc generated `__ne__` uses `self` type instead of `__eq__`'s RHS type with strict dunder typing · Issue #21568 · python/mypy · GitHub
Open Graph Title: mypyc generated `__ne__` uses `self` type instead of `__eq__`'s RHS type with strict dunder typing · Issue #21568 · python/mypy
X Title: mypyc generated `__ne__` uses `self` type instead of `__eq__`'s RHS type with strict dunder typing · Issue #21568 · python/mypy
Description: Bug Report While testing fixes for #21566 , I tried to set strict_dunder_typing=True which allowed me to somewhat narrow the issue down, and I've sinced removed this bool. However it did expose a seemingly other issue. When mypyc implici...
Open Graph Description: Bug Report While testing fixes for #21566 , I tried to set strict_dunder_typing=True which allowed me to somewhat narrow the issue down, and I've sinced removed this bool. However it did expose a s...
X Description: Bug Report While testing fixes for #21566 , I tried to set strict_dunder_typing=True which allowed me to somewhat narrow the issue down, and I've sinced removed this bool. However it did expose...
Opengraph URL: https://github.com/python/mypy/issues/21568
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypyc generated `__ne__` uses `self` type instead of `__eq__`'s RHS type with strict dunder typing","articleBody":"**Bug Report**\n\nWhile testing fixes for #21566 , I tried to set `strict_dunder_typing=True` which allowed me to somewhat narrow the issue down, and I've sinced removed this bool. However it did expose a seemingly other issue.\n\nWhen mypyc implicitly generates `__ne__` from a user-defined `__eq__` with `strict_dunder_typing=True`, the generated `__ne__` appears to use the type of `self` as the type of its right-hand-side argument.\n\nI may be misunderstanding the intended behavior of `strict_dunder_typing`, but my expectation is that the generated `__ne__` should mirror the argument type of `__eq__`'s other parameter. In other words, if `__eq__` accepts a particular set of object types, then the generated `__ne__` should also accept that same set of object types.\n\n**To Reproduce**\n\nAdd this test case to `mypyc/test-data/run-dunders.test`:\n\n```\n[case testDundersImplicitNeWithNarrowRhs]\nclass Accepted:\n pass\n\nclass Eq:\n def __eq__(self, other: Accepted) -> bool: # type: ignore[override]\n return True\n\ndef test_implicit_ne_with_narrow_rhs() -> None:\n assert Eq() == Accepted()\n assert not (Eq() != Accepted())\n```\n\nRun the strict dunder tests:\n\n```\npython -m pytest -q mypyc/test/test_run.py -k testDundersImplicitNeWithNarrowRhs\n```\n\n**Expected Behavior**\n\nBoth comparisons should work.\n\nSince `__eq__` is defined as:\n\n```python\ndef __eq__(self, other: Accepted) -> bool:\n return True\n```\n\nthe generated `__ne__` should accept the same other type and behave as though it were approximately:\n\n```python\ndef __ne__(self, other: Accepted) -> bool:\n return not self.__eq__(other)\n```\n\nSo this should pass:\n\n```python\nassert Eq() == Accepted()\nassert not (Eq() != Accepted())\n```\n\n**Actual Behavior**\n\nThe `==` comparison succeeds, but the `!=` comparison fails at runtime because the implicitly generated `__ne__` expects the right-hand-side argument to be `Eq`.\n\nIn other words, the generated `__ne__` appears to behave as though its RHS argument type came from `self`, not from `__eq__`'s other parameter.\n","author":{"url":"https://github.com/rheard","@type":"Person","name":"rheard"},"datePublished":"2026-05-30T04:46:52Z","url":"https://github.com/python/mypy/issues/21568"}
| 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:dab68678-ca07-7c29-e670-d87fcfa116bf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C6A:2A4B31:386334:4C5854:6B002A20 |
| html-safe-nonce | c964294db5e9c72919d16aa5d78f68d3ac5b76e9b784bd5c78cc2e0d6c9e96d3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzZBOjJBNEIzMTozODYzMzQ6NEM1ODU0OjZCMDAyQTIwIiwidmlzaXRvcl9pZCI6IjgwNzQwNjM2NjY2OTA5MjcxMzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e12b9ec46e361bf6f431a00ce6cf0f1382ab5b5224831cc1da68859d96e29cbd |
| hovercard-subject-tag | issue:4553241201 |
| 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/21568/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a7bf3669327c71ed4afe8eeab5747e5330d719077254b1c2e2a0651079e4f96d/python/mypy/issues/21568 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a7bf3669327c71ed4afe8eeab5747e5330d719077254b1c2e2a0651079e4f96d/python/mypy/issues/21568 |
| og:image:alt | Bug Report While testing fixes for #21566 , I tried to set strict_dunder_typing=True which allowed me to somewhat narrow the issue down, and I've sinced removed this bool. However it did expose a s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rheard |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width