diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index 8ddd8b64ea..63179d32d1 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +* [Python] Fix missing `await` on else branch of ternary expressions in async closures (by @dbrattli) * [Beam] Fix `|> ignore` on cross-module Emit calls generating variable bindings that shadow Emit case-clause variables (by @dbrattli) * [Beam] Fix `containsIdentRef` not checking `Call` ThisArg (by @dbrattli) * [All] Fix CLI color not resetting after error messages (fixes #3755) (by @MangelMaxime) diff --git a/src/Fable.Compiler/CHANGELOG.md b/src/Fable.Compiler/CHANGELOG.md index c54085d64c..58b9d6bd46 100644 --- a/src/Fable.Compiler/CHANGELOG.md +++ b/src/Fable.Compiler/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +* [Python] Fix missing `await` on else branch of ternary expressions in async closures (by @dbrattli) * [Beam] Fix `|> ignore` on cross-module Emit calls generating variable bindings that shadow Emit case-clause variables (by @dbrattli) * [Beam] Fix `containsIdentRef` not checking `Call` ThisArg (by @dbrattli) * [JS/TS] `StringEnum` now respect `CompiledValue` and `CompiledName` (by @shayanhabibi) diff --git a/src/Fable.Transforms/Python/PythonPrinter.fs b/src/Fable.Transforms/Python/PythonPrinter.fs index d4270cf909..aa89f08ed4 100644 --- a/src/Fable.Transforms/Python/PythonPrinter.fs +++ b/src/Fable.Transforms/Python/PythonPrinter.fs @@ -816,7 +816,7 @@ module PrinterExtensions = | Name ex -> printer.Print(ex) | Await ex -> printer.Print("await ") - printer.Print(ex) + printer.ComplexExpressionWithParens(ex) | Yield expr -> printer.Print("yield") diff --git a/tests/Python/TestTask.fs b/tests/Python/TestTask.fs index 06ab90294d..24fe18e192 100644 --- a/tests/Python/TestTask.fs +++ b/tests/Python/TestTask.fs @@ -199,3 +199,23 @@ let ``test async returns in try-with are awaited`` () = let result2 = wrapper true |> fun tsk -> tsk.GetAwaiter().GetResult() equal -1 result2 + +// Regression: closure with let binding before if/else returning Task emits ternary; +// both branches of the ternary must be awaited. +let asyncNone () : Task = Task.FromResult None + +let makeClosure (flag: bool) = + let captured = flag + fun (value: int) -> + let x = value + 1 + if captured then Task.FromResult(Some x) else asyncNone () + +[] +let ``test async ternary in closure awaits both branches`` () = + let fn = makeClosure true + let result = fn 41 |> fun tsk -> tsk.GetAwaiter().GetResult() + equal (Some 42) result + + let fn2 = makeClosure false + let result2 = fn2 41 |> fun tsk -> tsk.GetAwaiter().GetResult() + equal None result2