Skip to content

fix: let nested keys override non-mapping values in build_dict (#215) - #224

Open
patchwright wants to merge 1 commit into
beetbox:mainfrom
patchwright:fix/build-dict-nested-collision
Open

patchwright wants to merge 1 commit into
beetbox:mainfrom
patchwright:fix/build-dict-nested-collision

Conversation

@patchwright

Copy link
Copy Markdown

Problem

Fixes #215. A nested key that overrides a non-mapping value inside an existing mapping raises. With APP_DATABASE="{host: localhost, port: 5432}" and APP_DATABASE__PORT=5433, EnvSource("APP_", parse_yaml_docs=True) fails with AttributeError: 'int' object has no attribute 'update'. set_args({'database': {'port': 5432}, 'database.port': 5433}, dots=True) hits the same path. Confirmed on main HEAD 23bc86a.

Root cause

build_dict in confuse/util.py calls save_to[key].update(result) whenever key already exists, even if the existing value is a scalar, list or None.

Fix

Only call .update() when both the existing value and the new result are dicts. Otherwise the new, more specific value replaces the old one. Mapping-to-mapping shallow merges behave as before. One line changed in util.py, plus tests and a changelog entry.

How to test

$ pytest test/test_utils.py test/test_env.py

The three new tests fail on current main (AttributeError) and pass with this change. Full suite: 308 passed, 1 skipped (test_release.py, needs click).

Backward compatibility

No API changes. Inputs that used to raise now return the more specific value.


Assisted-by: Claude (code generation, reviewed and tested locally)

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.

Nested overrides of full-YAML environment mappings raise AttributeError

1 participant