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

[Python] Nested type with Custom Hashcode fails #3674

Closed
HLWeil opened this issue Dec 19, 2023 · 3 comments · Fixed by #3680
Closed

[Python] Nested type with Custom Hashcode fails #3674

HLWeil opened this issue Dec 19, 2023 · 3 comments · Fixed by #3680

Comments

@HLWeil
Copy link

HLWeil commented Dec 19, 2023

Description

If a record type with custom equality and hashcode is included in another type, calling the hashcode on the upper type fails, stating that the record type with custom hashcode is unhashable.

Repro code

test.fsx

[<CustomEquality; NoComparison>]
type X = 
    {
        ID : int 
        Name : string 
    }

    override x.GetHashCode() = x.ID

    override this.Equals(other : obj) = 
        match other with
        | :? X as other -> this.ID = other.ID
        | _ -> false

type Y = 
    | X of X

let x = {ID = 1; Name = "a"}

// Works
printfn $"x:{x.GetHashCode()}"

let y = X x

// Fails
printfn $"y:{y.GetHashCode()}"

test.cmd

dotnet fable . --lang python
python test.py

Expected and actual results

Expected result: Should return a hashcode for x and y

Actual result: Only returns a hashcode for x but fails for y

x:1
Traceback (most recent call last):
  File "C:\Users\HLWei\Downloads\fable-testing\test.py", line 51, in <module>
    to_console(("y:" + str(safe_hash(y))) + "")
                           ^^^^^^^^^^^^
  File "C:\Users\HLWei\Downloads\fable-testing\fable_modules\fable_library\util.py", line 2632, in safe_hash
    else hash(x)
         ^^^^^^^
  File "C:\Users\HLWei\Downloads\fable-testing\fable_modules\fable_library\types.py", line 85, in __hash__
    return hash((hash(self.tag), *hashes))
                ^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'X'

Related information

  • dotnet fable --version: 4.9.0
  • dotnet tool list/update/install: 4.9.0
  • Windows11

@Freymaurer

@HLWeil
Copy link
Author

HLWeil commented Jan 22, 2024

Thanks for the fix, @dbrattli!

Any plans on when the next version including this fix will be released?

@MangelMaxime
Copy link
Member

@HLWeil I will be working on the next release of Fable during the comings days.

@HLWeil
Copy link
Author

HLWeil commented Jan 22, 2024

Great to hear, thank you, @MangelMaxime!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants