Skip to content

show method issues for CategoricalValue #435

@andreasnoack

Description

@andreasnoack

The package currently only defines a two-argument show method. This causes two issues. First, the output of repr can't be parsed

julia> Meta.parse(repr(categorical(["placebo"])[1]))
ERROR: ParseError:
# Error @ none:1:33
CategoricalValue{String, UInt32} "placebo"
#                               └────────┘ ── extra tokens after end of expression
Stacktrace:
 [1] #parse#3
   @ ./meta.jl:242 [inlined]
 [2] parse
   @ ./meta.jl:234 [inlined]
 [3] parse(str::String; filename::String, raise::Bool, depwarn::Bool)
   @ Base.Meta ./meta.jl:276
 [4] parse(str::String)
   @ Base.Meta ./meta.jl:274
 [5] top-level scope
   @ REPL[151]:1

Second, display of CategoricalArray is overly verbose

julia> [String7("placebo")]
1-element Vector{String7}:
 "placebo"

julia> categorical([String7("placebo")])
1-element CategoricalArray{String7,1,UInt32}:
 String7("placebo")

Changing

function Base.show(io::IO, x::CategoricalValue)
if get(io, :compact, false) ||
nonmissingtype(get(io, :typeinfo, Any)) === nonmissingtype(typeof(x))
show(io, unwrap(x))
else
print(io, typeof(x))
print(io, ' ')
show(io, unwrap(x))
if isordered(pool(x))
@printf(io, " (%i/%i)", levelcode(x), length(pool(x)))
end
end
end
to a three-argument method that also calls the three-argument methods might be sufficient to fix both issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions