-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-141510, PEP 814: Add built-in frozendict type #144757
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
base: main
Are you sure you want to change the base?
Conversation
Add TYPE_FROZENDICT to the marshal module. Add C API functions: * PyAnyDict_Check() * PyAnyDict_CheckExact() * PyFrozenDict_Check() * PyFrozenDict_CheckExact() * PyFrozenDict_New() Add PyFrozenDict_Type C type.
|
cc @corona10 |
Fix also indentation.
| .. _whatsnew315-pep814: | ||
|
|
||
| :pep:`814`: Add frozendict built-in type | ||
| ---------------------------------------- | ||
|
|
||
| A new public immutable type :class:`frozendict` is added to the :mod:`builtins` | ||
| module. It is not a ``dict`` subclass but inherits directly from ``object``. | ||
|
|
||
| A ``frozendict`` can be hashed with ``hash(frozendict)`` if all keys and values | ||
| can be hashed. | ||
|
|
||
| .. seealso:: :pep:`814` for the full specification and rationale. |
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.
I think let's list this directly after lazy imports.
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.
Do you mean that I should move the PEP 814 section after the PEP 810 section? (I'm not sure if I understood your comment correctly.)
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.
Yes, that's right, PEP 810 first and then 814.
| self.assertTrue(d.get(list(self.other.keys())[0]) is None) | ||
| self.assertEqual(d.get(list(self.other.keys())[0], 3), 3) | ||
| d = self.reference | ||
| self.assertTrue(d.get(list(self.other.keys())[0]) is None) |
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.
| self.assertTrue(d.get(list(self.other.keys())[0]) is None) | |
| self.assertEqual(d.get(list(self.other.keys())[0], 3), 3) | |
| d = self.reference | |
| self.assertTrue(d.get(list(self.other.keys())[0]) is None) | |
| self.assertIsNone(d.get(list(self.other.keys())[0])) | |
| self.assertEqual(d.get(list(self.other.keys())[0], 3), 3) | |
| d = self.reference | |
| self.assertIsNone(d.get(list(self.other.keys())[0])) |
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
| } | ||
|
|
||
| if (PyDict_CheckExact(d)) { | ||
| if (PyAnyDict_CheckExact(d)) { |
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.
Maybe same opinion with @kumaraditya303 we can avoid a lot of critical section if dict is frozen dict.
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.
I agree, but I would prefer to make such "optimization" change in a separated PR.
| dict_length(PyObject *self) | ||
| { | ||
| return FT_ATOMIC_LOAD_SSIZE_RELAXED(((PyDictObject *)self)->ma_used); | ||
| return GET_USED(_PyAnyDict_CAST(self)); |
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.
Do we have to use atomic operation for frozendict?
| }; | ||
|
|
||
| static PyMappingMethods frozendict_as_mapping = { | ||
| .mp_length = dict_length, |
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.
I think that we can make frozendict_lenth
|
|
||
| static PyMappingMethods frozendict_as_mapping = { | ||
| .mp_length = dict_length, | ||
| .mp_subscript = dict_subscript, |
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.
Still perfer to make frozendict_subscript, but let's do it at the separate PR.
Add TYPE_FROZENDICT to the marshal module.
Add C API functions:
Add PyFrozenDict_Type C type.
📚 Documentation preview 📚: https://cpython-previews--144757.org.readthedocs.build/