Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

shubhamsugara22
Copy link

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 position attribute to a string (if it exists) for consistent sorting.
  • Fallback to sorting by the name when position is not available.
  • Ensure Python 3 compatibility by avoiding mixed-type comparisons during parameter sorting.

Fixes #5130

@pull-request-size pull-request-size bot added the size/S PR that changes 10-29 lines. Very easy to review. label Oct 20, 2024
@CLAassistant
Copy link

CLAassistant commented Oct 20, 2024

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

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.

@pull-request-size pull-request-size bot added size/L PR that changes 100-499 lines. Requires some effort to review. and removed size/S PR that changes 10-29 lines. Very easy to review. labels Oct 20, 2024
@pull-request-size pull-request-size bot added size/S PR that changes 10-29 lines. Very easy to review. and removed size/L PR that changes 100-499 lines. Requires some effort to review. labels Oct 20, 2024
@shubhamsugara22
Copy link
Author

@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

@pull-request-size pull-request-size bot added size/L PR that changes 100-499 lines. Requires some effort to review. and removed size/S PR that changes 10-29 lines. Very easy to review. labels Nov 17, 2024
@pull-request-size pull-request-size bot added size/S PR that changes 10-29 lines. Very easy to review. and removed size/L PR that changes 100-499 lines. Requires some effort to review. labels Nov 17, 2024
@shubhamsugara22
Copy link
Author

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

@cognifloyd
Copy link
Member

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

@shubhamsugara22
Copy link
Author

@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
Copy link
Member

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
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/S PR that changes 10-29 lines. Very easy to review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with action help display when having ordered optional parameters
3 participants