Skip to content

Commit

Permalink
[Python] Generic typing fixes (#3577)
Browse files Browse the repository at this point in the history
- Use Any type for all non-repeated generic parameters
- Don't generate unnecessary type type-vars if generic type is replaced by Any
- Generate new style _T | None instead of Optional[_T]
  • Loading branch information
dbrattli authored Oct 31, 2023
1 parent 25f6bf6 commit 5d309a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ 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)
* 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

#### JavaScript
Expand Down
19 changes: 4 additions & 15 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,6 @@ module Annotation =
com.GetImportExpr(ctx, "typing", "Generic")
|> ignore

com.GetImportExpr(ctx, "typing", "TypeVar")
|> ignore

let genParams =
genParams
|> Set.toList
Expand Down Expand Up @@ -695,7 +692,7 @@ module Annotation =
|> Helpers.unzipArgs

let typeAnnotation (com: IPythonCompiler) ctx (repeatedGenerics: Set<string> 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" []
Expand All @@ -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, []
Expand All @@ -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, _) ->
Expand Down Expand Up @@ -1048,9 +1039,8 @@ module Util =
discardUnitArg args
|> List.map (fun arg ->
let name = getUniqueNameInDeclarationScope ctx (arg.Name + "_mut")

let ta, _ = typeAnnotation com ctx None arg.Type
Arg.arg (name, ta))
// Ignore type annotation here as it generates unnecessary typevars
Arg.arg name)

interface ITailCallOpportunity with
member _.Label = name
Expand Down Expand Up @@ -3288,7 +3278,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
Expand Down

0 comments on commit 5d309a0

Please sign in to comment.