Skip to content

Commit

Permalink
[Python] Fix char to string regression and add tests (fable-compiler#…
Browse files Browse the repository at this point in the history
…3588)

* Fix python char typing issue
  • Loading branch information
dbrattli authored Nov 14, 2023
1 parent 37feeeb commit 847ff00
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 29 deletions.
18 changes: 1 addition & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ homepage = "https://fable.io"

[tool.poetry.dependencies]
python = ">= 3.10, < 4.0"
fable-library = "*"
python-dateutil = "^2.8.2"

[tool.poetry.dev-dependencies]
fable-library = {path = "./temp/fable-library-py", develop = true}
pytest = "^6.2.4"
ruff = "^0.1.3"

Expand Down
4 changes: 4 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Added `Guid.TryParse`, `Guid.ToByteArray` (by @ncave)

#### Python

* Fixed char to string type regression with binary operator (by @dbrattli)

## 4.5.0 - 2023-11-07

### Changed
Expand Down
16 changes: 7 additions & 9 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ let toString com (ctx: Context) r (args: Expr list) =
|> addErrorAndReturnNull com ctx.InlinePath r
| head :: tail ->
match head.Type with
| Char
| Char -> TypeCast(head, String)
| String -> head
| Builtin BclGuid when tail.IsEmpty -> Helper.GlobalCall("str", String, [ head ], ?loc = r)
| Builtin (BclGuid
Expand Down Expand Up @@ -466,16 +466,14 @@ let applyOp (com: ICompiler) (ctx: Context) r t opName (args: Expr list) =
| CustomOp com ctx r t opName args e -> e
| _ -> nativeOp opName argTypes args

let isCompatibleWithNativeComparison =
function
| Builtin (BclGuid
| BclTimeSpan)
| Boolean
| Number _
| String
| Char -> true
let isCompatibleWithNativeComparison = function
| Builtin (BclGuid|BclTimeSpan|BclTimeOnly)
| Boolean | Char | String | Number _ -> true
// TODO: Non-record/union declared types without custom equality
// should be compatible with Py comparison
| _ -> false


// Overview of hash rules:
// * `hash`, `Unchecked.hash` first check if GetHashCode is implemented and then default to structural hash.
// * `.GetHashCode` called directly defaults to identity hash (for reference types except string) if not implemented.
Expand Down
2 changes: 1 addition & 1 deletion src/fable-library-py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fable-library"
version = "0.8.0"
version = "0.9.0"
description = "Fable library for Python"
authors = ["Dag Brattli <dag@brattli.net>"]
license = "MIT License"
Expand Down
1 change: 1 addition & 0 deletions tests/Python/Fable.Tests.Python.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<Compile Include="TestAsync.fs" />
<Compile Include="TestApplicative.fs" />
<Compile Include="TestArithmetic.fs" />
<Compile Include="TestAnonRecords.fs" />
<Compile Include="TestArray.fs" />
<Compile Include="TestChar.fs" />
<Compile Include="TestComparison.fs" />
Expand Down
59 changes: 59 additions & 0 deletions tests/Python/TestAnonRecords.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module Fable.Tests.AnonRecordTests

open Util.Testing

let anonRecAcceptingFn (x: {|A: int; B: string|}) =
{|C = x.A + 1; D = "Z"|}

let structAnonRecAcceptingFn (x: struct {|A: int; B: string|}) =
struct {|C = x.A + 1; D = "Z"|}

[<Fact>]
let ``test Anonimous records work`` () =
let r = {| A = 1; B = "hello"; X = 3.141; D = 4|}
r.A |> equal 1
r.X |> equal 3.141 //just in case alphabetical ordering is going to throw off index
r.B |> equal "hello"
r.D |> equal 4

[<Fact>]
let ``test Anonimous records can pe passed as parameters`` () =
let m = {| A = 1; B = "hello"|}
let res = anonRecAcceptingFn m
res.C |> equal 2
res.D |> equal "Z"

[<Fact>]
let ``test Anonimous records structural equality works`` () =
let a = {| A = 1; B = "hello"|}
let b = {| A = 1; B = "hello"|}
let c = {| A = 2; B = "test"|}
a = a |> equal true
a = b |> equal true
a = c |> equal false
b = c |> equal false

[<Fact>]
let ``test Struct anonimous records work`` () =
let r = struct {| A = 1; B = "hello"; X = 3.141; D = 4|}
r.A |> equal 1
r.X |> equal 3.141 //just in case alphabetical ordering is going to throw off index
r.B |> equal "hello"
r.D |> equal 4

[<Fact>]
let ``test Struct anonimous records can pe passed as parameters`` () =
let m = struct {| A = 1; B = "hello"|}
let res = structAnonRecAcceptingFn m
res.C |> equal 2
res.D |> equal "Z"

[<Fact>]
let ``test Struct anonimous records structural equality works`` () =
let a = struct {| A = 1; B = "hello"|}
let b = struct {| A = 1; B = "hello"|}
let c = struct {| A = 2; B = "test"|}
a = a |> equal true
a = b |> equal true
a = c |> equal false
b = c |> equal false
5 changes: 5 additions & 0 deletions tests/Python/TestChar.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ let ``test Char.ToLowerInvariant works`` () =
let ``test Char.ToString works`` () =
Char.ToString('b') |> equal "b"

[<Fact>]
let ``test Char.ToString type is casted correctly`` () =
('t'.ToString()) + "est" |> equal "test"


[<Fact>]
let ``test Char.GetUnicodeCategory works`` () =
Char.GetUnicodeCategory('a') |> equal UnicodeCategory.LowercaseLetter
Expand Down

0 comments on commit 847ff00

Please sign in to comment.