Repository navigation
fix(defaults): synchronize setdefault attributes - #53
Merged
leplatrem merged 2 commits intoNov 9, 2026
Merged
Conversation
leplatrem
approved these changes
Nov 9, 2026
leplatrem
added a commit
that referenced
this pull request
Nov 9, 2026
* Update __init__.py so EasyDict does not convert subclasses of dict, tuple, and list like datasets.dataset_dict.DatasetDict * Create test.py Add `test.py` to validate the auto-conversion of `list`, `tuple`, and `dict`, and to validate the non-auto-conversion of their subclasses. * Fix update() mutating its positional argument (#54) When called as obj.update(src, key=val), `d = e or dict()` aliased the caller's dict `e` (a non-empty dict is truthy), so `d.update(f)` merged the keyword arguments into `e` itself. Standard dict.update never mutates its argument; this left the caller's `src` silently modified. Use `dict(e) if e else {}` to operate on a copy. Added a doctest asserting the argument is unchanged. * fix: synchronize setdefault attributes (#53) Co-authored-by: Mathieu Leplatre <mathieu@leplat.re> * Move to docstring tests * Fix lint * Drop EOL python versions --------- Co-authored-by: Dream <2468001320@qq.com> Co-authored-by: Awais Ur Rehman <75811924+awais-ur-rehman@users.noreply.github.com> Co-authored-by: Mathieu Leplatre <mathieu@leplat.re> Co-authored-by: Mathieu Leplatre <mathieu@mozilla.com>
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.
Summary
EasyDict.setdefault()currently inherits the built-in dict implementation, which inserts values without passing through the attribute/recursive conversion path. For example, initializing a nested configuration withconfig.setdefault('settings', {'enabled': True})leavesconfig.settingsunavailable.Route insertion through the existing assignment implementation and return the stored value, preserving normal setdefault behavior for existing keys. Add doctests for scalar and nested defaults, return identity, existing keys, omitted defaults, and ensuring the new method is not added as a dictionary key. Non-string and double-underscore keys retain dictionary-only insertion, preserving their previous behavior and avoiding mutation of Python special attributes.
Fixes #24.
Testing
Breaking changes
New ordinary string-key defaults now follow EasyDict assignment semantics, including shadowing same-named methods. Non-string and magic-name keys retain dictionary-only behavior.
Manual check:
config.setdefault("settings", {"enabled": True})returns the stored EasyDict, andconfig.settings.enabledis accessible. Existing defaults are not overwritten.Assistance
Prepared with OpenAI Codex assistance, including reproduction, implementation and local validation.