Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Numbers compactly when typeinfo is a Union with Nothing or Missing #48822

Merged
merged 12 commits into from
Jan 3, 2024
2 changes: 1 addition & 1 deletion base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end
function show(io::IO, x::Rational)
show(io, numerator(x))

if isone(denominator(x)) && get(io, :typeinfo, Any) <: Rational
if isone(denominator(x)) && nonnothing_nonmissing_typeinfo(io) <: Rational
return
end

Expand Down
2 changes: 1 addition & 1 deletion base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ end
function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
compact = get(io, :compact, false)::Bool
buf = Base.StringVector(neededdigits(T))
typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
typed = !forceuntyped && !compact && Base.nonnothing_nonmissing_typeinfo(io) != typeof(x)
pos = writeshortest(buf, 1, x, false, false, true, -1,
(x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
write(io, resize!(buf, pos - 1))
Expand Down
3 changes: 2 additions & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1231,8 +1231,9 @@ function show(io::IO, tn::Core.TypeName)
print(io, ")")
end

nonnothing_nonmissing_typeinfo(io::IO) = nonmissingtype(nonnothingtype(get(io, :typeinfo, Any)))
show(io::IO, b::Bool) = print(io, nonnothing_nonmissing_typeinfo(io) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
show(io::IO, ::Nothing) = print(io, "nothing")
show(io::IO, b::Bool) = print(io, get(io, :typeinfo, Any) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
print(io::IO, n::Unsigned) = print(io, string(n))
Expand Down
14 changes: 14 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,20 @@ replstrcolor(x) = sprint((io, x) -> show(IOContext(io, :limit => true, :color =>
@test_repr "Bool[1, 0]"
end

@testset "Unions with Bool (#39590)" begin
@test repr([missing, false]) == "Union{Missing, Bool}[missing, 0]"
@test_repr "Union{Bool, Nothing}[1, 0, nothing]"
end

# issue #26847
@test_repr "Union{Missing, Float32}[1.0]"

# intersection of #45396 and #48822
@test_repr "Union{Missing, Rational{Int64}}[missing, 1//2, 2]"

# Don't go too far with #48822
@test_repr "Union{String, Bool}[true]"

# issue #30505
@test repr(Union{Tuple{Char}, Tuple{Char, Char}}[('a','b')]) == "Union{Tuple{Char}, Tuple{Char, Char}}[('a', 'b')]"

Expand Down