chore: Adds indent to YAML sequence by default for better visual readability

Both are valid YAML, just with indent, it's more visually friend to see the data structure hierarchy.

Before

```
items:
- item1
- item2
- item3
```

After

```
items:
  - item1
  - item2
  - item3
```
PiperOrigin-RevId: 806117290
This commit is contained in:
Wei Sun (Jack)
2025-09-11 21:59:20 -07:00
committed by Copybara-Service
parent f73ae6e101
commit 91528890db
2 changed files with 55 additions and 2 deletions
+9 -2
View File
@@ -43,9 +43,16 @@ def dump_pydantic_to_yaml(
file_path = Path(file_path)
file_path.parent.mkdir(parents=True, exist_ok=True)
# Create a custom dumper class
class _MultilineDumper(yaml.SafeDumper):
pass
def increase_indent(self, flow=False, indentless=False):
"""Override to force consistent indentation for sequences in mappings.
By default, PyYAML uses indentless=True for sequences that are values
in mappings, creating flush-left alignment. This override forces proper
indentation for all sequences regardless of context.
"""
return super(_MultilineDumper, self).increase_indent(flow, False)
def multiline_str_representer(dumper, data):
if '\n' in data: