Skip to content

Commit

Permalink
fix(yaml): stringify DebianArchitectures for YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Mar 6, 2025
1 parent 63f818b commit 4948831
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions craft_application/util/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pathlib
from typing import TYPE_CHECKING, Any, TextIO, cast, overload

import craft_platforms
import yaml

from craft_application import errors
Expand Down Expand Up @@ -141,6 +142,11 @@ def dump_yaml(data: Any, stream: TextIO | None = None, **kwargs: Any) -> str | N
yaml.add_representer(
str, _repr_str, Dumper=cast(type[yaml.Dumper], yaml.SafeDumper)
)
yaml.add_representer(
craft_platforms.DebianArchitecture,
_repr_str,
Dumper=cast(type[yaml.Dumper], yaml.SafeDumper),
)
kwargs.setdefault("sort_keys", False)
kwargs.setdefault("allow_unicode", True)
return cast( # This cast is needed for pyright but not mypy
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/util/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io
import pathlib

import craft_platforms
import pytest
import pytest_check

Expand Down Expand Up @@ -90,6 +91,7 @@ def test_safe_yaml_loader_specific_error(yaml_text: str, error_msg: str):
{"sort_keys": True},
"comes_first: true\nordered: 'yes'\n",
),
({"arch": craft_platforms.DebianArchitecture.RISCV64}, {}, "arch: riscv64\n"),
],
)
def test_dump_yaml_to_string(data, kwargs, expected):
Expand Down

0 comments on commit 4948831

Please sign in to comment.