Skip to content

Commit b884dca

Browse files
authored
pass correct typeinfo IOContext when showing keys and values in dict (#37567)
* pass correct typeinfo IOContext when showing keys and values in dict
1 parent 9c8914e commit b884dca

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

base/show.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function show(io::IO, ::MIME"text/plain", t::AbstractDict{K,V}) where {K,V}
8585
if !haskey(io, :compact)
8686
recur_io = IOContext(recur_io, :compact => true)
8787
end
88+
recur_io_k = IOContext(recur_io, :typeinfo=>keytype(t))
89+
recur_io_v = IOContext(recur_io, :typeinfo=>valtype(t))
8890

8991
summary(io, t)
9092
isempty(t) && return
@@ -105,8 +107,8 @@ function show(io::IO, ::MIME"text/plain", t::AbstractDict{K,V}) where {K,V}
105107
vallen = 0
106108
for (i, (k, v)) in enumerate(t)
107109
i > rows && break
108-
ks[i] = sprint(show, k, context=recur_io, sizehint=0)
109-
vs[i] = sprint(show, v, context=recur_io, sizehint=0)
110+
ks[i] = sprint(show, k, context=recur_io_k, sizehint=0)
111+
vs[i] = sprint(show, v, context=recur_io_v, sizehint=0)
110112
keylen = clamp(length(ks[i]), keylen, cols)
111113
vallen = clamp(length(vs[i]), vallen, cols)
112114
end
@@ -127,7 +129,7 @@ function show(io::IO, ::MIME"text/plain", t::AbstractDict{K,V}) where {K,V}
127129
if limit
128130
key = rpad(_truncate_at_width_or_chars(ks[i], keylen, "\r\n"), keylen)
129131
else
130-
key = sprint(show, k, context=recur_io, sizehint=0)
132+
key = sprint(show, k, context=recur_io_k, sizehint=0)
131133
end
132134
print(recur_io, key)
133135
print(io, " => ")
@@ -136,7 +138,7 @@ function show(io::IO, ::MIME"text/plain", t::AbstractDict{K,V}) where {K,V}
136138
val = _truncate_at_width_or_chars(vs[i], cols - keylen, "\r\n")
137139
print(io, val)
138140
else
139-
show(recur_io, v)
141+
show(recur_io_v, v)
140142
end
141143
end
142144
end

test/show.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,18 @@ end
13871387

13881388
d = Dict("+"=>1)
13891389
@test showstr(d) == "Dict(\"+\" => 1)"
1390+
1391+
struct Foo
1392+
a::Int
1393+
end
1394+
struct Bar
1395+
a::Int
1396+
end
1397+
d = Dict([Bar(1), Bar(2)] => [Foo(1), Foo(2)])
1398+
@test showstr(d) == "Dict{Vector{$(curmod_prefix)Bar}, Vector{$(curmod_prefix)Foo}}([$(curmod_prefix)Bar(1), $(curmod_prefix)Bar(2)] => [$(curmod_prefix)Foo(1), $(curmod_prefix)Foo(2)])"
1399+
@test sprint(show, MIME("text/plain"), d) == """
1400+
Dict{Vector{$(curmod_prefix)Bar}, Vector{$(curmod_prefix)Foo}} with 1 entry:
1401+
[Bar(1), Bar(2)] => [Foo(1), Foo(2)]"""
13901402
end
13911403

13921404
@testset "alignment for pairs" begin # (#22899)

0 commit comments

Comments
 (0)