StackStorm / st2

StackStorm (aka "IFTTT for Ops") is event-driven automation for auto-remediation, incident responses, troubleshooting, deployments, and more for DevOps and SREs. Includes rules engine, workflow, 160 integration packs with 6000+ actions (see https://exchange.stackstorm.org) and ChatOps. Installer at https://docs.stackstorm.com/install/index.html
https://stackstorm.com/
Apache License 2.0
6.09k stars 746 forks source link

Fix TypeError in help command by handling mixed parameter types for sorting #6266

Open shubhamsugara22 opened 1 month ago

shubhamsugara22 commented 1 month ago

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:

Fixes #5130

CLAassistant commented 1 month ago

CLA assistant check
All committers have signed the CLA.

CLAassistant commented 1 month ago

CLA assistant check
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.

shubhamsugara22 commented 1 month ago

@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

shubhamsugara22 commented 5 days ago

@amanda11 @nzlosh @cognifloyd Can you review this request ?

cognifloyd commented 5 days ago

Converting position to string will change the sort order; for example "10" sorts before "2". So, I'm concerned about merging this solution.

shubhamsugara22 commented 4 days ago

@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

cognifloyd commented 4 days ago

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.

shubhamsugara22 commented 4 days ago

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)