Misalignment of first line in multiline show of PyObject

Open Beginner friendly
#888 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
julia, python
Domain
tooling

Research direction

Start at the show(io::IO, o::PyObject) implementation and reproduce the issue with a PyObject whose pystring result contains newlines, using the provided qiskit example. Verify that single-line output keeps its spacing while multiline output places PyObject on its own line and no longer misaligns the first row.

Written by the indexing model from the issue text.

Description

PyObjects whose pystring method returns a multiline string have their first row misaligned when shown. For example

using PyCall
qiskit = pyimport("qiskit")
qc = qiskit.QuantumCircuit(1)
qc.x(0)
qc.draw()

gives

PyObject      ┌───┐
q_0: ┤ X ├
     └───┘

A fix would be to replace

function show(io::IO, o::PyObject)
    print(io, "PyObject $(pystring(o))")
end

with

function show(io::IO, o::PyObject)
    str = pystring(o)
    sep = contains(str, "\n") ? "\n" : " " # Avoid misaligning the first line for multiline prints
    print(io, "PyObject$sep$str")
end

which gives the following print instead.

PyObject
     ┌───┐
q_0: ┤ X ├
     └───┘
Dominant language
Julia
Stars
1.5k
Forks
186
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaPy/PyCall.jl

All issues in JuliaPy/PyCall.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.