Skip to content

Keep the box configuration on Box.get() dict and list defaults - #364

Open
EnesKartal wants to merge 1 commit into
cdgriffith:developfrom
EnesKartal:fix-get-default-config
Open

EnesKartal wants to merge 1 commit into
cdgriffith:developfrom
EnesKartal:fix-get-default-config

Conversation

@EnesKartal

Copy link
Copy Markdown

Box.get(key, default) converts a plain dict / list default into a Box / BoxList, but it builds them with no options, so the returned value loses the parent's configuration (box_dots, default_box, camel_killer_box, frozen_box, ...) and is always a plain Box even for subclasses. setdefault() already passes the parent config through, so the two disagree:

from box import Box

bx = Box(box_dots=True, default_box=True)
bx.setdefault("x", {"a": {"b": 1}})["a.b"]   # 1
bx.get("y", {"a": {"b": 1}})["a.b"]          # BoxKeyError: "'a.b'"

The fix builds the default the same way setdefault() does: box_class with self.__box_config(extra_namespace=key), and BoxList with the same config. The default is still not stored in the box.

Test plan

  • Added test_get_default_keeps_box_config (box_dots path, camel_killer attribute, default_box lookup, BoxList default, subclass type, key not stored).
  • Before the fix: 1 failed, 28 passed for pytest test/test_box.py -k get (BoxKeyError: "'a.b'").
  • After the fix: pytest test/ gives 1210 passed; the only failures are the 6 toon tests that also fail on master without the optional toon dependency installed.
  • ruff check / ruff format --check pass.

This branch has not been deployed

No deployments
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.

1 participant