beacon-biosignals / StableHashTraits.jl

Compute hashes over any Julia object simply and reproducibly
MIT License
7 stars 1 forks source link

Errors on Pairs #20

Closed ssfrr closed 1 year ago

ssfrr commented 1 year ago

It appears that stable_hash doesn't support Pairs, such as what you get from keyword argument splatting.

julia> using StableHashTraits

julia> foo(args...; kwargs...) = (stable_hash(args), stable_hash(kwargs))
foo (generic function with 1 method)

julia> foo(1)
(0xb07c9714, 0x48674bc7)

julia> foo(2; a=2)
ERROR: MethodError: no method matching write(::IOBuffer, ::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:a,), Tuple{Int64}}})
Closest candidates are:
  write(::IO, ::Any) at io.jl:672
  write(::IO, ::Any, ::Any...) at io.jl:673
  write(::Base.GenericIOBuffer, ::UInt8) at iobuffer.jl:442
  ...
Stacktrace:
 [1] write(io::IOBuffer, x::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:a,), Tuple{Int64}}})
   @ Base ./io.jl:672
 [2] write(io::IOBuffer, x::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:a,), Tuple{Int64}}})
   @ StableHashTraits ~/.julia/packages/StableHashTraits/XLtxZ/src/StableHashTraits.jl:57
 [3] write(io::IOBuffer, x::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:a,), Tuple{Int64}}}, context::StableHashTraits.GlobalContext)
   @ StableHashTraits ~/.julia/packages/StableHashTraits/XLtxZ/src/StableHashTraits.jl:56
 [4] stable_hash_helper(x::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:a,), Tuple{Int64}}}, hash::StableHashTraits.GenericFunHash{typeof(CRC32c.crc32c), UInt32}, context::StableHashTraits.GlobalContext, #unused#::UseWrite)
...
ssfrr commented 1 year ago

looks like

StableHashTraits.hash_method(::Base.Pairs) = UseIterate()

should work. I'll test things out a bit more and can put in a PR if no one gets to it first.

palday commented 1 year ago

Probably slightly better to do

StableHashTraits.hash_method(::AbstractDict) = UseIterate()

so that dictionaries as a whole are covered because Base.Pairs <: AbstractDict

haberdashPI commented 1 year ago

will be closed by #22

haberdashPI commented 1 year ago

Closed by #24