Skip to content

Commit 792a35b

Browse files
authored
Show Numbers compactly when typeinfo is a Union with Nothing or Missing (JuliaLang#48822)
1 parent fb8d7f1 commit 792a35b

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

base/rational.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ end
9595
function show(io::IO, x::Rational)
9696
show(io, numerator(x))
9797

98-
if isone(denominator(x)) && get(io, :typeinfo, Any) <: Rational
98+
if isone(denominator(x)) && nonnothing_nonmissing_typeinfo(io) <: Rational
9999
return
100100
end
101101

base/ryu/Ryu.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ end
112112
function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
113113
compact = get(io, :compact, false)::Bool
114114
buf = Base.StringVector(neededdigits(T))
115-
typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
115+
typed = !forceuntyped && !compact && Base.nonnothing_nonmissing_typeinfo(io) != typeof(x)
116116
pos = writeshortest(buf, 1, x, false, false, true, -1,
117117
(x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
118118
write(io, resize!(buf, pos - 1))

base/show.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,8 +1231,9 @@ function show(io::IO, tn::Core.TypeName)
12311231
print(io, ")")
12321232
end
12331233

1234+
nonnothing_nonmissing_typeinfo(io::IO) = nonmissingtype(nonnothingtype(get(io, :typeinfo, Any)))
1235+
show(io::IO, b::Bool) = print(io, nonnothing_nonmissing_typeinfo(io) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
12341236
show(io::IO, ::Nothing) = print(io, "nothing")
1235-
show(io::IO, b::Bool) = print(io, get(io, :typeinfo, Any) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
12361237
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
12371238
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
12381239
print(io::IO, n::Unsigned) = print(io, string(n))

test/show.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,20 @@ replstrcolor(x) = sprint((io, x) -> show(IOContext(io, :limit => true, :color =>
21702170
@test_repr "Bool[1, 0]"
21712171
end
21722172

2173+
@testset "Unions with Bool (#39590)" begin
2174+
@test repr([missing, false]) == "Union{Missing, Bool}[missing, 0]"
2175+
@test_repr "Union{Bool, Nothing}[1, 0, nothing]"
2176+
end
2177+
2178+
# issue #26847
2179+
@test_repr "Union{Missing, Float32}[1.0]"
2180+
2181+
# intersection of #45396 and #48822
2182+
@test_repr "Union{Missing, Rational{Int64}}[missing, 1//2, 2]"
2183+
2184+
# Don't go too far with #48822
2185+
@test_repr "Union{String, Bool}[true]"
2186+
21732187
# issue #30505
21742188
@test repr(Union{Tuple{Char}, Tuple{Char, Char}}[('a','b')]) == "Union{Tuple{Char}, Tuple{Char, Char}}[('a', 'b')]"
21752189

0 commit comments

Comments
 (0)