Skip to content

fix(intrinsics): compare Fn::Equals values as strings - #9407

Merged
vicheey merged 2 commits into
aws:developfrom
Shaurya55555:fix/fn-equals-string-comparison
Nov 10, 2026
Merged

vicheey merged 2 commits into
aws:developfrom
Shaurya55555:fix/fn-equals-string-comparison

Conversation

@Shaurya55555

@Shaurya55555 Shaurya55555 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Which issue(s) does this change fix?

None filed. Related: #4308 uses the same condition (!Equals [!Ref SomeParameter, true]), but its deploy error comes from the SAM translator, so this does not fix it.

Why is this change necessary?

IntrinsicResolver.handle_fn_equals compares the two resolved values with Python ==. A parameter value is a string, while a YAML literal such as true or 1 is a bool or an int, so !Equals [!Ref EnableX, true] is never true and an Fn::If that uses it takes the false branch. With P="false" and !Equals [!Ref P, false] it also returns false, because "false" != False.

ConditionResolver._resolve_equals in cfn_language_extensions already handles this ("CloudFormation Fn::Equals performs string comparison"), so the two resolvers disagree on the same template. SamBaseProvider.get_template runs the old resolver last, so sam local and sam build see the wrong value for properties set through Fn::If.

How does it address the issue?

Both values are converted to strings before comparing, with a bool lowercased to true/false, the same rule _resolve_equals uses.

Tests: one parameterized test, test_fn_equals_compares_as_strings, with 11 cases (string, bool and int literals against string parameter values, equal and not equal, including "True" vs true and "Prod" vs "prod", which stay unequal because the comparison is case-sensitive).

What side effects does this have?

Comparisons between values of different types that print the same now succeed, for example the string "1" and the int 1. Two behavior changes in the other direction, both pinned by tests: True vs 1 was equal in Python and is now not equal ("true" vs "1"), and 1 vs 1.0 was equal and is now not equal ("1" vs "1.0"). Comparisons between two strings, and between two values of the same type, behave as before. sam local and sam build can therefore resolve some templates differently, which may be worth a release note.

Mandatory Checklist

PRs will only be reviewed after checklist is complete

  • Review the generative AI contribution guidelines (I used an AI assistant, Claude Code, to find and draft this. I reviewed the change and can answer questions about it.)
  • Add input/output type hints to new functions/methods (the new private static helper is typed)
  • Write design document if needed (not needed)
  • Write/update unit tests
  • Write/update integration tests (not needed for this change)
  • Write/update functional tests if needed (not needed)
  • make pr passes (not run. black --check -l 120 passes on both files)
  • make update-reproducible-reqs if dependencies were changed (no dependencies changed)
  • Write documentation (not needed)

How I checked it

The resolver files are copies of develop before and after this change, with a stub for the one unrelated import (get_template_data, used only by Fn::Transform).

  • Fn::If on a Fn::Equals [Ref P, literal] condition: with P="true" and true, P="false" and false, P="1" and 1, P="1.5" and 1.5, and P=1 against "1", the old code returns the false branch and the new code returns the true branch. P="prod" against "dev" still returns the false branch.
  • All 302 tests in test_intrinsic_resolver.py pass after the change. Against the old code, 6 of the 11 new cases fail: the 4 string-comparison cases, and the True vs 1 and 1 vs 1.0 cases above.
  • Not run: the full unit suite, integration tests and make pr. I could not check the string-comparison rule against a real CloudFormation stack; I relied on _resolve_equals.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@Shaurya55555
Shaurya55555 requested a review from a team as a code owner November 6, 2026 07:56
@github-actions github-actions Bot added pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Nov 6, 2026

@vicheey vicheey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up! A couple of small things before merge:

  • Could you add a few test cases: ("True", True, False), ("Prod", "prod", False), (True, 1, False) and (1, 1.0, False)? Right now a case-insensitive compare would still pass every test, and the last two are behavior changes worth pinning (and calling out in the description).
  • Nit: type hints on _equals_as_string, and a one-line comment pointing at ConditionResolver._resolve_equals so the two stay in sync.

Since sam local / sam build will resolve some templates differently after this, it's probably worth a line in the release notes.

@Shaurya55555

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed 6c5c6b7:

  • Added the four cases you suggested, ("True", True), ("Prod", "prod"), (True, 1) and (1, 1.0), all expecting False. Against the old code the last two fail, because Python treated True == 1 and 1 == 1.0 as equal, so they are behavior changes. I called both out in the description.
  • Added type hints to _equals_as_string and a comment pointing at ConditionResolver._resolve_equals.
  • Release note: I did not touch a release-notes file because I could not find where the project keeps them. If you tell me where it goes, I will add the line.

All 302 tests in test_intrinsic_resolver.py pass.

@vicheey
vicheey self-requested a review November 10, 2026 20:45

@bnusunny bnusunny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I checked the new rule against a real CloudFormation stack, and SAM CLI now gives the same answer in every case I tried. develop got three of them wrong.

Fn::Equals CloudFormation this PR develop
"true" vs yes equal equal not equal
"true" vs on equal equal not equal
"true" vs True equal equal not equal
"True" vs True not equal not equal not equal
"yes" vs yes not equal not equal not equal
"1" vs 1.0 not equal not equal not equal
"1.50" vs 1.50 not equal not equal not equal

CloudFormation reads unquoted yes, on and True as the boolean true and compares it as "true", which is the rule this PR uses. It also normalises YAML numbers, so the 1 vs 1.0 change matches CloudFormation.

Optional follow-up, not blocking: str() now applies to lists too, so [a] equals the string "['a']". ConditionResolver rejects list and map arguments, and that check could be shared here.

On release notes: the repo doesn't keep a changelog file. Maintainers write the notes when they cut a release.

@vicheey
vicheey merged commit c77273d into aws:develop Nov 10, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants