From d8624d7157382c53df86eac6579fc091980fdac7 Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Mon, 30 Oct 2023 19:10:07 +0100 Subject: [PATCH 1/7] Python typevar fixes --- src/Fable.Transforms/Python/Fable2Python.fs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/Fable.Transforms/Python/Fable2Python.fs b/src/Fable.Transforms/Python/Fable2Python.fs index 5113d7912e..edce31915e 100644 --- a/src/Fable.Transforms/Python/Fable2Python.fs +++ b/src/Fable.Transforms/Python/Fable2Python.fs @@ -579,9 +579,6 @@ module Annotation = com.GetImportExpr(ctx, "typing", "Generic") |> ignore - com.GetImportExpr(ctx, "typing", "TypeVar") - |> ignore - let genParams = genParams |> Set.toList @@ -695,7 +692,7 @@ module Annotation = |> Helpers.unzipArgs let typeAnnotation (com: IPythonCompiler) ctx (repeatedGenerics: Set option) (t: Fable.Type) : Expression * Statement list = - // printfn "typeAnnotation: %A" t + // printfn "typeAnnotation: %A" (t, repeatedGenerics) match t with | Fable.Measure _ | Fable.Any -> stdlibModuleTypeHint com ctx "typing" "Any" [] @@ -704,16 +701,10 @@ module Annotation = | Fable.GenericParam (name = name) -> match repeatedGenerics with | Some names when names.Contains name -> - com.GetImportExpr(ctx, "typing", "TypeVar") - |> ignore - let name = Helpers.clean name com.AddTypeVar(ctx, name), [] | Some _ -> stdlibModuleTypeHint com ctx "typing" "Any" [] | None -> - com.GetImportExpr(ctx, "typing", "TypeVar") - |> ignore - let name = Helpers.clean name com.AddTypeVar(ctx, name), [] | Fable.Unit -> Expression.none, [] @@ -726,7 +717,7 @@ module Annotation = stdlibModuleTypeHint com ctx "typing" "Callable" (argTypes @ [ returnType ]) | Fable.DelegateType (argTypes, returnType) -> stdlibModuleTypeHint com ctx "typing" "Callable" (argTypes @ [ returnType ]) | Fable.Option (genArg, _) -> - let resolved, stmts = resolveGenerics com ctx [genArg] None + let resolved, stmts = resolveGenerics com ctx [genArg] repeatedGenerics Expression.binOp(resolved[0], BitOr, Expression.none), stmts | Fable.Tuple (genArgs, _) -> makeGenericTypeAnnotation com ctx "tuple" genArgs None, [] | Fable.Array (genArg, _) -> @@ -1049,7 +1040,7 @@ module Util = |> List.map (fun arg -> let name = getUniqueNameInDeclarationScope ctx (arg.Name + "_mut") - let ta, _ = typeAnnotation com ctx None arg.Type + let ta, _ = typeAnnotation com ctx (Some (set [])) arg.Type Arg.arg (name, ta)) interface ITailCallOpportunity with @@ -3288,7 +3279,6 @@ module Util = BoundVars = ctx.BoundVars.EnterScope() ScopedTypeParams = Set.union ctx.ScopedTypeParams newTypeParams } - // printfn "Args: %A" args let body = if body.Type = Fable.Unit then transformBlock com ctx (Some ReturnUnit) body From 6c7d4aba031cbc249ade01960aa30856652ad5ce Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Mon, 30 Oct 2023 19:13:55 +0100 Subject: [PATCH 2/7] Update changelog --- src/Fable.Cli/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index c0136d6922..1d8f249327 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- [Python] Use `Any` type for all non-repeated generic types (by @dbrattli) +- [Python] Don't generate unnecessary type type-vars if generic type is replaced by `Any` (by @dbrattli) + ### Fixed #### JavaScript From 2d6de64efce6e3a40199265591aa8acecade031f Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Mon, 30 Oct 2023 19:17:37 +0100 Subject: [PATCH 3/7] Adjust text for changelog --- src/Fable.Cli/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index 1d8f249327..f0ea3b41ba 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -- [Python] Use `Any` type for all non-repeated generic types (by @dbrattli) +- [Python] Use `Any` type for all non-repeated generic arguments (by @dbrattli) - [Python] Don't generate unnecessary type type-vars if generic type is replaced by `Any` (by @dbrattli) ### Fixed From 591378b85a2fda3d3e56f788470d2ab66e76f8ad Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Mon, 30 Oct 2023 19:21:13 +0100 Subject: [PATCH 4/7] Simplify set creation --- src/Fable.Transforms/Python/Fable2Python.fs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Fable.Transforms/Python/Fable2Python.fs b/src/Fable.Transforms/Python/Fable2Python.fs index edce31915e..769f6b0734 100644 --- a/src/Fable.Transforms/Python/Fable2Python.fs +++ b/src/Fable.Transforms/Python/Fable2Python.fs @@ -1039,8 +1039,7 @@ module Util = discardUnitArg args |> List.map (fun arg -> let name = getUniqueNameInDeclarationScope ctx (arg.Name + "_mut") - - let ta, _ = typeAnnotation com ctx (Some (set [])) arg.Type + let ta, _ = typeAnnotation com ctx (Some Set.empty) arg.Type Arg.arg (name, ta)) interface ITailCallOpportunity with From 3ab7d520441a7dc388a5397323a8c741e61e8d86 Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Mon, 30 Oct 2023 19:23:05 +0100 Subject: [PATCH 5/7] Add comment for future self --- src/Fable.Transforms/Python/Fable2Python.fs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Fable.Transforms/Python/Fable2Python.fs b/src/Fable.Transforms/Python/Fable2Python.fs index 769f6b0734..aeb6ab2fb4 100644 --- a/src/Fable.Transforms/Python/Fable2Python.fs +++ b/src/Fable.Transforms/Python/Fable2Python.fs @@ -1039,6 +1039,7 @@ module Util = discardUnitArg args |> List.map (fun arg -> let name = getUniqueNameInDeclarationScope ctx (arg.Name + "_mut") + // Use empty repeated generics to avoid creating unnecssary typevars let ta, _ = typeAnnotation com ctx (Some Set.empty) arg.Type Arg.arg (name, ta)) From d0edbcedef4179a88634437893c3d8c215aa6289 Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Mon, 30 Oct 2023 21:00:55 +0100 Subject: [PATCH 6/7] More changelog for Python --- src/Fable.Cli/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index f0ea3b41ba..7b3b01734f 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [Python] Use `Any` type for all non-repeated generic arguments (by @dbrattli) - [Python] Don't generate unnecessary type type-vars if generic type is replaced by `Any` (by @dbrattli) +- [Python] Generate new style `_T | None` instead of `Optional[_T]` (by @dbrattli) ### Fixed From d43bec562aa18098d191a35c35b3b377fb151bbc Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Tue, 31 Oct 2023 07:13:50 +0100 Subject: [PATCH 7/7] Fix changelog --- src/Fable.Cli/CHANGELOG.md | 8 +++++--- src/Fable.Transforms/Python/Fable2Python.fs | 5 ++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index 7b3b01734f..c7b34044a0 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -6,9 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -- [Python] Use `Any` type for all non-repeated generic arguments (by @dbrattli) -- [Python] Don't generate unnecessary type type-vars if generic type is replaced by `Any` (by @dbrattli) -- [Python] Generate new style `_T | None` instead of `Optional[_T]` (by @dbrattli) +#### Python + +* Use `Any` type for all non-repeated generic arguments (by @dbrattli) +* Don't generate unnecessary type type-vars if generic type is replaced by `Any` (by @dbrattli) +* Generate new style `_T | None` instead of `Optional[_T]` (by @dbrattli) ### Fixed diff --git a/src/Fable.Transforms/Python/Fable2Python.fs b/src/Fable.Transforms/Python/Fable2Python.fs index aeb6ab2fb4..e93dc9c582 100644 --- a/src/Fable.Transforms/Python/Fable2Python.fs +++ b/src/Fable.Transforms/Python/Fable2Python.fs @@ -1039,9 +1039,8 @@ module Util = discardUnitArg args |> List.map (fun arg -> let name = getUniqueNameInDeclarationScope ctx (arg.Name + "_mut") - // Use empty repeated generics to avoid creating unnecssary typevars - let ta, _ = typeAnnotation com ctx (Some Set.empty) arg.Type - Arg.arg (name, ta)) + // Ignore type annotation here as it generates unnecessary typevars + Arg.arg name) interface ITailCallOpportunity with member _.Label = name