Skip to content

Commit bbd2902

Browse files
authored
Fix bug in prune_dict where empty dict and list would be removed even in strict mode (#32573)
1 parent 94122d1 commit bbd2902

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

airflow/providers/amazon/aws/utils/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,28 @@
2121
from datetime import datetime
2222
from enum import Enum
2323

24+
from airflow.utils.helpers import prune_dict
2425
from airflow.version import version
2526

2627
log = logging.getLogger(__name__)
2728

2829

2930
def trim_none_values(obj: dict):
30-
return {key: val for key, val in obj.items() if val is not None}
31+
from packaging.version import Version
32+
33+
from airflow.version import version
34+
35+
if Version(version) < Version("2.7"):
36+
# before version 2.7, the behavior is not the same.
37+
# Empty dict and lists are removed from the given dict.
38+
return {key: val for key, val in obj.items() if val is not None}
39+
else:
40+
# once airflow 2.6 rolls out of compatibility support for provider packages,
41+
# we can replace usages of this method with the core one in our code,
42+
# and uncomment this warning for users who may use it.
43+
# warnings.warn("use airflow.utils.helpers.prune_dict() instead",
44+
# AirflowProviderDeprecationWarning, stacklevel=2)
45+
return prune_dict(obj)
3146

3247

3348
def datetime_to_epoch(date_time: datetime) -> int:

airflow/utils/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def is_empty(x):
341341
continue
342342
elif isinstance(v, (list, dict)):
343343
new_val = prune_dict(v, mode=mode)
344-
if new_val:
344+
if not is_empty(new_val):
345345
new_dict[k] = new_val
346346
else:
347347
new_dict[k] = v
@@ -353,7 +353,7 @@ def is_empty(x):
353353
continue
354354
elif isinstance(v, (list, dict)):
355355
new_val = prune_dict(v, mode=mode)
356-
if new_val:
356+
if not is_empty(new_val):
357357
new_list.append(new_val)
358358
else:
359359
new_list.append(v)

tests/providers/amazon/aws/utils/test_utils.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
datetime_to_epoch_ms,
2727
datetime_to_epoch_us,
2828
get_airflow_version,
29-
trim_none_values,
3029
)
3130

3231
DT = datetime(2000, 1, 1, tzinfo=pytz.UTC)
@@ -37,17 +36,6 @@ class EnumTest(_StringCompareEnum):
3736
FOO = "FOO"
3837

3938

40-
def test_trim_none_values():
41-
input_object = {
42-
"test": "test",
43-
"empty": None,
44-
}
45-
expected_output_object = {
46-
"test": "test",
47-
}
48-
assert trim_none_values(input_object) == expected_output_object
49-
50-
5139
def test_datetime_to_epoch():
5240
assert datetime_to_epoch(DT) == EPOCH
5341

tests/utils/test_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ def assert_at_most_one(true=0, truthy=0, false=0, falsy=0, notset=0):
309309
"c": {"b": "", "c": "hi", "d": ["", 0, "1"]},
310310
"d": ["", 0, "1"],
311311
"e": ["", 0, {"b": "", "c": "hi", "d": ["", 0, "1"]}, ["", 0, "1"], [""]],
312+
"f": {},
313+
"g": [""],
312314
},
313315
),
314316
(
@@ -324,7 +326,7 @@ def assert_at_most_one(true=0, truthy=0, false=0, falsy=0, notset=0):
324326
def test_prune_dict(self, mode, expected):
325327
l1 = ["", 0, "1", None]
326328
d1 = {"a": None, "b": "", "c": "hi", "d": l1}
327-
d2 = {"a": None, "b": "", "c": d1, "d": l1, "e": [None, "", 0, d1, l1, [""]]}
329+
d2 = {"a": None, "b": "", "c": d1, "d": l1, "e": [None, "", 0, d1, l1, [""]], "f": {}, "g": [""]}
328330
assert prune_dict(d2, mode=mode) == expected
329331

330332

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy