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

0/1 printing of false/true is confusing in Dict #30683

Closed
tkf opened this issue Jan 10, 2019 · 8 comments · Fixed by #33290
Closed

0/1 printing of false/true is confusing in Dict #30683

tkf opened this issue Jan 10, 2019 · 8 comments · Fixed by #33290
Labels
display and printing Aesthetics and correctness of printed representations of objects.

Comments

@tkf
Copy link
Member

tkf commented Jan 10, 2019

julia> VERSION
v"1.2.0-DEV.146"

julia> show(Dict(true => false))
Dict(1=>0)

Also,

julia> Dict(true => false)
Dict{Bool,Bool} with 1 entry:
  true => false

seems to be inconsistent with

julia> [true => false]
1-element Array{Pair{Bool,Bool},1}:
 1 => 0

It looks like display(Dict(...)) does not set typeinfo while show(Dict(...)) does. Here is the code I used to check it:

function logio_show(io, mime)
    push!(logio_log, ("mime", mime))
    push!(logio_log, ("io isa IOContext", io isa IOContext))
    if io isa IOContext
        push!(logio_log, ("io.dict", io.dict))
    end
    print(io, "LogIO")
end

Base.show(io::IO, mime::MIME"text/plain", ::LogIO) =
    logio_show(io, mime)

Base.show(io::IO, ::LogIO) =
    logio_show(io, nothing)

then

empty!(logio_log)
display(Dict(:a=>LogIO()))
logio_log

shows

3-element Array{Any,1}:
 ("mime", nothing)                                                                                                
 ("io isa IOContext", true)                                                                                       
 ("io.dict", Base.ImmutableDict{Symbol,Any}(:compact=>true,:SHOWN_SET=>Dict(:a=>LogIO),:module=>Main,:limit=>true,:color=>true))

(i.e., no typeinfo) while

empty!(logio_log)
show(Dict(:a=>LogIO()))
logio_log

shows

3-element Array{Any,1}:
 ("mime", nothing)                                                                                                
 ("io isa IOContext", true)                                                                                       
 ("io.dict", Base.ImmutableDict{Symbol,Any}(:typeinfo=>LogIO,:compact=>true,:compact=>true,:typeinfo=>Pair{Symbol,LogIO},:SHOWN_SET=>Dict(:a=>LogIO),:color=>true))

i.e., typeinfo is set to LogIO.

@stevengj stevengj added the display and printing Aesthetics and correctness of printed representations of objects. label Jan 11, 2019
@stevengj
Copy link
Member

The show(io, dict) method sets :typeinfo here, which it shouldn't since it doesn't print the type. Whereas the show(io, text/plain, dict) method fails to set :typeinfo here.

@stevengj
Copy link
Member

That being said, we may want to think about disabling printing of boolean values as 0/1 when printing dict (or maybe in general for printing pairs and tuples even if the typeinfo indicates a Bool field).

@stevengj
Copy link
Member

Would be good to revisit this.

@JeffBezanson
Copy link
Member

This might be another case where a parseable or repr printing flag would make sense.

@tkf
Copy link
Member Author

tkf commented Sep 13, 2019

a parseable or repr printing flag would make sense

Please see #33260

@stevengj
Copy link
Member

This might be another case where a parseable or repr printing flag would make sense.

That seems orthogonal to the present issue, which is simply that typeinfo is being set incorrectly.

@logankilpatrick
Copy link
Member

@JeffBezanson @stevengj Can you add some context as to why this behavior exists:

d = [true, false, true]
3-element Vector{Bool}:
1
0
1

instead of showing true, false, true in the output?

@KristofferC
Copy link
Member

#30575 is the context. Now, one could ask the question if it is worth printing it compactly like that for a Vector{Bool} where every element is on its own line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants