st2
st2 copied to clipboard
Fix TypeError in help command by handling mixed parameter types for sorting
Summary:
This PR addresses the issue where the st2 run action pack.action -h command fails with a TypeError: '<' not supported between instances of 'str' and 'int'. The error occurs when sorting action parameters that have a mix of names (strings) and positions (integers). In Python 3, comparing these types directly causes a failure.
Root Cause:
The sorting function attempts to sort action parameters by either the position attribute (if present) or the name. In some cases, position is an integer while name is a string, leading to a type comparison error in Python 3.
Fix:
The _get_parameter_sort_value method has been updated to:
- Convert the
positionattribute to a string (if it exists) for consistent sorting. - Fallback to sorting by the
namewhenpositionis not available. - Ensure Python 3 compatibility by avoiding mixed-type comparisons during parameter sorting.
Fixes #5130
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.
@nzlosh @cognifloyd can you check getting lint error again and again adjusted the file according to black format still logic is fine but because of lint check is failing
@amanda11 @nzlosh @cognifloyd Can you review this request ?
Converting position to string will change the sort order; for example "10" sorts before "2". So, I'm concerned about merging this solution.
@cognifloyd made some changes but does it look like , is this change feasible to apply if yes , then can discuss how to proceed further otherwise , we can close if the issue doesn't look like solving
The logic is now pretty much the same as before.
I think a clean way to solve this would be to create a custom object wrapper that handles sorting comparisons. The object would wrap either name or position (a str or int). When the objects are compared with each other, an int should be sorted before a str, and if the types are the same, use the standard python comparison.
Something like this (ignore the class it can be updated later & mainly focus on logic) @cognifloyd
class ParameterWrapper:
def __init__(self, value):
self.value = value
def __lt__(self, other):
# Prioritize int values over str values
if isinstance(self.value, int) and isinstance(other.value, str):
return True
if isinstance(self.value, str) and isinstance(other.value, int):
return False
# Use natural ordering if types are the same
return self.value < other.value
def __repr__(self):
return f"ParameterWrapper({self.value})"
and update the existing function
if parameter:
position = parameter.get("position")
# Return a ParameterWrapper object for consistent sorting behavior
return ParameterWrapper(position if position is not None else name)
Closing the PR not able to put time on it Getting stuck on sorry for wasting time