Skip to content

gh-135228: When @dataclass(slots=True) replaces a dataclass, make the original class collectible (take 2) #137047

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Jul 23, 2025

This is a redo of #136893 without relying on a hack to get to the type dictionary.

JelleZijlstra and others added 3 commits July 23, 2025 08:13
…ke the original class collectible (python#136893)

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.

Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check that this is a Python class? Should we clear the whole class dict or set it to NULL for the case if there are more descriptors?

@JelleZijlstra
Copy link
Member Author

Can we check that this is a Python class?

Maybe we can check that it's a heap type?

Should we clear the whole class dict or set it to NULL for the case if there are more descriptors?

I'm not aware of other descriptors that could cause problems. It's better to minimize the amount of changes we make to the class, because there are edge cases where the class is still accessible after the dataclass transformation is applied.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@JelleZijlstra
Copy link
Member Author

@encukou @serhiy-storchaka could you review again and approve if you're satisfied, so that we can get this fixed in 3.14?

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find any issues in the PR; this is pretty much is the local optimum.
But, I can't tell if there will be unintended consequences. This PR feels quite risky for a RC fix.

Docs-wise: the notes/comments shouldn't imply that the original class is guaranteed to be garbage collected -- something else could still keep it alive.

@encukou
Copy link
Member

encukou commented Aug 11, 2025

@hugovk Your thoughts on this?

IMO: If we weren't in RC period, I'd push for #136966, which is IMO a (slight) improvement on its own. But, it has a higher risk of unknown unknowns.
For backporting to 3.14.0, this dataclasses-specific workaround is probably better. But, all of it is a band-aid.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @encukou. #136966 may be better in long perspective (or it may be a move in right direction), but this change has more limited impact, and therefore lokks safer. We always can backport #136966 if we test it well.

/*[clinic input]
sys._clear_type_descriptors

type: object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Argument Clinic for type checking. Although it does not make much difference.

Suggested change
type: object
type: object(subclass_of="&PyType_Type")

PyErr_SetString(PyExc_TypeError, "argument must be a type");
return NULL;
}
PyTypeObject *typeobj = (PyTypeObject *)(type);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PyTypeObject *typeobj = (PyTypeObject *)(type);
PyTypeObject *typeobj = (PyTypeObject *)type;

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

Successfully merging this pull request may close these issues.

4 participants