Skip to content

fix(defaults): synchronize setdefault attributes - #53

Merged
leplatrem merged 2 commits into
makinacorpus:masterfrom
awais-ur-rehman:feat/setdefault-attributes
Nov 9, 2026
Merged

leplatrem merged 2 commits into
makinacorpus:masterfrom
awais-ur-rehman:feat/setdefault-attributes

Conversation

@awais-ur-rehman

Copy link
Copy Markdown
Contributor

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 with config.setdefault('settings', {'enabled': True}) leaves config.settings unavailable.

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

  • Complete doctest suite: 72 passed on Python 3.9.6, 3.11.15 and 3.12.13.
  • Regression verified before the implementation: five doctest failures.
  • Ruff 0.17.3 reports the same ten existing findings on base and head; no new findings.
  • Python 3.8/3.10 were not tested locally.

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, and config.settings.enabled is accessible. Existing defaults are not overwritten.

Assistance

Prepared with OpenAI Codex assistance, including reproduction, implementation and local validation.

@leplatrem
leplatrem merged commit 9a9311f into makinacorpus:master 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>
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.

setdefault not work

2 participants