Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Generic typing fixes #3577

Merged
merged 7 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading