Title: How should type checkers type check equality? · Issue #378 · python/typing · GitHub
Open Graph Title: How should type checkers type check equality? · Issue #378 · python/typing
X Title: How should type checkers type check equality? · Issue #378 · python/typing
Description: Right now typeshed defines equality to be defined for each pair of types, even though something like n == (m,) is almost certainly a bug if both n and m are integers, for example. There is no runtime error, so not rejecting it would also...
Open Graph Description: Right now typeshed defines equality to be defined for each pair of types, even though something like n == (m,) is almost certainly a bug if both n and m are integers, for example. There is no runti...
X Description: Right now typeshed defines equality to be defined for each pair of types, even though something like n == (m,) is almost certainly a bug if both n and m are integers, for example. There is no runti...
Opengraph URL: https://github.com/python/typing/issues/378
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"How should type checkers type check equality?","articleBody":"Right now typeshed defines equality to be defined for each pair of types, even though something like `n == (m,)` is almost certainly a bug if both `n` and `m` are integers, for example. There is no runtime error, so not rejecting it would also be somewhat reasonable (also, a subclass of `int` might plausibly return `True` when compared against a tuple, though this more of a theoretical concern). It's unclear how type checkers should deal with these. As equality checking is very common, and anecdotally errors related to equality checking are not infrequent, we may want to spell out how this should be done in PEP 484, so that all tools would handle these consistently.\r\n\r\nHere are some options:\r\n\r\n* Leave it up to individual type checkers to figure this out, and make the signatures of `__eq__` and `__ne__` accept arbitrary objects.\r\n* Make the signatures of `__eq__` and `__ne__` accept arbitrary objects, but require type checkers to have special, well-defined rules for type checking `==` and `!=` operations so that we can reject `n == (m,)` and other similar cases consistently.\r\n* Encode the information about what a particular type can be equal to in the signature of `__eq__` and `__ne__`. This may require special rules for these methods, since we may want to narrow down the argument type in a subclass (as it should be possible to compare `object` to `object`), but normally method argument types vary contravariantly so this is not allowed. For example, `int.__eq__` would only accept `int` arguments, since this seems to return `NotImplemented` for other argument types. It's still unclear what the exact rules for type checking `__eq__` and `__ne__` should be.\r\n* Add a new type system concept to PEP 484 and `typing` to represent \"comparable types\" or \"equatable types\". For example, I might want to define function `isequal(x, y)` that takes two arguments that can be of arbitrary types as long as they might compare equal to each other. `isequal(1, 1.1)` should be okay but `isequal(1, 'x')` should be rejected. We'd then use this concept to type check `==` and `!=` as well. This could be useful for things like `__contains__`. If the result of an `in` operation is known to be `False` statically, we should perhaps reject it as well.\r\n\r\nAlso see https://github.com/python/mypy/issues/1271 and the conversation in https://github.com/python/typeshed/pull/874.","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2017-01-30T12:29:53Z","url":"https://github.com/python/typing/issues/378"}
| 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:314ac79e-3dfe-e827-e4cd-64c9f959069f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8E4:2003E:68CC9:8FE73:6B0018A5 |
| html-safe-nonce | 00c622625eed52a72b60f9fb5db8d14bd0f16ab73b4577f790ef03e046f9b620 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEU0OjIwMDNFOjY4Q0M5OjhGRTczOjZCMDAxOEE1IiwidmlzaXRvcl9pZCI6Ijc5MTUzNzQwMDUzMTIyOTcxMjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 88e99b6cbaf64e8742c5e688ade98e375a7d85146e536501b2b9a69a60d50e11 |
| hovercard-subject-tag | issue:203997755 |
| 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/typing/378/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7fe7302dd8a8d4c0e90711d4a55781b1a8c2e19add1657c180e243be170f1548/python/typing/issues/378 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7fe7302dd8a8d4c0e90711d4a55781b1a8c2e19add1657c180e243be170f1548/python/typing/issues/378 |
| og:image:alt | Right now typeshed defines equality to be defined for each pair of types, even though something like n == (m,) is almost certainly a bug if both n and m are integers, for example. There is no runti... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JukkaL |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width