Skip to content

fix only_if type conversion bug#203

Merged
whotwagner merged 2 commits intoait-testbed:developmentfrom
thorinaboenke:fix/conditional_string_comparison
Feb 12, 2026
Merged

fix only_if type conversion bug#203
whotwagner merged 2 commits intoait-testbed:developmentfrom
thorinaboenke:fix/conditional_string_comparison

Conversation

@thorinaboenke
Copy link
Contributor

@thorinaboenke thorinaboenke commented Feb 12, 2026

This PR relates to #202

When compare_value method of the Conditional sees ast.Constant it always tries to convert it to an integer with return int(name.value). --> assumed all constants are integers, but ast.Constant can hold any literal value (strings, numbers, booleans, etc.). Solution: returning name.value directly instead of int(name.value), Python's built-in comparison operators will handle type checking.

playbook content:
Contents of demo_int.yml:

commands:
  - type: shell
    cmd: echo "hello world 123"

    # search for '123', save the first match to $VAR_INT
  - type: regex
    cmd: "123"
    output:
        VAR_INT: "$MATCH_0"

  - type: debug
    cmd: "Content of VAR_INT: $VAR_INT"

  - type: debug
    cmd: "this only prints if VAR_INT == '123'"
    only_if: $VAR_INT == "123"

  - type: debug
    cmd: "this only prints if VAR_INT == '123'"
    only_if: "$VAR_INT == 123"

Contents of demo_str.yml:

commands:
  - type: shell
    cmd: echo "hello world 123"

    # search for 'hello', save the first match to $VAR_STR
  - type: regex
    cmd: "hello"
    output:
        VAR_STR: "$MATCH_0"

  - type: debug
    cmd: "Content of VAR_STR: $VAR_STR"

    # this prints just fine
  - type: debug
    cmd: "this only prints if VAR_STR == 'hello'"
    only_if: "$VAR_STR == hello"

    # this crashes even though 'hello' is part of the original string
  - type: debug
    cmd: "this only prints if VAR_STR == 'hello'"
    only_if: $VAR_STR == "hello"
image

Additionally fixes test test/conditionals.py
Explanation.
The test is wrong because it misunderstands how Python's is operator works versus how the Conditional class implements it. == compares values (equality), is compares identity

# In actual Python:
1 == True   # True (values are equal, Python treats 1 and True as equal)
1 is True   # False (different objects in memory)

in python 1 is True is always FALSE because 1 is an integer object and True is a boolean object.
image

Before this PR both cast to integer let test pass

@whotwagner whotwagner merged commit 94a55dd into ait-testbed:development Feb 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants