Skip to content

Commit

Permalink
[Python] Fix type testing against uint8, uint32, uint64, decimal
Browse files Browse the repository at this point in the history
Fix #3971
  • Loading branch information
MangelMaxime committed Dec 17, 2024
1 parent 6498a3d commit 53fda11
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -440,16 +440,17 @@ module Reflection =
| Fable.String -> pyTypeof "<class 'str'>" expr
| Fable.Number(kind, _b) ->
match kind, typ with
| _, Fable.Type.Number(UInt8, _) -> pyTypeof "<fable_modules.fable_library.types.uint8'>>" expr
| _, Fable.Type.Number(Int8, _) -> pyTypeof "<class 'fable_modules.fable_library.types.int8'>" expr
| _, Fable.Type.Number(UInt8, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint8'>" expr
| _, Fable.Type.Number(Int16, _) -> pyTypeof "<class 'fable_modules.fable_library.types.int16'>" expr
| _, Fable.Type.Number(UInt16, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint16'>" expr
| _, Fable.Type.Number(Int32, _) -> pyTypeof "<class 'int'>" expr
| _, Fable.Type.Number(UInt32, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint32>" expr
| _, Fable.Type.Number(UInt32, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint32'>" expr
| _, Fable.Type.Number(Int64, _) -> pyTypeof "<class 'fable_modules.fable_library.types.int64'>" expr
| _, Fable.Type.Number(UInt64, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint32'>" expr
| _, Fable.Type.Number(UInt64, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint64'>" expr
| _, Fable.Type.Number(Float32, _) -> pyTypeof "<class 'fable_modules.fable_library.types.float32'>" expr
| _, Fable.Type.Number(Float64, _) -> pyTypeof "<class 'float'>" expr
| _, Fable.Type.Number(Decimal, _) -> pyTypeof "<class 'decimal.Decimal'>" expr
| _ -> pyTypeof "<class 'int'>" expr

| Fable.Regex -> pyInstanceof (com.GetImportExpr(ctx, "typing", "Pattern")) expr
Expand Down
120 changes: 110 additions & 10 deletions tests/Python/TestType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -869,16 +869,6 @@ let ``test Type test with Date`` () =
DateTime.Now |> box |> isDate |> equal true
box 5 |> isDate |> equal false

[<Fact>]
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

[<Fact>]
let ``test Type test with BigInt`` () =
let isBigInd (x: obj) =
Expand Down Expand Up @@ -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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

0 comments on commit 53fda11

Please sign in to comment.