Skip to content

Commit

Permalink
[Python] Fix quotation of union string cases (#3819)
Browse files Browse the repository at this point in the history
* Fix quotation of union string cases

* Fix fix single case
  • Loading branch information
dbrattli authored May 20, 2024
1 parent 1d50541 commit e41f2ec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 9 additions & 6 deletions src/fable-library-py/fable_library/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def __init__(self):

@staticmethod
@abstractmethod
def cases() -> list[str]:
...
def cases() -> list[str]: ...

@property
def name(self) -> str:
Expand All @@ -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 "")

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions tests/Python/TestUnionType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
[<Fact>]
let ``test sprintf formats strings cases correctly`` () =
let s = sprintf "%A" (S "1")
s |> equal "S \"1\""

0 comments on commit e41f2ec

Please sign in to comment.