Title: Unexpected behavior with dataclasses and weakref · Issue #102069 · python/cpython · GitHub
Open Graph Title: Unexpected behavior with dataclasses and weakref · Issue #102069 · python/cpython
X Title: Unexpected behavior with dataclasses and weakref · Issue #102069 · python/cpython
Description: We recently discovered the following error in our project: TypeError: descriptor '__weakref__' for 'XYZ' objects doesn't apply to a 'XYZ' object XYZ in our case is a dataclass which uses slots (added v3.10) and weakref_slot (added v3.11)...
Open Graph Description: We recently discovered the following error in our project: TypeError: descriptor '__weakref__' for 'XYZ' objects doesn't apply to a 'XYZ' object XYZ in our case is a dataclass which uses slots (add...
X Description: We recently discovered the following error in our project: TypeError: descriptor '__weakref__' for 'XYZ' objects doesn't apply to a 'XYZ' object XYZ in our case is a dat...
Opengraph URL: https://github.com/python/cpython/issues/102069
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unexpected behavior with dataclasses and weakref","articleBody":"We recently discovered the following error in our project:\r\n`TypeError: descriptor '__weakref__' for 'XYZ' objects doesn't apply to a 'XYZ' object`\r\n\r\nXYZ in our case is a dataclass which uses slots (added v3.10) and weakref_slot (added v3.11) parameters.\r\n\r\nWe further investigated the error and have come to the following example to reproduce it:\r\n\r\n```py\r\nimport dataclasses\r\nimport weakref\r\n\r\n\r\n@dataclasses.dataclass(slots=True, weakref_slot=True)\r\nclass EntityAsDataclass:\r\n x: int = 10\r\n\r\n\r\nclass Entity:\r\n __slots__ = (\"x\", \"__weakref__\")\r\n\r\n def __init__(self, x: int = 10) -\u003e None:\r\n self.x = x\r\n\r\n\r\ne1 = Entity()\r\ne1_ref = weakref.ref(e1)\r\n\r\ne2 = EntityAsDataclass()\r\ne2_ref = weakref.ref(e2)\r\n\r\n\r\nassert e1.__weakref__ is e1_ref # works\r\nassert e2.__weakref__ is e2_ref # fails with \"TypeError: descriptor '__weakref__' for 'EntityAsDataclass' objects doesn't apply to a 'EntityAsDataclass' object\"\r\n```\r\n\r\nI don't know if this is intended, but we expect EntityAsDataclass and Entity to have the same behavior here.\r\n\r\nThanks!\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-102075\n* gh-102662\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/jbaudisch","@type":"Person","name":"jbaudisch"},"datePublished":"2023-02-20T12:23:54.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/102069/cpython/issues/102069"}
| 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:88316e32-0c57-47dc-bc1a-602653ef59e0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB38:256EA0:11C03C7:17B64FF:6969AA18 |
| html-safe-nonce | 06d9880df4d0f87b762989ad5d20cab86ae0f10250e2c350a5516016254e9d1d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjM4OjI1NkVBMDoxMUMwM0M3OjE3QjY0RkY6Njk2OUFBMTgiLCJ2aXNpdG9yX2lkIjoiODE3NTgzMjYxMTgwMDMyODcyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 97466caac3839368279df928f533535379133923b9e1b41bcaefd399f7c6c095 |
| hovercard-subject-tag | issue:1591763926 |
| 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/102069/issue_layout |
| twitter:image | https://opengraph.githubassets.com/095fb61253f4a5907526802d4636401526a473d5053bc6a6c302fca94a56864d/python/cpython/issues/102069 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/095fb61253f4a5907526802d4636401526a473d5053bc6a6c302fca94a56864d/python/cpython/issues/102069 |
| og:image:alt | We recently discovered the following error in our project: TypeError: descriptor '__weakref__' for 'XYZ' objects doesn't apply to a 'XYZ' object XYZ in our case is a dataclass which uses slots (add... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jbaudisch |
| hostname | github.com |
| expected-hostname | github.com |
| None | 24c4c97a2d520cb286b35e1a4c22d7a4df3c26a2fa28dd7cdf0e65db327b4de7 |
| 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 | 124667f43168afb6c9c03b7c02eb5b1d2e1be3d9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width