-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
gh-135228: Create __dict__ and __weakref__ descriptors for object #136966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ke the original class collectible An interesting hack, but more localized in scope than python#135230. This may be a breaking change if people intentionally keep the original class around when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the original class.
I can polish this, but I think it'd be better to target 3.15, and follow through a bit more. |
Here's a polished version: the descriptors are made for And since they're the same for all objects, they can be shared across all types in an interpreter -- that is, cached in interpreter state. |
This now partially reverts #137047. I don't think keeping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is a nice improvement! I think there's risk this will break some advanced introspection code, similar to the change you had to make to inspect, but if it goes in early in the 3.15 cycle, that should be OK.
I suggest to wait some time, maybe until 3.14.1, so we can have chance to get some feedback about an alternate approach and decide whether it is worth to backport this approach to 3.14. We may also find completely different solution of the original problem in 3.15. |
Misc/NEWS.d/next/Core_and_Builtins/2025-08-05-10-22-15.gh-issue-136966.J5lrE0.rst
Show resolved
Hide resolved
Co-authored-by: Jelle Zijlstra <[email protected]>
Definitely! Personally, I don't think this should be backported at all. (But I wouldn't have backported #137047 to a RC either if that was up to me...) |
Here's another possible way to solve #135228:
The
__dict__
and__weakref__
descriptors don't really need to hold a strong reference to "their" type; they are generic and could be created forobject
instead.Here's a proof of concept. A cleaner way to do this would be creating the descriptor objects once and caching them in the interpreter state.
This does involve a behaviour change -- before:
After: