Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ This document explains the changes made to Iris for this release
representation of the 64-bit non-cryptographic hash of the object using the
extremely fast `xxhash`_ hashing algorithm. (:pull:`4020`)

#. `@rcomer`_ implemented a ``__str__`` method for metadata classes, so
printing these objects skips metadata elements that are set to None or an
empty string or dictionary. (:pull:`4040`)


🐛 Bugs Fixed
=============
Expand Down
10 changes: 10 additions & 0 deletions lib/iris/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ def __ne__(self, other):

return result

def __str__(self):
field_strings = []
for field in self._fields:
value = getattr(self, field)
if value is None or isinstance(value, (str, dict)) and not value:
continue
field_strings.append(f"{field}={value}")

return f"{type(self).__name__}({', '.join(field_strings)})"

def _api_common(
self, other, func_service, func_operation, action, lenient=None
):
Expand Down