From 53fda11730817e08cdf252ac71dbe771dcd8d770 Mon Sep 17 00:00:00 2001 From: Maxime Mangel Date: Tue, 17 Dec 2024 16:57:22 +0100 Subject: [PATCH] [Python] Fix type testing against `uint8`, `uint32`, `uint64`, `decimal` Fix #3971 --- src/Fable.Cli/CHANGELOG.md | 4 + src/Fable.Transforms/Python/Fable2Python.fs | 7 +- tests/Python/TestType.fs | 120 ++++++++++++++++++-- 3 files changed, 118 insertions(+), 13 deletions(-) diff --git a/src/Fable.Cli/CHANGELOG.md b/src/Fable.Cli/CHANGELOG.md index 4e8a82725..e4d319fae 100644 --- a/src/Fable.Cli/CHANGELOG.md +++ b/src/Fable.Cli/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed + +* [Python] Fix type testing against `uint8`, `uint32`, `uint64`, `decimal` (@MangelMaxime) + ## 5.0.0-alpha.2 - 2024-11-25 ### Fixed diff --git a/src/Fable.Transforms/Python/Fable2Python.fs b/src/Fable.Transforms/Python/Fable2Python.fs index ad18342ec..742eb4a20 100644 --- a/src/Fable.Transforms/Python/Fable2Python.fs +++ b/src/Fable.Transforms/Python/Fable2Python.fs @@ -440,16 +440,17 @@ module Reflection = | Fable.String -> pyTypeof "" expr | Fable.Number(kind, _b) -> match kind, typ with - | _, Fable.Type.Number(UInt8, _) -> pyTypeof ">" expr | _, Fable.Type.Number(Int8, _) -> pyTypeof "" expr + | _, Fable.Type.Number(UInt8, _) -> pyTypeof "" expr | _, Fable.Type.Number(Int16, _) -> pyTypeof "" expr | _, Fable.Type.Number(UInt16, _) -> pyTypeof "" expr | _, Fable.Type.Number(Int32, _) -> pyTypeof "" expr - | _, Fable.Type.Number(UInt32, _) -> pyTypeof "" expr + | _, Fable.Type.Number(UInt32, _) -> pyTypeof "" expr | _, Fable.Type.Number(Int64, _) -> pyTypeof "" expr - | _, Fable.Type.Number(UInt64, _) -> pyTypeof "" expr + | _, Fable.Type.Number(UInt64, _) -> pyTypeof "" expr | _, Fable.Type.Number(Float32, _) -> pyTypeof "" expr | _, Fable.Type.Number(Float64, _) -> pyTypeof "" expr + | _, Fable.Type.Number(Decimal, _) -> pyTypeof "" expr | _ -> pyTypeof "" expr | Fable.Regex -> pyInstanceof (com.GetImportExpr(ctx, "typing", "Pattern")) expr diff --git a/tests/Python/TestType.fs b/tests/Python/TestType.fs index 716e2f393..e4d7106d4 100644 --- a/tests/Python/TestType.fs +++ b/tests/Python/TestType.fs @@ -869,16 +869,6 @@ let ``test Type test with Date`` () = DateTime.Now |> box |> isDate |> equal true box 5 |> isDate |> equal false -[] -let ``test Type test with Long`` () = - let isLong (x: obj) = - match x with - | :? int64 -> true - | _ -> false - - box 5L |> isLong |> equal true -//box 50 |> isLong |> equal false - [] let ``test Type test with BigInt`` () = let isBigInd (x: obj) = @@ -1559,3 +1549,113 @@ let ``test Choice with arity 3+ is represented correctly`` () = // See #2485 let ``test Can call the base version of a mangled abstract method that was declared above in the hierarchy`` () = let c = ConcreteClass1() c.MyMethod(4) |> equal 58 + +[] +let ``test Type test uint8`` () = + let isUInt8 (x: obj) = + match x with + | :? byte -> true + | _ -> false + + box 5uy |> isUInt8 |> equal true + box 5 |> isUInt8 |> equal false + +[] +let ``test Type test int8`` () = + let isInt8 (x: obj) = + match x with + | :? sbyte -> true + | _ -> false + + box 5y |> isInt8 |> equal true + box 5 |> isInt8 |> equal false + +[] +let ``test Type test uint16`` () = + let isUInt16 (x: obj) = + match x with + | :? uint16 -> true + | _ -> false + + box 5us |> isUInt16 |> equal true + box 5 |> isUInt16 |> equal false + +[] +let ``test Type test int16`` () = + let isInt16 (x: obj) = + match x with + | :? int16 -> true + | _ -> false + + box 5s |> isInt16 |> equal true + box 5 |> isInt16 |> equal false + +[] +let ``test Type test uint32`` () = + let isUInt32 (x: obj) = + match x with + | :? uint32 -> true + | _ -> false + + box 5u |> isUInt32 |> equal true + box 5 |> isUInt32 |> equal false + +[] +let ``test Type test int32`` () = + let isInt32 (x: obj) = + match x with + | :? int32 -> true + | _ -> false + + box 5 |> isInt32 |> equal true + box 5L |> isInt32 |> equal false + +[] +let ``test Type test uint64`` () = + let isUInt64 (x: obj) = + match x with + | :? uint64 -> true + | _ -> false + + box 5UL |> isUInt64 |> equal true + box 5 |> isUInt64 |> equal false + +[] +let ``test Type test int64`` () = + let isInt64 (x: obj) = + match x with + | :? int64 -> true + | _ -> false + + box 5L |> isInt64 |> equal true + box 5 |> isInt64 |> equal false + +[] +let ``test Type test float32`` () = + let isFloat32 (x: obj) = + match x with + | :? float32 -> true + | _ -> false + + box 5.f |> isFloat32 |> equal true + box 5. |> isFloat32 |> equal false + +[] +let ``test Type test float64`` () = + let isFloat64 (x: obj) = + match x with + | :? float -> true + | _ -> false + + box 5. |> isFloat64 |> equal true + box 5.f |> isFloat64 |> equal false + +[] +let ``test Type test decimal`` () = + let isDecimal (x: obj) = + match x with + | :? decimal -> true + | _ -> false + + box 5M |> isDecimal |> equal true + box 5 |> isDecimal |> equal false