Skip to content

Commit

Permalink
Simplify parameter value comparisons
Browse files Browse the repository at this point in the history
Cast self instead of other. This fails 5 == '5', which is less magic
(galaxyproject#9047 (comment))
  • Loading branch information
mvdbeek committed Nov 29, 2019
1 parent 85f6cf2 commit 4bf01c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
23 changes: 11 additions & 12 deletions lib/galaxy/tools/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,16 @@ def __init__(self, input, value, other_values={}):
self._other_values = other_values

def __eq__(self, other):
if isinstance(other, string_types) or (isinstance(other, InputValueWrapper) and other.input.type == 'text'):
return str(self) == other
elif isinstance(other, (int, float)) or (isinstance(other, InputValueWrapper) and other.input.type in ('float', 'integer')):
return float(self) == other
else:
return self.value == other
return self.get_cast_value() == other

def get_cast_value(self):
cast = {
'text': str,
'integer': int,
'float': float,
'boolean': bool,
}
return cast.get(self.input.type, str)(self)

def __ne__(self, other):
return not self == other
Expand All @@ -108,12 +112,7 @@ def __getattr__(self, key):
return getattr(self.value, key)

def __gt__(self, other):
if isinstance(other, string_types) or (isinstance(other, InputValueWrapper) and other.input.type == 'text'):
return str(self) > other
elif isinstance(other, (int, float)) or (isinstance(other, InputValueWrapper) and other.input.type in ('float', 'integer')):
return float(self) > other
else:
return self.value > other
return self.get_cast_value() > other

def __int__(self):
return int(float(self))
Expand Down
4 changes: 2 additions & 2 deletions test/unit/tools/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_input_value_wrapper_comparison(tool):
wrapper = valuewrapper(tool, 5, "integer")
assert str(wrapper) == "5"
assert int(wrapper) == 5
assert wrapper == "5"
assert wrapper != "5"
assert wrapper == 5
assert wrapper == 5.0
assert wrapper > 2
Expand All @@ -113,7 +113,7 @@ def test_input_value_wrapper_input_value_wrapper_comparison(tool):
wrapper = valuewrapper(tool, 5, "integer")
assert str(wrapper) == valuewrapper(tool, "5", "text")
assert int(wrapper) == valuewrapper(tool, "5", "integer")
assert wrapper == valuewrapper(tool, "5", "text")
assert wrapper != valuewrapper(tool, "5", "text")
assert wrapper == valuewrapper(tool, "5", "integer")
assert wrapper == valuewrapper(tool, "5", "float")
assert wrapper > valuewrapper(tool, "2", "integer")
Expand Down

0 comments on commit 4bf01c8

Please sign in to comment.