This is an automated email from the ASF dual-hosted git repository.
kaxil pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 33fd85055b7 Fix `merge_dicts` crash when a dict overrides a non-dict
value (#70349)
33fd85055b7 is described below
commit 33fd85055b7fa50501403425aa3ea2f97e980bde
Author: Nikolaus Schuetz <[email protected]>
AuthorDate: Fri Nov 27 18:37:37 2026 -0500
Fix `merge_dicts` crash when a dict overrides a non-dict value (#70349)
merge_dicts recursed whenever the value in dict2 was a dict, without also
checking that the existing value in dict1 was a dict. Merging a dict over a
scalar or list value therefore raised (AttributeError/TypeError) instead of
overwriting it as the docstring promises ("Items in dict2 overwrite those
also found in dict1").
* Parametrize merge_dicts non-dict override test
Claude-Session: https://claude.ai/code/session_019p6yTLVyh3x4UFbpn2bN78
---
airflow-core/src/airflow/utils/helpers.py | 4 ++--
airflow-core/tests/unit/utils/test_helpers.py | 4 ++++
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/airflow-core/src/airflow/utils/helpers.py
b/airflow-core/src/airflow/utils/helpers.py
index 51691a2dce1..43a9fae4d34 100644
--- a/airflow-core/src/airflow/utils/helpers.py
+++ b/airflow-core/src/airflow/utils/helpers.py
@@ -151,8 +151,8 @@ def merge_dicts(dict1: dict, dict2: dict) -> dict:
"""
merged = dict1.copy()
for k, v in dict2.items():
- if k in merged and isinstance(v, dict):
- merged[k] = merge_dicts(merged.get(k, {}), v)
+ if k in merged and isinstance(v, dict) and isinstance(merged[k], dict):
+ merged[k] = merge_dicts(merged[k], v)
else:
merged[k] = v
return merged
diff --git a/airflow-core/tests/unit/utils/test_helpers.py
b/airflow-core/tests/unit/utils/test_helpers.py
index 7136db3c149..213b76d5c5a 100644
--- a/airflow-core/tests/unit/utils/test_helpers.py
+++ b/airflow-core/tests/unit/utils/test_helpers.py
@@ -113,6 +113,10 @@ class TestHelpers:
merged = merge_dicts(dict1, dict2)
assert merged == {"a": 1, "r": {"b": 0, "c": 3}}
+ @pytest.mark.parametrize("existing", [1, "x", [1, 2], None])
+ def test_merge_dicts_overwrites_non_dict_with_dict(self, existing):
+ assert merge_dicts({"a": existing}, {"a": {"b": 2}}) == {"a": {"b": 2}}
+
def test_build_airflow_dagrun_url(self):
expected_url = "/dags/somedag/runs/abc123"
assert build_airflow_dagrun_url(dag_id="somedag", run_id="abc123") ==
expected_url