diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index e311df418c..4e3add0e2d 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed * [JS/TS] Fixed TimeSpan.FromMilliseconds (#3815) (by @ncave) +* [Python] Fixed quotation for union string cases (by @dbrattli) ## 4.17.0 - 2024-04-23 diff --git a/src/fable-library-py/fable_library/types.py b/src/fable-library-py/fable_library/types.py index 438858e1fa..eb4505f608 100644 --- a/src/fable-library-py/fable_library/types.py +++ b/src/fable-library-py/fable_library/types.py @@ -55,8 +55,7 @@ def __init__(self): @staticmethod @abstractmethod - def cases() -> list[str]: - ... + def cases() -> list[str]: ... @property def name(self) -> str: @@ -66,14 +65,19 @@ def __str__(self) -> str: if not len(self.fields): return self.name + def to_string(value: Any) -> str: + if isinstance(value, str): + return f'"{value}"' + return str(value) + fields = "" with_parens = True if len(self.fields) == 1: - field = str(self.fields[0]) + field = to_string(self.fields[0]) with_parens = field.find(" ") >= 0 fields = field else: - fields = ", ".join(map(str, self.fields)) + fields = ", ".join(map(to_string, self.fields)) return self.name + (" (" if with_parens else " ") + fields + (")" if with_parens else "") @@ -204,8 +208,7 @@ def __hash__(self) -> int: return record_get_hashcode(self) -class Attribute: - ... +class Attribute: ... def seq_to_string(self: Iterable[Any]) -> str: diff --git a/tests/Python/TestUnionType.fs b/tests/Python/TestUnionType.fs index d581a27e0b..c8aa09712a 100644 --- a/tests/Python/TestUnionType.fs +++ b/tests/Python/TestUnionType.fs @@ -205,3 +205,9 @@ let ``test Equality works in filter`` () = |> Array.filter (fun r -> r.Case = MyUnion3.Case1) |> Array.length |> equal 2 + +type S = S of string +[] +let ``test sprintf formats strings cases correctly`` () = + let s = sprintf "%A" (S "1") + s |> equal "S \"1\""