diff --git a/swh/model/model.py b/swh/model/model.py --- a/swh/model/model.py +++ b/swh/model/model.py @@ -18,6 +18,7 @@ from .collections import ImmutableDict from .hashutil import DEFAULT_ALGORITHMS, MultiHash, hash_to_bytes from .identifiers import ( + _BaseSWHID, directory_identifier, extid_identifier, normalize_timestamp, @@ -66,16 +67,19 @@ def dictify(value): "Helper function used by BaseModel.to_dict()" - if isinstance(value, BaseModel): + if isinstance(value, (int, float, str, bytes)): + # short-path for common objects + return value + elif hasattr(value, "to_dict"): # model object return value.to_dict() - elif isinstance(value, (CoreSWHID, ExtendedSWHID)): - return str(value) + elif hasattr(value, "items"): # dict + return {k: dictify(v) for k, v in value.items()} + elif hasattr(value, "__iter__"): # tuple + return tuple(map(dictify, value)) elif isinstance(value, Enum): return value.value - elif isinstance(value, (dict, ImmutableDict)): - return {k: dictify(v) for k, v in value.items()} - elif isinstance(value, tuple): - return tuple(dictify(v) for v in value) + elif isinstance(value, _BaseSWHID): + return str(value) else: return value