Skip to content

Commit 53fda11

Browse files
committed
[Python] Fix type testing against uint8, uint32, uint64, decimal
Fix #3971
1 parent 6498a3d commit 53fda11

File tree

3 files changed

+118
-13
lines changed

3 files changed

+118
-13
lines changed

src/Fable.Cli/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Fixed
11+
12+
* [Python] Fix type testing against `uint8`, `uint32`, `uint64`, `decimal` (@MangelMaxime)
13+
1014
## 5.0.0-alpha.2 - 2024-11-25
1115

1216
### Fixed

src/Fable.Transforms/Python/Fable2Python.fs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,16 +440,17 @@ module Reflection =
440440
| Fable.String -> pyTypeof "<class 'str'>" expr
441441
| Fable.Number(kind, _b) ->
442442
match kind, typ with
443-
| _, Fable.Type.Number(UInt8, _) -> pyTypeof "<fable_modules.fable_library.types.uint8'>>" expr
444443
| _, Fable.Type.Number(Int8, _) -> pyTypeof "<class 'fable_modules.fable_library.types.int8'>" expr
444+
| _, Fable.Type.Number(UInt8, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint8'>" expr
445445
| _, Fable.Type.Number(Int16, _) -> pyTypeof "<class 'fable_modules.fable_library.types.int16'>" expr
446446
| _, Fable.Type.Number(UInt16, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint16'>" expr
447447
| _, Fable.Type.Number(Int32, _) -> pyTypeof "<class 'int'>" expr
448-
| _, Fable.Type.Number(UInt32, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint32>" expr
448+
| _, Fable.Type.Number(UInt32, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint32'>" expr
449449
| _, Fable.Type.Number(Int64, _) -> pyTypeof "<class 'fable_modules.fable_library.types.int64'>" expr
450-
| _, Fable.Type.Number(UInt64, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint32'>" expr
450+
| _, Fable.Type.Number(UInt64, _) -> pyTypeof "<class 'fable_modules.fable_library.types.uint64'>" expr
451451
| _, Fable.Type.Number(Float32, _) -> pyTypeof "<class 'fable_modules.fable_library.types.float32'>" expr
452452
| _, Fable.Type.Number(Float64, _) -> pyTypeof "<class 'float'>" expr
453+
| _, Fable.Type.Number(Decimal, _) -> pyTypeof "<class 'decimal.Decimal'>" expr
453454
| _ -> pyTypeof "<class 'int'>" expr
454455

455456
| Fable.Regex -> pyInstanceof (com.GetImportExpr(ctx, "typing", "Pattern")) expr

tests/Python/TestType.fs

Lines changed: 110 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -869,16 +869,6 @@ let ``test Type test with Date`` () =
869869
DateTime.Now |> box |> isDate |> equal true
870870
box 5 |> isDate |> equal false
871871

872-
[<Fact>]
873-
let ``test Type test with Long`` () =
874-
let isLong (x: obj) =
875-
match x with
876-
| :? int64 -> true
877-
| _ -> false
878-
879-
box 5L |> isLong |> equal true
880-
//box 50 |> isLong |> equal false
881-
882872
[<Fact>]
883873
let ``test Type test with BigInt`` () =
884874
let isBigInd (x: obj) =
@@ -1559,3 +1549,113 @@ let ``test Choice with arity 3+ is represented correctly`` () = // See #2485
15591549
let ``test Can call the base version of a mangled abstract method that was declared above in the hierarchy`` () =
15601550
let c = ConcreteClass1()
15611551
c.MyMethod(4) |> equal 58
1552+
1553+
[<Fact>]
1554+
let ``test Type test uint8`` () =
1555+
let isUInt8 (x: obj) =
1556+
match x with
1557+
| :? byte -> true
1558+
| _ -> false
1559+
1560+
box 5uy |> isUInt8 |> equal true
1561+
box 5 |> isUInt8 |> equal false
1562+
1563+
[<Fact>]
1564+
let ``test Type test int8`` () =
1565+
let isInt8 (x: obj) =
1566+
match x with
1567+
| :? sbyte -> true
1568+
| _ -> false
1569+
1570+
box 5y |> isInt8 |> equal true
1571+
box 5 |> isInt8 |> equal false
1572+
1573+
[<Fact>]
1574+
let ``test Type test uint16`` () =
1575+
let isUInt16 (x: obj) =
1576+
match x with
1577+
| :? uint16 -> true
1578+
| _ -> false
1579+
1580+
box 5us |> isUInt16 |> equal true
1581+
box 5 |> isUInt16 |> equal false
1582+
1583+
[<Fact>]
1584+
let ``test Type test int16`` () =
1585+
let isInt16 (x: obj) =
1586+
match x with
1587+
| :? int16 -> true
1588+
| _ -> false
1589+
1590+
box 5s |> isInt16 |> equal true
1591+
box 5 |> isInt16 |> equal false
1592+
1593+
[<Fact>]
1594+
let ``test Type test uint32`` () =
1595+
let isUInt32 (x: obj) =
1596+
match x with
1597+
| :? uint32 -> true
1598+
| _ -> false
1599+
1600+
box 5u |> isUInt32 |> equal true
1601+
box 5 |> isUInt32 |> equal false
1602+
1603+
[<Fact>]
1604+
let ``test Type test int32`` () =
1605+
let isInt32 (x: obj) =
1606+
match x with
1607+
| :? int32 -> true
1608+
| _ -> false
1609+
1610+
box 5 |> isInt32 |> equal true
1611+
box 5L |> isInt32 |> equal false
1612+
1613+
[<Fact>]
1614+
let ``test Type test uint64`` () =
1615+
let isUInt64 (x: obj) =
1616+
match x with
1617+
| :? uint64 -> true
1618+
| _ -> false
1619+
1620+
box 5UL |> isUInt64 |> equal true
1621+
box 5 |> isUInt64 |> equal false
1622+
1623+
[<Fact>]
1624+
let ``test Type test int64`` () =
1625+
let isInt64 (x: obj) =
1626+
match x with
1627+
| :? int64 -> true
1628+
| _ -> false
1629+
1630+
box 5L |> isInt64 |> equal true
1631+
box 5 |> isInt64 |> equal false
1632+
1633+
[<Fact>]
1634+
let ``test Type test float32`` () =
1635+
let isFloat32 (x: obj) =
1636+
match x with
1637+
| :? float32 -> true
1638+
| _ -> false
1639+
1640+
box 5.f |> isFloat32 |> equal true
1641+
box 5. |> isFloat32 |> equal false
1642+
1643+
[<Fact>]
1644+
let ``test Type test float64`` () =
1645+
let isFloat64 (x: obj) =
1646+
match x with
1647+
| :? float -> true
1648+
| _ -> false
1649+
1650+
box 5. |> isFloat64 |> equal true
1651+
box 5.f |> isFloat64 |> equal false
1652+
1653+
[<Fact>]
1654+
let ``test Type test decimal`` () =
1655+
let isDecimal (x: obj) =
1656+
match x with
1657+
| :? decimal -> true
1658+
| _ -> false
1659+
1660+
box 5M |> isDecimal |> equal true
1661+
box 5 |> isDecimal |> equal false

0 commit comments

Comments
 (0)