Skip to content

Commit 4642fef

Browse files
authored
[Python] Fix missing await on else branch of ternary in async closures (#4445)
1 parent 0d29556 commit 4642fef

4 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
* [Python] Fix missing `await` on else branch of ternary expressions in async closures (by @dbrattli)
1213
* [Beam] Fix `|> ignore` on cross-module Emit calls generating variable bindings that shadow Emit case-clause variables (by @dbrattli)
1314
* [Beam] Fix `containsIdentRef` not checking `Call` ThisArg (by @dbrattli)
1415
* [All] Fix CLI color not resetting after error messages (fixes #3755) (by @MangelMaxime)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
* [Python] Fix missing `await` on else branch of ternary expressions in async closures (by @dbrattli)
1213
* [Beam] Fix `|> ignore` on cross-module Emit calls generating variable bindings that shadow Emit case-clause variables (by @dbrattli)
1314
* [Beam] Fix `containsIdentRef` not checking `Call` ThisArg (by @dbrattli)
1415
* [JS/TS] `StringEnum` now respect `CompiledValue` and `CompiledName` (by @shayanhabibi)

src/Fable.Transforms/Python/PythonPrinter.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ module PrinterExtensions =
816816
| Name ex -> printer.Print(ex)
817817
| Await ex ->
818818
printer.Print("await ")
819-
printer.Print(ex)
819+
printer.ComplexExpressionWithParens(ex)
820820
| Yield expr ->
821821
printer.Print("yield")
822822

tests/Python/TestTask.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,23 @@ let ``test async returns in try-with are awaited`` () =
199199

200200
let result2 = wrapper true |> fun tsk -> tsk.GetAwaiter().GetResult()
201201
equal -1 result2
202+
203+
// Regression: closure with let binding before if/else returning Task emits ternary;
204+
// both branches of the ternary must be awaited.
205+
let asyncNone () : Task<int option> = Task.FromResult None
206+
207+
let makeClosure (flag: bool) =
208+
let captured = flag
209+
fun (value: int) ->
210+
let x = value + 1
211+
if captured then Task.FromResult(Some x) else asyncNone ()
212+
213+
[<Fact>]
214+
let ``test async ternary in closure awaits both branches`` () =
215+
let fn = makeClosure true
216+
let result = fn 41 |> fun tsk -> tsk.GetAwaiter().GetResult()
217+
equal (Some 42) result
218+
219+
let fn2 = makeClosure false
220+
let result2 = fn2 41 |> fun tsk -> tsk.GetAwaiter().GetResult()
221+
equal None result2

0 commit comments

Comments
 (0)