Skip to content
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

Make __bases__ mutable #5325

Merged
merged 4 commits into from
May 20, 2024
Merged

Make __bases__ mutable #5325

merged 4 commits into from
May 20, 2024

Conversation

dchiquito
Copy link
Contributor

Assigning __bases__ allows you to alter the class inheritance tree at any point. The updated typing.py uses this feature when constructing NamedTuples, for reasons unknown to me.

I had to make PyType.bases and PyType.mro mutable behind a PyRwLock in order to enable this feature.

Assigning __bases__ has several features I wasn't sure how to implement. I left TODOs for these:

  • Making sure the new method resolution order is actually a tree, i.e. it contains no cycles
  • Removing the modified class from any __subclass__ lists it used to belong to
  • Removing any slots that no longer apply (not sure if this is required?)

@dchiquito
Copy link
Contributor Author

The test failures don't seem related to my changes. @youknowone any insight?

@youknowone
Copy link
Member

The failing test is about threading, it might be flaky. re-triggered failed jobs.

vm/src/builtins/type.rs Outdated Show resolved Hide resolved
vm/src/builtins/type.rs Show resolved Hide resolved
@youknowone
Copy link
Member

youknowone commented May 17, 2024

Making sure the new method resolution order is actually a tree, i.e. it contains no cycles

Leaving a comment will be enough for now.

Removing the modified class from any subclass lists it used to belong to

How is this done in CPython? Does it maintain weakrefs to subclasses?

Removing any slots that no longer apply (not sure if this is required?)

We don't have this feature yet. To do this, we have to implement slot copy first, which will remove most of iter_mro calls in our codebase.

vm/src/builtins/type.rs Outdated Show resolved Hide resolved
@dchiquito
Copy link
Contributor Author

Removing the modified class from any subclass lists it used to belong to

How is this done in CPython? Does it maintain weakrefs to subclasses?

Yes, the subclass list is a PyRwLock<Vec<PyRef<PyWeak>>>, and CPython is similar-ish. The complicated part is that CPython maintains a dict of subclasses so that they are uniquely identifiable, presumably just so they can be removed or modified. The key function looks complicated to get right, so I left the TODO.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

👍

@youknowone youknowone merged commit b59d876 into RustPython:main May 20, 2024
11 checks passed
@dchiquito dchiquito deleted the set-bases branch May 20, 2024 13:41
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.

None yet

2 participants