Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparaison between optional list when one is None #3618

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

#### Python

* Fix #3617: Fix comparaison between list option when one is None

## 4.6.0 - 2023-11-27

### Changed
Expand Down
21 changes: 14 additions & 7 deletions src/fable-library-py/fable_library/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,20 @@ class DateKind(IntEnum):


def equals(a: Any, b: Any) -> bool:
if a is b:
return True

if is_array_like(a):
return equal_arrays(a, b)

return a == b
match (a, b):
case (a, b) if a is b:
return True
# Don't test (None, None) here, because a is b already covers that
MangelMaxime marked this conversation as resolved.
Show resolved Hide resolved
# case (None, None):
# return True
case (None, _):
return False
case (_, None):
return False
case (a, b) if is_array_like(a):
return equal_arrays(a, b)
case _:
return a == b


def is_comparable(x: Any) -> bool:
Expand Down
107 changes: 107 additions & 0 deletions tests/Python/TestComparison.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ let ``test Typed array equality works`` () =
equal false (xs1 <> xs2)
equal true (xs1 <> xs4)

[<Fact>]
let ``test Typed array option equality works`` () =
let xs1 = Some [| 1; 2; 3 |]
let xs2 = Some [| 1; 2; 3 |]
let xs3 = Some [| 1; 2; 4 |]
let xs4 = Some [| 1; 2 |]
let xs5 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal false (xs1 = xs4)
equal true (xs1 <> xs4)
equal false (xs1 = xs5)
equal true (xs1 <> xs5)
equal true (xs5 = None)
equal false (xs5 <> None)

[<Fact>]
let ``test Array equality works`` () =
let xs1 = [| "1"; "2"; "3" |]
Expand All @@ -93,6 +111,24 @@ let ``test Array equality works`` () =
equal false (xs1 <> xs2)
equal true (xs1 <> xs4)

[<Fact>]
let ``test Array option equality works`` () =
let xs1 = Some [| "1"; "2"; "3" |]
let xs2 = Some [| "1"; "2"; "3" |]
let xs3 = Some [| "1"; "2"; "4" |]
let xs4 = Some [| "1"; "2" |]
let xs5 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal false (xs1 = xs4)
equal true (xs1 <> xs4)
equal false (xs1 = xs5)
equal true (xs1 <> xs5)
equal true (xs5 = None)
equal false (xs5 <> None)

[<Fact>]
let ``test Tuple equality works`` () =
let xs1 = ( 1, 2, 3 )
Expand All @@ -103,6 +139,21 @@ let ``test Tuple equality works`` () =
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)

[<Fact>]
let ``test Tuple option equality works``() =
let xs1 = Some ( 1, 2, 3 )
let xs2 = Some ( 1, 2, 3 )
let xs3 = Some ( 1, 2, 4 )
let xs5 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal false (xs1 = xs5)
equal true (xs1 <> xs5)
equal true (xs5 = None)
equal false (xs5 <> None)

[<Fact>]
let ``test List equality works`` () =
let xs1 = [ 1; 2; 3 ]
Expand All @@ -113,6 +164,24 @@ let ``test List equality works`` () =
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)

[<Fact>]
let ``test List option equality works``() =
let xs1 = Some [ 1; 2; 3 ]
let xs2 = Some [ 1; 2; 3 ]
let xs3 = Some [ 1; 2; 4 ]
let xs4 = Some [ 1; 2; 3; 1 ]
let xs5 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal false (xs1 = xs4)
equal true (xs1 <> xs4)
equal false (xs1 = xs5)
equal true (xs1 <> xs5)
equal true (xs5 = None)
equal false (xs5 <> None)

[<Fact>]
let ``test Set equality works`` () =
let xs1 = Set [ 1; 2; 3 ]
Expand All @@ -127,6 +196,25 @@ let ``test Set equality works`` () =
equal true (xs1 = xs4)
equal false (xs1 <> xs5)

[<Fact>]
let ``test Set option equality works`` () =
let xs1 = Some (Set [ 1; 2; 3 ])
let xs2 = Some (Set [ 1; 2; 3 ])
let xs3 = Some (Set [ 1; 2; 4 ])
let xs4 = Some (Set [ 3; 2; 1 ])
let xs5 = Some (Set [ 1; 2; 3; 1 ])
let xs6 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal true (xs1 = xs4)
equal false (xs1 <> xs5)
equal false (xs1 = xs6)
equal true (xs1 <> xs6)
equal true (xs6 = None)
equal false (xs6 <> None)

[<Fact>]
let ``test Map equality works`` () =
let xs1 = Map [ ("a", 1); ("b", 2); ("c", 3) ]
Expand All @@ -139,6 +227,25 @@ let ``test Map equality works`` () =
equal false (xs1 <> xs2)
equal true (xs1 = xs4)

[<Fact>]
let ``test Map option equality works`` () =
let xs1 = Some (Map [ ("a", 1); ("b", 2); ("c", 3) ])
let xs2 = Some (Map [ ("a", 1); ("b", 2); ("c", 3) ])
let xs3 = Some (Map [ ("a", 1); ("b", 2); ("c", 4) ])
let xs4 = Some (Map [ ("c", 3); ("b", 2); ("a", 1) ])
let xs5 = Some (Map [ ("a", 1); ("b", 2); ("c", 3); ("d", 1) ])
let xs6 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal true (xs1 = xs4)
equal true (xs1 <> xs5)
equal false (xs1 = xs6)
equal true (xs1 <> xs6)
equal true (xs6 = None)
equal false (xs6 <> None)

[<Fact>]
let ``test Union equality works`` () =
let u1 = A 2
Expand Down
Loading