Skip to content

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

encukou
Copy link
Member

@encukou encukou commented Jul 22, 2025

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 for object 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:

>>> class C: pass
>>> C.__dict__['__dict__']
<attribute '__dict__' of 'C' objects>
>>> C.__dict__['__dict__'].__objclass__
<class '__main__.C'>

After:

>>> class C: pass
>>> C.__dict__['__dict__']
<attribute '__dict__' of 'object' objects>
>>> C.__dict__['__dict__'].__objclass__
<class 'object'>

JelleZijlstra and others added 5 commits July 20, 2025 16:57
…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.
@encukou
Copy link
Member Author

encukou commented Jul 22, 2025

I can polish this, but I think it'd be better to target 3.15, and follow through a bit more.
The descriptor objects themselves could be shared across all classes that need them (within an interpreter), but it's not as straightforward to plug that into the type creation machinery.

@encukou
Copy link
Member Author

encukou commented Jul 30, 2025

Here's a polished version: the descriptors are made for type, which means thet skip type-checking (they work on any object). The getter/setter functions already check if their argument has a __dict__/__weakref__.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy