Skip to content

Commit fdb342c

Browse files
authored
make keytype and valtype work for UnionAll AbstractDicts (#53116)
Fixes #53115
1 parent ef69db6 commit fdb342c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

base/abstractdict.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ julia> keytype(Dict(Int32(1) => "foo"))
301301
Int32
302302
```
303303
"""
304-
keytype(::Type{<:AbstractDict{K,V}}) where {K,V} = K
304+
keytype(::Type{<:AbstractDict{K}}) where {K} = K
305305
keytype(a::AbstractDict) = keytype(typeof(a))
306306

307307
"""
@@ -315,7 +315,7 @@ julia> valtype(Dict(Int32(1) => "foo"))
315315
String
316316
```
317317
"""
318-
valtype(::Type{<:AbstractDict{K,V}}) where {K,V} = V
318+
valtype(::Type{<:AbstractDict{<:Any,V}}) where {V} = V
319319
valtype(a::AbstractDict) = valtype(typeof(a))
320320

321321
"""

test/dict.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,3 +1519,24 @@ let d = Dict()
15191519
d[1.0] = 'b'
15201520
@test only(d) === Pair{Any,Any}(1.0, 'b')
15211521
end
1522+
1523+
@testset "UnionAll `keytype` and `valtype` (issue #53115)" begin
1524+
K = Int8
1525+
V = Int16
1526+
dicts = (
1527+
AbstractDict, IdDict, Dict, WeakKeyDict, Base.ImmutableDict,
1528+
Base.PersistentDict, Iterators.Pairs
1529+
)
1530+
1531+
@testset "D: $D" for D dicts
1532+
@test_throws MethodError keytype(D)
1533+
@test_throws MethodError keytype(D{<:Any,V})
1534+
@test keytype(D{K }) == K
1535+
@test keytype(D{K, V}) == K
1536+
1537+
@test_throws MethodError valtype(D)
1538+
@test valtype(D{<:Any,V}) == V
1539+
@test_throws MethodError valtype(D{K })
1540+
@test valtype(D{K, V}) == V
1541+
end
1542+
end

0 commit comments

Comments
 (0)