From 792a35b8b5d3dc102622bdead551e485a24e3a54 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Wed, 3 Jan 2024 19:23:38 +0600 Subject: [PATCH] Show Numbers compactly when typeinfo is a Union with Nothing or Missing (#48822) --- base/rational.jl | 2 +- base/ryu/Ryu.jl | 2 +- base/show.jl | 3 ++- test/show.jl | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 5288492e72e71..1b060384d95b2 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -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 diff --git a/base/ryu/Ryu.jl b/base/ryu/Ryu.jl index 9b236caeb6ff1..89589aa4ab668 100644 --- a/base/ryu/Ryu.jl +++ b/base/ryu/Ryu.jl @@ -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)) diff --git a/base/show.jl b/base/show.jl index eb9f7bcece49d..db0b429f3386f 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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)) diff --git a/test/show.jl b/test/show.jl index 08a29612b245c..b6106c710931c 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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')]"