Repository navigation
feat: add dict::get - #6265
Merged
Merged
feat: add dict::get#6265
Conversation
Mirror Python's dict.get(key, default=None) with a single lookup. Assisted-by: ClaudeCode:claude-fable-5-2
rwgk
approved these changes
Nov 21, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🤖 AI text below 🤖
Description
Adds
py::dict::get(key, default), equivalent to Python'sdict.get. The default is optional and isNoneif omitted. Keys and defaults go throughobject_or_cast, the same asdict::contains.The lookup is done once, with no
KeyError(this is the benefit to adding this as new API, rather than the current workaround). A newdetail::dict_getitemrefhelper usesPyDict_GetItemRefon Python 3.13+ (a strong reference, safe on free-threaded builds) andPyDict_GetItemWithErroron older versions. Errors from__hash__or__eq__propagate.Supersedes #1513 and the
dict::getpart of, and close #2779.Why close #2779?
🤖 AI text below 🤖
The only other part of #2779 is the
dict_accessorspecialization ofdict::operator[], and I recommend that you do not take it.py::dictaccepts anyPyDict_Checkobject. Todayd[key]usesPyObject_GetItem/PyObject_SetItem, which call__missing__and overridden__getitem__/__setitem__. ThePyDict_GetItemWithError/PyDict_SetItemcalls in Add shortcutdict::getand optimize item access fordict#2779 skip these, sod["x"]on adefaultdictwould raiseKeyError.PyObject_GetItemgoes through one slot call todict_subscript, which does the same hash lookup. rwgk raised the same objection in the thread.PyDict_GetItemRef, the function that feat: add dict::get #6265 already wraps indict_getitemref.dict::operator[]. The type goes fromitem_accessortodict_accessor. This breaks code that names the type, and it removes the deprecatedoperator bool, which is enabled only forgeneric_item.The rest of #2779 is obsolete or already on master: the Python 2 macro and the
dict_containstests.setdefaultwas discussed in the thread but is not in the diff. It has the same subclass problem asoperator[]if it usesPyDict_SetDefault, andd.attr("setdefault")(k, v)already works.Suggested changelog entry:
py::dict::get(key, default), equivalent to Python'sdict.get.