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] Custom hash functions are never called on Record Types. #3778

Closed
HLWeil opened this issue Mar 8, 2024 · 0 comments · Fixed by #3796
Closed

[Python] Custom hash functions are never called on Record Types. #3778

HLWeil opened this issue Mar 8, 2024 · 0 comments · Fixed by #3796

Comments

@HLWeil
Copy link

HLWeil commented Mar 8, 2024

Description

Custom hash functions are never called on Record Types.

Repro code

test.fsx

[<CustomEquality>]
[<NoComparison>]
type MyRecord = 

    {Name : string; Age: int }

    override this.Equals(that) =
        this.GetHashCode() = that.GetHashCode()

    /// Hash should just return age of person
    override this.GetHashCode() =
        this.Age

let p1 = {Name = "John"; Age = 30}

// Should return 30, but returns -4426817161268664163
printfn "Hash1: %A" (p1.GetHashCode())

test.cmd

dotnet fable . --lang python
python test.py

Explanation

In the transpiled code, the class MyRecord now implements a __hash__ method. But the method call is transpiled to safe_hash, which first checks for the existence of an GetHashCode method. This is always true for Record Types, so the custom __hash__ methdo is never called.

Related information

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

Workaround Code

#if FABLE_COMPILER_PYTHON
[<Emit("hasattr($0,\"__hash__\")")>]
let pyHasCustomHash (obj) : bool = nativeOnly
    
[<Emit("$0.__hash__()")>]
let pyCustomHash (obj) : int = nativeOnly
#endif

let hash obj =
    #if FABLE_COMPILER_PYTHON
        if pyHasCustomHash obj then
            pyCustomHash obj
        else
            obj.GetHashCode()
    #else
        obj.GetHashCode()
    #endif

@Freymaurer

@HLWeil HLWeil changed the title [Python] [Python] Custom hash functions are never called on Record Types. Mar 8, 2024
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.

1 participant