Skip to content

Commit 427310a

Browse files
committed
Consider "empty" only None and empty containers and add a comment about that.
1 parent 8cf1b08 commit 427310a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

dandischema/metadata.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,20 +451,28 @@ def migrate(
451451
# Downgrades
452452

453453
# Simple downgrades that just require removing fields, which is totally fine
454-
# if they are empty
454+
# if they are empty, as they are None or empty containers (list, tuple, etc).
455+
# List only those for which such notion of "empty" applies.
455456
SIMPLE_DOWNGRADES = [
456457
# version added, fields to remove
457458
("0.6.11", ["releaseNotes"]),
458459
]
459460
for ver_added, fields in SIMPLE_DOWNGRADES:
460461
# additional guards are via ALLOWED_TARGET_SCHEMAS
461-
if (to_version_tuple < version2tuple(ver_added) <= obj_version_tuple):
462+
if to_version_tuple < version2tuple(ver_added) <= obj_version_tuple:
462463
for field in fields:
463464
if field in obj_migrated:
464-
if val := obj_migrated.get(field):
465-
raise ValueError(f"Cannot downgrade to {to_version} from "
466-
f"{obj_version} with {field}={val!r} present")
467-
del obj_migrated[field]
465+
value = obj_migrated.get(field)
466+
# Explicit check for "empty" value per above description.
467+
if value is None or (
468+
not value and isinstance(value, (list, tuple, dict, set))
469+
):
470+
del obj_migrated[field]
471+
else:
472+
raise ValueError(
473+
f"Cannot downgrade to {to_version} from "
474+
f"{obj_version} with {field}={value!r} present"
475+
)
468476

469477
# Always update schemaVersion when migrating
470478
obj_migrated["schemaVersion"] = to_version

0 commit comments

Comments
 (0)