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

Make --showlocals work together with --tb=short #6384

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Omer Hadari
Ondřej Súkup
Oscar Benjamin
Patrick Hayes
Pauli Virtanen
Paweł Adamczak
Pedro Algarvio
Philipp Loose
Expand Down
1 change: 1 addition & 0 deletions changelog/6384.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make `--showlocals` work also with `--tb=short`.
10 changes: 5 additions & 5 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,7 @@ def repr_traceback_entry(
message = excinfo and excinfo.typename or ""
path = self._makepath(entry.path)
filelocrepr = ReprFileLocation(path, entry.lineno + 1, message)
localsrepr = None
if not short:
localsrepr = self.repr_locals(entry.locals)
localsrepr = self.repr_locals(entry.locals)
return ReprEntry(lines, reprargs, localsrepr, filelocrepr, style)
if excinfo:
lines.extend(self.get_exconly(excinfo, indent=4))
Expand Down Expand Up @@ -1044,6 +1042,8 @@ def toterminal(self, tw) -> None:
for line in self.lines:
red = line.startswith("E ")
tw.line(line, bold=True, red=red)
if self.reprlocals:
self.reprlocals.toterminal(tw, indent=" " * 8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should have consistent indentation as with the case below, i.e. none currently.
(but when adding indentation it might be better to have 4 here (and below then probably also) maybe)

Copy link
Contributor Author

@pv pv Dec 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe 8 is better here, as the source code is indented to 4, which makes it harder to visually scan it if the indent is the same. The long traceback has quite different layout with horizontal dashes separating the different traceback entries, so I think it's not necessary to be consistent with it.

return
if self.reprfuncargs:
self.reprfuncargs.toterminal(tw)
Expand Down Expand Up @@ -1085,9 +1085,9 @@ class ReprLocals(TerminalRepr):
def __init__(self, lines: Sequence[str]) -> None:
self.lines = lines

def toterminal(self, tw) -> None:
def toterminal(self, tw, indent="") -> None:
for line in self.lines:
tw.line(line)
tw.line(indent + line)


class ReprFuncArgs(TerminalRepr):
Expand Down
20 changes: 20 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,26 @@ def test_showlocals():
]
)

def test_showlocals_short(self, testdir):
p1 = testdir.makepyfile(
"""
def test_showlocals_short():
x = 3
y = "xxxx"
assert 0
"""
)
result = testdir.runpytest(p1, "-l", "--tb=short")
result.stdout.fnmatch_lines(
[
"test_showlocals_short.py:*",
" assert 0",
"E assert 0",
" x = 3",
" y = 'xxxx'",
]
)

@pytest.fixture
def verbose_testfile(self, testdir):
return testdir.makepyfile(
Expand Down