Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-41779: Allow defining the __dict__ and __weakref__ __slots__ for any class#141755
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
Merged
serhiy-storchaka
merged 2 commits into
python:main
from
serhiy-storchaka:special-slots-with-items3Nov 19, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1329,18 +1329,17 @@ class D(object): | ||
| self.assertNotHasAttr(a, "__weakref__") | ||
| a.foo = 42 | ||
| self.assertEqual(a.__dict__, {"foo": 42}) | ||
| with self.assertRaises(TypeError): | ||
| weakref.ref(a) | ||
| class W(object): | ||
| __slots__ = ["__weakref__"] | ||
| a = W() | ||
| self.assertHasAttr(a, "__weakref__") | ||
| self.assertNotHasAttr(a, "__dict__") | ||
| try: | ||
| with self.assertRaises(AttributeError): | ||
| a.foo = 42 | ||
| except AttributeError: | ||
| pass | ||
| else: | ||
| self.fail("shouldn't be allowed to set a.foo") | ||
| self.assertIs(weakref.ref(a)(), a) | ||
| class C1(W, D): | ||
| __slots__ = [] | ||
| @@ -1349,6 +1348,7 @@ class C1(W, D): | ||
| self.assertHasAttr(a, "__weakref__") | ||
| a.foo = 42 | ||
| self.assertEqual(a.__dict__, {"foo": 42}) | ||
| self.assertIs(weakref.ref(a)(), a) | ||
| class C2(D, W): | ||
| __slots__ = [] | ||
| @@ -1357,6 +1357,77 @@ class C2(D, W): | ||
| self.assertHasAttr(a, "__weakref__") | ||
| a.foo = 42 | ||
| self.assertEqual(a.__dict__, {"foo": 42}) | ||
| self.assertIs(weakref.ref(a)(), a) | ||
| @unittest.skipIf(_testcapi is None, 'need the _testcapi module') | ||
| def test_slots_special_before_items(self): | ||
| class D(_testcapi.HeapCCollection): | ||
| __slots__ = ["__dict__"] | ||
| a = D(1, 2, 3) | ||
| self.assertHasAttr(a, "__dict__") | ||
| self.assertNotHasAttr(a, "__weakref__") | ||
| a.foo = 42 | ||
| self.assertEqual(a.__dict__, {"foo": 42}) | ||
| with self.assertRaises(TypeError): | ||
| weakref.ref(a) | ||
| del a.__dict__ | ||
| self.assertNotHasAttr(a, "foo") | ||
| self.assertEqual(a.__dict__, {}) | ||
| self.assertEqual(list(a), [1, 2, 3]) | ||
| class W(_testcapi.HeapCCollection): | ||
| __slots__ = ["__weakref__"] | ||
| a = W(1, 2, 3) | ||
| self.assertHasAttr(a, "__weakref__") | ||
| self.assertNotHasAttr(a, "__dict__") | ||
| with self.assertRaises(AttributeError): | ||
| a.foo = 42 | ||
| self.assertIs(weakref.ref(a)(), a) | ||
| with self.assertRaises(TypeError): | ||
| class X(_testcapi.HeapCCollection): | ||
| __slots__ = ['x'] | ||
| with self.assertRaises(TypeError): | ||
| class X(_testcapi.HeapCCollection): | ||
| __slots__ = ['__dict__', 'x'] | ||
| @support.subTests(('base', 'arg'), [ | ||
| (tuple, (1, 2, 3)), | ||
| (int, 9876543210**2), | ||
| (bytes, b'ab'), | ||
| ]) | ||
| def test_slots_special_after_items(self, base, arg): | ||
| class D(base): | ||
| __slots__ = ["__dict__"] | ||
| a = D(arg) | ||
| self.assertHasAttr(a, "__dict__") | ||
| self.assertNotHasAttr(a, "__weakref__") | ||
| a.foo = 42 | ||
| self.assertEqual(a.__dict__, {"foo": 42}) | ||
| with self.assertRaises(TypeError): | ||
| weakref.ref(a) | ||
| del a.__dict__ | ||
| self.assertNotHasAttr(a, "foo") | ||
| self.assertEqual(a.__dict__, {}) | ||
| self.assertEqual(a, base(arg)) | ||
| class W(base): | ||
| __slots__ = ["__weakref__"] | ||
| a = W(arg) | ||
| self.assertHasAttr(a, "__weakref__") | ||
| self.assertNotHasAttr(a, "__dict__") | ||
| with self.assertRaises(AttributeError): | ||
| a.foo = 42 | ||
| self.assertIs(weakref.ref(a)(), a) | ||
| self.assertEqual(a, base(arg)) | ||
serhiy-storchaka marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| with self.assertRaises(TypeError): | ||
| class X(base): | ||
| __slots__ = ['x'] | ||
| with self.assertRaises(TypeError): | ||
| class X(base): | ||
| __slots__ = ['__dict__', 'x'] | ||
| def test_slots_special2(self): | ||
| # Testing __qualname__ and __classcell__ in __slots__ | ||
2 changes: 2 additions & 0 deletions
2 Misc/NEWS.d/next/Core_and_Builtins/2025-11-16-21-14-48.gh-issue-41779.rXIj5h.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Allowed defining the *__dict__* and *__weakref__* :ref:`__slots__ <slots>` | ||
| for any class. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.