Skip to content

Commit 167ca59

Browse files
committed
fix dict x == x to return missing if x contains it
closes #34744 use `isequal` to compare keys in `ImmutableDict`
1 parent b0d1c1a commit 167ca59

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

base/abstractdict.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
1717
haskey(d::AbstractDict, k) = in(k, keys(d))
1818

1919
function in(p::Pair, a::AbstractDict, valcmp=(==))
20-
v = get(a,p[1],secret_table_token)
20+
v = get(a, p.first, secret_table_token)
2121
if v !== secret_table_token
22-
return valcmp(v, p[2])
22+
return valcmp(v, p.second)
2323
end
2424
return false
2525
end
@@ -474,14 +474,13 @@ function isequal(l::AbstractDict, r::AbstractDict)
474474
end
475475

476476
function ==(l::AbstractDict, r::AbstractDict)
477-
l === r && return true
478477
if isa(l,IdDict) != isa(r,IdDict)
479478
return false
480479
end
481480
length(l) != length(r) && return false
482481
anymissing = false
483482
for pair in l
484-
isin = in(pair, r, ==)
483+
isin = in(pair, r)
485484
if ismissing(isin)
486485
anymissing = true
487486
elseif !isin

base/dict.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ ImmutableDict(KV::Pair, rest::Pair...) = ImmutableDict(ImmutableDict(rest...), K
746746
function in(key_value::Pair, dict::ImmutableDict, valcmp=(==))
747747
key, value = key_value
748748
while isdefined(dict, :parent)
749-
if dict.key == key
749+
if isequal(dict.key, key)
750750
valcmp(value, dict.value) && return true
751751
end
752752
dict = dict.parent
@@ -756,22 +756,22 @@ end
756756

757757
function haskey(dict::ImmutableDict, key)
758758
while isdefined(dict, :parent)
759-
dict.key == key && return true
759+
isequal(dict.key, key) && return true
760760
dict = dict.parent
761761
end
762762
return false
763763
end
764764

765765
function getindex(dict::ImmutableDict, key)
766766
while isdefined(dict, :parent)
767-
dict.key == key && return dict.value
767+
isequal(dict.key, key) && return dict.value
768768
dict = dict.parent
769769
end
770770
throw(KeyError(key))
771771
end
772772
function get(dict::ImmutableDict, key, default)
773773
while isdefined(dict, :parent)
774-
dict.key == key && return dict.value
774+
isequal(dict.key, key) && return dict.value
775775
dict = dict.parent
776776
end
777777
return default

test/dict.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ end
277277

278278
@test ismissing(Dict(1=>missing) == Dict(1=>missing))
279279
@test isequal(Dict(1=>missing), Dict(1=>missing))
280+
d = Dict(1=>missing)
281+
@test ismissing(d == d)
282+
d = Dict(1=>[missing])
283+
@test ismissing(d == d)
284+
d = Dict(1=>NaN)
285+
@test d != d
286+
@test isequal(d, d)
280287

281288
@test Dict(missing=>1) == Dict(missing=>1)
282289
@test isequal(Dict(missing=>1), Dict(missing=>1))
@@ -425,7 +432,7 @@ mutable struct T10647{T}; x::T; end
425432
a[1] = a
426433
a[a] = 2
427434
a[3] = T10647(a)
428-
@test a == a
435+
@test isequal(a, a)
429436
show(IOBuffer(), a)
430437
Base.show(Base.IOContext(IOBuffer(), :limit => true), a)
431438
Base.show(IOBuffer(), a)
@@ -449,7 +456,7 @@ end
449456

450457
ca = copy(a)
451458
@test length(ca) == length(a)
452-
@test ca == a
459+
@test isequal(ca, a)
453460
@test ca !== a # make sure they are different objects
454461

455462
ca = empty!(ca)
@@ -490,7 +497,7 @@ end
490497

491498
ca = copy(a)
492499
@test length(ca) == length(a)
493-
@test ca == a
500+
@test isequal(ca, a)
494501
@test ca !== a # make sure they are different objects
495502

496503
ca = empty!(ca)
@@ -716,6 +723,8 @@ import Base.ImmutableDict
716723
d5 = ImmutableDict(v...)
717724
@test d5 == d2
718725
@test collect(d5) == v
726+
727+
@test !haskey(ImmutableDict(-0.0=>1), 0.0)
719728
end
720729

721730
@testset "filtering" begin

0 commit comments

Comments
 (0)