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

Base.show(::IO, ::MIME"text/plain", d::AbstractDict) requires length(d) #55931

Open
kuszmaul opened this issue Sep 29, 2024 · 1 comment · May be fixed by #56009
Open

Base.show(::IO, ::MIME"text/plain", d::AbstractDict) requires length(d) #55931

kuszmaul opened this issue Sep 29, 2024 · 1 comment · May be fixed by #56009
Labels
collections Data structures holding multiple items, e.g. sets display and printing Aesthetics and correctness of printed representations of objects.

Comments

@kuszmaul
Copy link

Base.show(::IO, ::MIME"text/plain", d::AbstractDict) for which length(d) is not defined crashes.

It crashes even if Base.IteratorSize(typeof(d)) isa Base.SizeUnknown, which is the standard way for an iterator to indicate that it doesn't support length.

It seems like AbstractDict doesn't require length to be defined in general. For example, Base.summary(::IO, ::AbstractDict) handles the case where the dictionary doesn't have length.

Reproduction:

struct NoLengthDict{K,V} <: AbstractDict{K,V}
    dict::Dict{K,V}
    NoLengthDict{K,V}() where {K,V} = new(Dict{K,V}())
end

Base.iterate(d::NoLengthDict, s...) = iterate(d.dict, s...)
Base.IteratorSize(::Type{<:NoLengthDict}) = Base.SizeUnknown()
Base.eltype(::Type{NoLengthDict{K,V}}) where {K,V} = Pair{K,V}
Base.setindex!(d::NoLengthDict, k, v) = d.dict[k] = v

Then in a REPL:

julia> x=NoLengthDict{Int,Int}()
NoLengthDict{Int64, Int64}()

julia> x[1]=2
2

julia> x
NoLengthDict{Int64, Int64}(...):Error showing value of type NoLengthDict{Int64, Int64}:
ERROR: MethodError: no method matching length(::NoLengthDict{Int64, Int64})

Closest candidates are:
  length(!Matched::Distributed.WorkerPool)
   @ Distributed /nix/store/xy70mqpgn05sydk1w6ismnc8plbmmhfs-julia-1.10.2/share/julia/stdlib/v1.10/Distributed/src/workerpool.jl:139
  length(!Matched::Core.SimpleVector)
   @ Base essentials.jl:770
  length(!Matched::BitSet)
   @ Base bitset.jl:348
  ...

Stacktrace:
  [1] show(io::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, t::NoLengthDict{Int64, Int64})
    @ Base ./show.jl:170

It seems like Base.show shouldn't crash in this case.

@Seelengrab Seelengrab added display and printing Aesthetics and correctness of printed representations of objects. collections Data structures holding multiple items, e.g. sets labels Sep 30, 2024
@fredrikekre fredrikekre changed the title Base.show(::IO, ::MIME"text/plain", d::AbstractDict) crashes if length(d) isn't defined Base.show(::IO, ::MIME"text/plain", d::AbstractDict) requires length(d) Sep 30, 2024
@ararslan
Copy link
Member

ararslan commented Oct 5, 2024

On current master (1.12.0-DEV.1298) you also get the error at the REPL just by typing x[.

@ararslan ararslan linked a pull request Oct 5, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants