Skip to content

Commit

Permalink
Do not print type information when :compact=>true (#371)
Browse files Browse the repository at this point in the history
`:compact=>true` used to be recommended only to print fewer digits for numbers,
but now the convention appears to be to use it more generally. This is not
needed for `CatetoricalArray` printing as they already set `:typeinfo`,
but it can help in other contexts such as when `NamedArray` dimension names
are `CategoricalValue`s.
  • Loading branch information
nalimilan authored Oct 25, 2021
1 parent 058ee8b commit f6bd5e7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ Base.convert(::Type{Union{S, Nothing}}, x::CategoricalValue) where {S <: Support
Base.Broadcast.broadcastable(x::CategoricalValue) = Ref(x)

function Base.show(io::IO, x::CategoricalValue)
if nonmissingtype(get(io, :typeinfo, Any)) === nonmissingtype(typeof(x))
if get(io, :compact, false) ||
nonmissingtype(get(io, :typeinfo, Any)) === nonmissingtype(typeof(x))
show(io, unwrap(x))
else
print(io, typeof(x))
Expand Down
8 changes: 8 additions & 0 deletions test/06_show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ using CategoricalArrays
@test sprint(show, ov2, context=:typeinfo=>typeof(ov2)) == "\"b\""
@test sprint(show, ov3, context=:typeinfo=>typeof(ov3)) == "\"a\""

@test sprint(show, nv1, context=:compact=>true) == "\"c\""
@test sprint(show, nv2, context=:compact=>true) == "\"b\""
@test sprint(show, nv3, context=:compact=>true) == "\"a\""

@test sprint(show, ov1, context=:compact=>true) == "\"c\""
@test sprint(show, ov2, context=:compact=>true) == "\"b\""
@test sprint(show, ov3, context=:compact=>true) == "\"a\""

@test sprint(print, nv1) == sprint(print, ov1) == "c"
@test sprint(print, nv2) == sprint(print, ov2) == "b"
@test sprint(print, nv3) == sprint(print, ov3) == "a"
Expand Down

0 comments on commit f6bd5e7

Please sign in to comment.