Skip to content

Preserve case when parsing boolean like strings#1634

Open
douglasjacobsen wants to merge 2 commits into
GoogleCloudPlatform:developfrom
douglasjacobsen:env-var-case
Open

Preserve case when parsing boolean like strings#1634
douglasjacobsen wants to merge 2 commits into
GoogleCloudPlatform:developfrom
douglasjacobsen:env-var-case

Conversation

@douglasjacobsen

Copy link
Copy Markdown
Collaborator

This merge adds a test, and fixes an issue where input strings (for example in env-vars) that can be converted into a boolean (i.e. TRUE) are converted into Python syntax for the booleans (i.e. True).

They now retain their original input case.

This commit updates the YAML parser so that input strings (for example,
TRUE) retain their case, and are not converted into Python equivalents
(i.e. True).
@douglasjacobsen douglasjacobsen added the bug Something isn't working label Jun 23, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces syaml_bool to preserve the original string representation of booleans (such as 'TRUE' or 'False') when parsing and dumping YAML files, and updates the validator, expander, and variant modules to support it. The review feedback highlights a duplicate and buggy definition of represent_bool in OrderedLineDumper inside spack_yaml.py, and recommends replacing fragile string-based type checks (type(...).__name__ == "syaml_bool") in variants.py with robust isinstance checks.

Comment on lines +205 to +209
def represent_bool(self, data):
if hasattr(data, 'string_val') and data.string_val:
return self.represent_scalar('tag:yaml.org,2002:bool', data.string_val)
return super(SafeDumper, self).represent_bool(bool(data))

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.

high

There is a duplicate definition of represent_bool in OrderedLineDumper. Additionally, this first definition incorrectly uses super(SafeDumper, self) instead of super(OrderedLineDumper, self), which would raise a TypeError at runtime if it were ever called. Since it is shadowed by the correct definition further down, this duplicate should be removed entirely to avoid confusion and potential bugs.

Comment on lines +192 to +195
if default_var and (
isinstance(default_var.default, bool)
or type(default_var.default).__name__ == "syaml_bool"
):

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.

medium

Using type(default_var.default).__name__ == "syaml_bool" is a fragile string-based type check. It is more robust and idiomatic to import syaml_bool and use isinstance to check the type, which also correctly handles subclasses.

        from spack.util.spack_yaml import syaml_bool
        if default_var and isinstance(default_var.default, (bool, syaml_bool)):


def _build_definitions(self) -> tuple:
if isinstance(self.default, bool):
if isinstance(self.default, bool) or type(self.default).__name__ == "syaml_bool":

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.

medium

Using type(self.default).__name__ == "syaml_bool" is a fragile string-based type check. It is more robust and idiomatic to import syaml_bool and use isinstance to check the type.

Suggested change
if isinstance(self.default, bool) or type(self.default).__name__ == "syaml_bool":
from spack.util.spack_yaml import syaml_bool
if isinstance(self.default, (bool, syaml_bool)):

def format_value(self, value: Any) -> str:
"""Format a value for this variant into Spack-like syntax"""
if isinstance(self.default, bool):
if isinstance(self.default, bool) or type(self.default).__name__ == "syaml_bool":

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.

medium

Using type(self.default).__name__ == "syaml_bool" is a fragile string-based type check. It is more robust and idiomatic to import syaml_bool and use isinstance to check the type.

Suggested change
if isinstance(self.default, bool) or type(self.default).__name__ == "syaml_bool":
from spack.util.spack_yaml import syaml_bool
if isinstance(self.default, (bool, syaml_bool)):

@ramble-pr-bot

ramble-pr-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

Ramble Performance Test Metrics

Results produced with commit: 58252c2

Test Name Outcome Duration (s) Most Recent Run (s) Last 5 Avg (s)
test_analyze_large_file passed 2.9673 3.0821 (09ce8a0) 3.0300
test_large_template_expansion passed 2.1020 2.1886 (09ce8a0) 2.1971
test_many_experiments passed 33.3007 35.3749 (09ce8a0) 34.7099
test_many_objects_defaults passed 18.4936 19.6031 (09ce8a0) 19.2642
test_matrix_filter_perf passed 1.6118 1.6886 (09ce8a0) 1.6532

@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.19%. Comparing base (09ce8a0) to head (58252c2).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1634   +/-   ##
========================================
  Coverage    93.19%   93.19%           
========================================
  Files          345      345           
  Lines        33456    33474   +18     
========================================
+ Hits         31178    31196   +18     
  Misses        2278     2278           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant