Skip to content

Commit

Permalink
Fix python char typing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrattli committed Nov 13, 2023
1 parent 37feeeb commit 520247a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 33 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
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
13 changes: 9 additions & 4 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 Expand Up @@ -254,7 +259,7 @@ let ``test Char addition works`` () =
'A' + 'B' |> int |> equal 131
'A' + char 7 |> int |> equal 72

// [<Fact>]
// let ``test Char subtraction works`` () =
// 'B' - 'A' |> int |> equal 1
// char 9 - char 7 |> int |> equal 2
[<Fact>]
let ``test Char subtraction works`` () =
'B' - 'A' |> int |> equal 1

Check failure on line 264 in tests/Python/TestChar.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

The type 'char' does not support the operator '-'

Check failure on line 264 in tests/Python/TestChar.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

The type 'char' does not support the operator '-'
char 9 - char 7 |> int |> equal 2

Check failure on line 265 in tests/Python/TestChar.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

The type 'char' does not support the operator '-'

Check failure on line 265 in tests/Python/TestChar.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

The type 'char' does not support the operator '-'

0 comments on commit 520247a

Please sign in to comment.