Title: Breaking change of `dataclasses.dataclass` comparison semantics in 3.13+ · Issue #128294 · python/cpython · GitHub
Open Graph Title: Breaking change of `dataclasses.dataclass` comparison semantics in 3.13+ · Issue #128294 · python/cpython
X Title: Breaking change of `dataclasses.dataclass` comparison semantics in 3.13+ · Issue #128294 · python/cpython
Description: Bug report Bug description: Brief Description Optimization done in #109870 changed semantic of dataclass comparison. Description The pseudo code below shows meaning of changes have been done in #109870. The semantic differs largely becau...
Open Graph Description: Bug report Bug description: Brief Description Optimization done in #109870 changed semantic of dataclass comparison. Description The pseudo code below shows meaning of changes have been done in #10...
X Description: Bug report Bug description: Brief Description Optimization done in #109870 changed semantic of dataclass comparison. Description The pseudo code below shows meaning of changes have been done in #10...
Opengraph URL: https://github.com/python/cpython/issues/128294
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Breaking change of `dataclasses.dataclass` comparison semantics in 3.13+","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\n#### Brief Description\r\n\r\nOptimization done in #109870 changed semantic of `dataclass` comparison.\r\n\r\n#### Description\r\n\r\nThe pseudo code below shows meaning of changes have been done in #109870. The semantic differs largely because of a shortcut in `__eq__` implementation of sequence-like containers (see [Objects/object.c][1]). The shortcut essentially does `self[i] is other[i]`. Consequently, method `__eq__` of `self[i]` is not evaluated for identical objects in 3.12 during `dataclasses.dataclass` comparison.\r\n\r\n[1]: https://github.com/python/cpython/blob/v3.13.1/Objects/listobject.c#L3376-L3378\r\n\r\n```python\r\ndef __eq__312(self, other):\r\n return astuple(self) == astuple(other)\r\n\r\ndef __eq__313(self, other):\r\n for lhs, rhs in zip(astuple(self), astuple(other)):\r\n if not lhs == rhs:\r\n return False\r\n else:\r\n return True\r\n``` \r\n\r\nAccording [Python docs](https://docs.python.org/3.13/library/dataclasses.html) (citation below), v3.13 introduces breaking change since it does not consider fields as a tuples for dataclass comparison.\r\n\r\n\u003e eq: If true (the default), an [`__eq__()`](https://docs.python.org/3.13/reference/datamodel.html#object.__eq__) method will be generated. This method compares the class as if it were a tuple of its fields, in order. Both instances in the comparison must be of the identical type.\r\n\r\n\r\n#### Test Case\r\n\r\n```python\r\nimport numpy as np\r\nfrom dataclasses import dataclass\r\n\r\n@dataclass\r\nclass A:\r\n xs: np.ndarray\r\n\r\na = A(np.ones(3))\r\nb = A(a.xs)\r\n\r\nprint(a == b) # FAIL (3.13); OK (3.12).\r\n# ValueError: Value The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()\r\n```\r\n\r\n\r\n### CPython versions tested on:\r\n\r\n3.13 (3.12)\r\n\r\n### Operating systems tested on:\r\n\r\nLinux","author":{"url":"https://github.com/daskol","@type":"Person","name":"daskol"},"datePublished":"2024-12-27T15:21:05.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":13},"url":"https://github.com/128294/cpython/issues/128294"}
| 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:7974a25d-990a-79b8-907c-ed2258296226 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E9DE:14FC13:C35078:10816FD:696AC16E |
| html-safe-nonce | 11af291b067df75eeca842dfd39e034d4379422acc1cbea51f27bf01fba6daf9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOURFOjE0RkMxMzpDMzUwNzg6MTA4MTZGRDo2OTZBQzE2RSIsInZpc2l0b3JfaWQiOiI1MDQwMjE1MjY1NDYyODk0OTU4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 97c6c3d11465e6ceff5a056596c6eabc3b8a2420410e16aefdcda526d228918b |
| hovercard-subject-tag | issue:2761001944 |
| 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/128294/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2e6e39f1efc5649ddff791a694710cb96c7cf6081eb418dcf45edd4a8581d7bc/python/cpython/issues/128294 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2e6e39f1efc5649ddff791a694710cb96c7cf6081eb418dcf45edd4a8581d7bc/python/cpython/issues/128294 |
| og:image:alt | Bug report Bug description: Brief Description Optimization done in #109870 changed semantic of dataclass comparison. Description The pseudo code below shows meaning of changes have been done in #10... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | daskol |
| hostname | github.com |
| expected-hostname | github.com |
| None | 986b6a1d774985095564e64d6963d11f094da3d0e2bfda2ab1a27d63662eb033 |
| 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 | 89ad2112b9c4e11df6a0c13c8c1f8eedd36b0977 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width