Skip to content
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
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [Python] Fix curry/uncurry to handle arbitrary number of arguments (by @dbrattli)
* [Python] Fix type annotations for protocols, Option casting, and abstract classes (by @dbrattli)
* [Python] Fix type annotations for curried functions and numeric types (by @dbrattli)
* [Python] Fix type annotations for inref, IList, DateKind, and regex collections (by @dbrattli)
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [Python] Fix curry/uncurry to handle arbitrary number of arguments (by @dbrattli)
* [Python] Fix type annotations for protocols, Option casting, and abstract classes (by @dbrattli)
* [Python] Fix type annotations for curried functions and numeric types (by @dbrattli)
* [Python] Fix type annotations for inref, IList, DateKind, and regex collections (by @dbrattli)
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Python/Fable2Python.Annotation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ let makeNumberTypeAnnotation com ctx kind info =
match name with
| "int"
| "float" -> Expression.name name
| _ -> fableModuleAnnotation com ctx "types" name []
| _ -> fableModuleAnnotation com ctx "core" name []


match kind, info with
Expand Down Expand Up @@ -631,7 +631,7 @@ let makeBuiltinTypeAnnotation com ctx typ repeatedGenerics kind =
| Replacements.Util.BclGuid -> stdlibModuleTypeHint com ctx "uuid" "UUID" [] repeatedGenerics
| Replacements.Util.FSharpReference genArg ->
let resolved, stmts = resolveGenerics com ctx [ genArg ] repeatedGenerics
fableModuleAnnotation com ctx "types" "FSharpRef" resolved, stmts
fableModuleAnnotation com ctx "core" "FSharpRef" resolved, stmts
(*
| Replacements.Util.BclTimeSpan -> NumberTypeAnnotation
| Replacements.Util.BclDateTime -> makeSimpleTypeAnnotation com ctx "Date"
Expand Down
28 changes: 14 additions & 14 deletions src/Fable.Transforms/Python/Fable2Python.Reflection.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let private libReflectionCall (com: IPythonCompiler) ctx r memberName args =

/// Wraps a Python list expression in Array(...) constructor
let private arrayExpr (com: IPythonCompiler) ctx (items: Expression list) =
Expression.call (libValue com ctx "types" "Array", [ Expression.list items ])
Expression.call (libValue com ctx "array_" "Array", [ Expression.list items ])

let private transformRecordReflectionInfo com ctx r (ent: Fable.Entity) generics =
// TODO: Refactor these three bindings to reuse in transformUnionReflectionInfo
Expand Down Expand Up @@ -318,18 +318,18 @@ let transformTypeTest (com: IPythonCompiler) ctx range expr (typ: Fable.Type) :
| Fable.String -> pyTypeof "<class 'str'>" expr
| Fable.Number(kind, _b) ->
match kind, typ with
| _, Fable.Type.Number(UInt8, _) -> pyInstanceof (libValue com ctx "types" "uint8") expr
| _, Fable.Type.Number(Int8, _) -> pyInstanceof (libValue com ctx "types" "int8") expr
| _, Fable.Type.Number(Int16, _) -> pyInstanceof (libValue com ctx "types" "int16") expr
| _, Fable.Type.Number(UInt16, _) -> pyInstanceof (libValue com ctx "types" "uint16") expr
| _, Fable.Type.Number(Int32, _) -> pyInstanceof (libValue com ctx "types" "int32") expr
| _, Fable.Type.Number(UInt32, _) -> pyInstanceof (libValue com ctx "types" "uint32") expr
| _, Fable.Type.Number(UInt8, _) -> pyInstanceof (libValue com ctx "core" "uint8") expr
| _, Fable.Type.Number(Int8, _) -> pyInstanceof (libValue com ctx "core" "int8") expr
| _, Fable.Type.Number(Int16, _) -> pyInstanceof (libValue com ctx "core" "int16") expr
| _, Fable.Type.Number(UInt16, _) -> pyInstanceof (libValue com ctx "core" "uint16") expr
| _, Fable.Type.Number(Int32, _) -> pyInstanceof (libValue com ctx "core" "int32") expr
| _, Fable.Type.Number(UInt32, _) -> pyInstanceof (libValue com ctx "core" "uint32") expr
| _, Fable.Type.Number(NativeInt, _)
| _, Fable.Type.Number(UNativeInt, _) -> pyInstanceof (Expression.name "int") expr
| _, Fable.Type.Number(Int64, _) -> pyInstanceof (libValue com ctx "types" "int64") expr
| _, Fable.Type.Number(UInt64, _) -> pyInstanceof (libValue com ctx "types" "uint64") expr
| _, Fable.Type.Number(Float32, _) -> pyInstanceof (libValue com ctx "types" "float32") expr
| _, Fable.Type.Number(Float64, _) -> pyInstanceof (libValue com ctx "types" "float64") expr
| _, Fable.Type.Number(Int64, _) -> pyInstanceof (libValue com ctx "core" "int64") expr
| _, Fable.Type.Number(UInt64, _) -> pyInstanceof (libValue com ctx "core" "uint64") expr
| _, Fable.Type.Number(Float32, _) -> pyInstanceof (libValue com ctx "core" "float32") expr
| _, Fable.Type.Number(Float64, _) -> pyInstanceof (libValue com ctx "core" "float64") expr
| _, Fable.Type.Number(Decimal, _) -> pyTypeof "<class 'decimal.Decimal'>" expr
| _ -> pyInstanceof (Expression.name "int") expr

Expand All @@ -338,7 +338,7 @@ let transformTypeTest (com: IPythonCompiler) ctx range expr (typ: Fable.Type) :
| Fable.DelegateType _ -> pyTypeof "<class 'function'>" expr
| Fable.Array _ ->
// Use isinstance(x, Array) where Array is from fable_library.types
pyInstanceof (libValue com ctx "types" "Array") expr
pyInstanceof (libValue com ctx "array_" "Array") expr
| Fable.Tuple _ ->
// Use isinstance(x, tuple) for Python tuple type test
pyInstanceof (Expression.name "tuple") expr
Expand Down Expand Up @@ -366,10 +366,10 @@ let transformTypeTest (com: IPythonCompiler) ctx range expr (typ: Fable.Type) :
[ expr ] |> libCall com ctx None "util" "isIterable", stmts
| Types.array ->
// Use isinstance(x, Array) where Array is from fable_library.types
pyInstanceof (libValue com ctx "types" "Array") expr
pyInstanceof (libValue com ctx "array_" "Array") expr
| Types.exception_ ->
let expr, stmts = com.TransformAsExpr(ctx, expr)
[ expr ] |> libCall com ctx None "types" "isException", stmts
[ expr ] |> libCall com ctx None "exceptions" "is_exception", stmts
| Types.datetime -> pyInstanceof (com.GetImportExpr(ctx, "datetime", "datetime")) expr
| _ ->
let ent = com.GetEntity(ent)
Expand Down
42 changes: 22 additions & 20 deletions src/Fable.Transforms/Python/Fable2Python.Transforms.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ let makeArrowFunctionExpression
let args =
match args.PosOnlyArgs, args.Args with
| [], [] ->
let ta = com.GetImportExpr(ctx, "typing", "Any")
let ta = com.GetImportExpr(ctx, getLibPath com "util", "Unit")

Arguments.arguments (args = [ Arg.arg ("__unit", annotation = ta) ], defaults = [ Expression.none ])
Arguments.arguments (args = [ Arg.arg ("__unit", annotation = ta) ], defaults = [ Expression.tuple [] ])
| _ -> args

let allDefaultsAreNone =
Expand Down Expand Up @@ -320,15 +320,15 @@ let transformCast (com: IPythonCompiler) (ctx: Context) t e : Expression * State

| _ -> com.TransformAsExpr(ctx, e)
| Fable.Number(Float32, _), _ ->
let cons = libValue com ctx "types" "float32"
let cons = libValue com ctx "core" "float32"
let value, stmts = com.TransformAsExpr(ctx, e)
Expression.call (cons, [ value ], ?loc = None), stmts
| Fable.Number(Float64, _), _ ->
let cons = libValue com ctx "types" "float64"
let cons = libValue com ctx "core" "float64"
let value, stmts = com.TransformAsExpr(ctx, e)
Expression.call (cons, [ value ], ?loc = None), stmts
| Fable.Number(Int32, _), _ ->
let cons = libValue com ctx "types" "int32"
let cons = libValue com ctx "core" "int32"
let value, stmts = com.TransformAsExpr(ctx, e)
Expression.call (cons, [ value ], ?loc = None), stmts
| _ -> com.TransformAsExpr(ctx, e)
Expand Down Expand Up @@ -393,9 +393,9 @@ let transformValue (com: IPythonCompiler) (ctx: Context) r value : Expression *
| Fable.NumberValue.Float64 x when x = -infinity -> libValue com ctx "double" "float64.negative_infinity", []
| Fable.NumberValue.Float64 x when Double.IsNaN(x) -> libValue com ctx "double" "float64.nan", []
| Fable.NumberValue.Float32 x when Single.IsNaN(x) ->
libCall com ctx r "types" "float32" [ Expression.stringConstant "nan" ], []
libCall com ctx r "core" "float32" [ Expression.stringConstant "nan" ], []
| Fable.NumberValue.Float16 x when Single.IsNaN(x) ->
libCall com ctx r "types" "float32" [ Expression.stringConstant "nan" ], []
libCall com ctx r "core" "float32" [ Expression.stringConstant "nan" ], []
| Fable.NumberValue.Float16 x -> makeFloat com ctx r value.Type "float32" (float x)
| Fable.NumberValue.Float32 x -> makeFloat com ctx r value.Type "float32" (float x)
| Fable.NumberValue.Float64 x -> makeFloat com ctx r value.Type "float64" (float x)
Expand Down Expand Up @@ -2977,21 +2977,23 @@ let transformFunction
args', finalDefaults, body

let arguments =
let unitDefault = libValue com ctx "util" "UNIT"

match args, isUnitOrGeneric, tcArgs with
// No args and no tail-call args: add __unit parameter
| [], _, [] ->
let unitDefault = libValue com ctx "util" "UNIT"
Arguments.arguments (args = [ Arg.arg (Identifier("__unit")) ], defaults = [ unitDefault ])
let unitType = com.GetImportExpr(ctx, getLibPath com "util", "Unit")

Arguments.arguments (
args = [ Arg.arg (Identifier("__unit"), annotation = unitType) ],
defaults = [ unitDefault ]
)
// No args but has tail-call args: skip __unit, tcArgs are sufficient
| [], _, _ -> Arguments.arguments (args = tcArgs, defaults = tcDefaults)
// Single generic/unit arg with no tail-call args: keep it with UNIT default
| [ arg ], true, [] ->
let unitDefault = libValue com ctx "util" "UNIT"
Arguments.arguments (args = args, defaults = [ unitDefault ])
// Single generic/unit arg with no tail-call args: keep it with () default
| [ arg ], true, [] -> Arguments.arguments (args = args, defaults = [ unitDefault ])
// Single generic/unit arg with tail-call args: keep arg (body may reference it)
| [ arg ], true, _ ->
let unitDefault = libValue com ctx "util" "UNIT"
Arguments.arguments (args @ tcArgs, defaults = unitDefault :: tcDefaults)
| [ arg ], true, _ -> Arguments.arguments (args @ tcArgs, defaults = unitDefault :: tcDefaults)
| _ -> Arguments.arguments (args @ tcArgs, defaults = defaults @ tcDefaults)

arguments, body
Expand Down Expand Up @@ -3828,7 +3830,7 @@ let transformUnion (com: IPythonCompiler) ctx (ent: Fable.Entity) (entName: stri
decoratorList = decorators
)

let baseExpr = libValue com ctx "types" "Union" |> Some
let baseExpr = libValue com ctx "union" "Union" |> Some
let classMembers = List.append [ cases ] classMembers

declareType com ctx ent entName args isOptional body baseExpr classMembers None [] []
Expand All @@ -3853,9 +3855,9 @@ let transformClassWithCompilerGeneratedConstructor

let baseExpr =
if ent.IsFSharpExceptionDeclaration then
libValue com ctx "types" "FSharpException" |> Some
libValue com ctx "exceptions" "FSharpException" |> Some
elif ent.IsFSharpRecord || ent.IsValueType then
libValue com ctx "types" "Record" |> Some
libValue com ctx "record" "Record" |> Some
else
None

Expand Down Expand Up @@ -4055,7 +4057,7 @@ let transformClassWithPrimaryConstructor
|> extractBaseExprFromBaseCall com ctx classEnt.BaseType
|> Option.orElseWith (fun () ->
if classEnt.IsValueType then
Some(libValue com ctx "Types" "Record", ([], [], []))
Some(libValue com ctx "record" "Record", ([], [], []))
else
None
)
Expand Down
8 changes: 4 additions & 4 deletions src/Fable.Transforms/Python/Fable2Python.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ module Util =

let ofInt (com: IPythonCompiler) (ctx: Context) (i: int) =
//Expression.intConstant (int i)
libCall com ctx None "types" "int32" [ Expression.intConstant (int i) ]
libCall com ctx None "core" "int32" [ Expression.intConstant (int i) ]

let ofString (s: string) = Expression.stringConstant s

Expand Down Expand Up @@ -560,7 +560,7 @@ module Util =
if l = "Any" then
com.GetImportExpr(ctx, "typing", "Any")
else
libValue com ctx "types" l
libValue com ctx "core" l

let types_array = Expression.subscript (value = array, slice = type_obj, ctx = Load)
Expression.call (types_array, [ expr ])
Expand Down Expand Up @@ -803,7 +803,7 @@ module Util =
Expression.attribute (expr, Identifier field, ctx = Load), []

let makeInteger (com: IPythonCompiler) (ctx: Context) r _t intName (x: obj) =
let cons = libValue com ctx "types" intName
let cons = libValue com ctx "core" intName
let value = Expression.intConstant (x, ?loc = r)

// Added support for a few selected literals for performance reasons
Expand Down Expand Up @@ -927,7 +927,7 @@ module Util =
| _ -> Expression.call (cons, [ value ], ?loc = r), []

let makeFloat (com: IPythonCompiler) (ctx: Context) r _t floatName x =
let cons = libValue com ctx "types" floatName
let cons = libValue com ctx "core" floatName
let value = Expression.floatConstant (x, ?loc = r)
Expression.call (cons, [ value ], ?loc = r), []

Expand Down
Loading
Loading