Skip to content

Commit 074cb29

Browse files
fix: make toyaml filter type-aware — preserve int/bool, only quote ambiguous strings
Co-Authored-By: Itamar Hartstein <haritamar@gmail.com>
1 parent 91f260c commit 074cb29

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

tests/profiles/generate_profiles.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,27 @@ def _yaml_inline(value: Any) -> str:
3232
"""Dump *value* as a YAML-safe scalar or compact inline mapping.
3333
3434
For dicts (e.g. bigquery_keyfile) this produces ``{key: val, ...}``.
35-
For scalars it strips the trailing YAML document-end marker (``...``)
36-
that ``yaml.dump`` appends to bare values.
35+
For non-string scalars (int, float, bool) the value passes through
36+
unchanged so that YAML keeps its native type.
37+
For strings, values that YAML would misinterpret (e.g. ``"yes"`` as
38+
bool, ``"123"`` as int, ``"null"`` as None) are quoted.
3739
"""
3840
if isinstance(value, Undefined):
3941
return "{}"
40-
dumped = yaml.dump(value, default_flow_style=True)
41-
result = dumped.rstrip()
42-
if result.endswith("\n..."):
43-
result = result[: -len("\n...")]
44-
elif result.endswith("..."):
45-
result = result[: -len("...")]
46-
return result.rstrip()
42+
if isinstance(value, dict):
43+
return yaml.dump(value, default_flow_style=True).strip()
44+
if not isinstance(value, str):
45+
# int, float, bool — pass through so YAML keeps native type
46+
return str(value).lower() if isinstance(value, bool) else str(value)
47+
# For strings, check if YAML would misinterpret the value
48+
loaded = yaml.safe_load(value)
49+
if loaded is None or not isinstance(loaded, str):
50+
# YAML would coerce to non-string — quote it
51+
dumped = yaml.dump(value, default_flow_style=True).rstrip()
52+
if dumped.endswith("..."):
53+
dumped = dumped[: -len("...")].rstrip()
54+
return dumped
55+
return value
4756

4857

4958
@click.command()

0 commit comments

Comments
 (0)