Skip to content

Commit

Permalink
columns: Make 'FormattableColumn' comparable
Browse files Browse the repository at this point in the history
Currently, comparing instances of this fails:

  >>> from cliff.columns import FormattableColumn
  >>> class Test(FormattableColumn):
  ...     def human_readable(self):
  ...         return str(self._data)
  ...
  >>> data = {'x': 'y'}
  >>> x = Test(data)
  >>> y = Test(data)
  >>> x == y
  False

Clearly it should not. Resolve this by implementing a custom comparison.

Change-Id: I4b96475ca6a689f4055dc5ea34b82b3867a65555
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
Story: #2008320
Task: #41218
  • Loading branch information
stephenfin committed Nov 4, 2020
1 parent def32ec commit 997e05f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cliff/columns.py
Expand Up @@ -24,17 +24,20 @@ class FormattableColumn(object):
def __init__(self, value):
self._value = value

def __eq__(self, other):
return (
self.__class__ == other.__class__ and self._value == other._value
)

@abc.abstractmethod
def human_readable(self):
"""Return a basic human readable version of the data.
"""
"""Return a basic human readable version of the data."""

def machine_readable(self):
"""Return a raw data structure using only Python built-in types.
It must be possible to serialize the return value directly
using a formatter like JSON, and it will be up to the
formatter plugin to decide how to make that transformation.
"""
return self._value

0 comments on commit 997e05f

Please sign in to comment.