Skip to content

Commit 48f14c4

Browse files
zkneupperZachary Kneupperpre-commit-ci[bot]
authored
Remove unnecessary else clause in repr_failure() (#8661)
* Remove unnecessary else clause in repr_failure() * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Zachary Kneupper <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3ae0103 commit 48f14c4

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

src/_pytest/doctest.py

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -322,58 +322,54 @@ def repr_failure( # type: ignore[override]
322322
elif isinstance(excinfo.value, MultipleDoctestFailures):
323323
failures = excinfo.value.failures
324324

325-
if failures is not None:
326-
reprlocation_lines = []
327-
for failure in failures:
328-
example = failure.example
329-
test = failure.test
330-
filename = test.filename
331-
if test.lineno is None:
332-
lineno = None
333-
else:
334-
lineno = test.lineno + example.lineno + 1
335-
message = type(failure).__name__
336-
# TODO: ReprFileLocation doesn't expect a None lineno.
337-
reprlocation = ReprFileLocation(filename, lineno, message) # type: ignore[arg-type]
338-
checker = _get_checker()
339-
report_choice = _get_report_choice(
340-
self.config.getoption("doctestreport")
341-
)
342-
if lineno is not None:
343-
assert failure.test.docstring is not None
344-
lines = failure.test.docstring.splitlines(False)
345-
# add line numbers to the left of the error message
346-
assert test.lineno is not None
347-
lines = [
348-
"%03d %s" % (i + test.lineno + 1, x)
349-
for (i, x) in enumerate(lines)
350-
]
351-
# trim docstring error lines to 10
352-
lines = lines[max(example.lineno - 9, 0) : example.lineno + 1]
353-
else:
354-
lines = [
355-
"EXAMPLE LOCATION UNKNOWN, not showing all tests of that example"
356-
]
357-
indent = ">>>"
358-
for line in example.source.splitlines():
359-
lines.append(f"??? {indent} {line}")
360-
indent = "..."
361-
if isinstance(failure, doctest.DocTestFailure):
362-
lines += checker.output_difference(
363-
example, failure.got, report_choice
364-
).split("\n")
365-
else:
366-
inner_excinfo = ExceptionInfo.from_exc_info(failure.exc_info)
367-
lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)]
368-
lines += [
369-
x.strip("\n")
370-
for x in traceback.format_exception(*failure.exc_info)
371-
]
372-
reprlocation_lines.append((reprlocation, lines))
373-
return ReprFailDoctest(reprlocation_lines)
374-
else:
325+
if failures is None:
375326
return super().repr_failure(excinfo)
376327

328+
reprlocation_lines = []
329+
for failure in failures:
330+
example = failure.example
331+
test = failure.test
332+
filename = test.filename
333+
if test.lineno is None:
334+
lineno = None
335+
else:
336+
lineno = test.lineno + example.lineno + 1
337+
message = type(failure).__name__
338+
# TODO: ReprFileLocation doesn't expect a None lineno.
339+
reprlocation = ReprFileLocation(filename, lineno, message) # type: ignore[arg-type]
340+
checker = _get_checker()
341+
report_choice = _get_report_choice(self.config.getoption("doctestreport"))
342+
if lineno is not None:
343+
assert failure.test.docstring is not None
344+
lines = failure.test.docstring.splitlines(False)
345+
# add line numbers to the left of the error message
346+
assert test.lineno is not None
347+
lines = [
348+
"%03d %s" % (i + test.lineno + 1, x) for (i, x) in enumerate(lines)
349+
]
350+
# trim docstring error lines to 10
351+
lines = lines[max(example.lineno - 9, 0) : example.lineno + 1]
352+
else:
353+
lines = [
354+
"EXAMPLE LOCATION UNKNOWN, not showing all tests of that example"
355+
]
356+
indent = ">>>"
357+
for line in example.source.splitlines():
358+
lines.append(f"??? {indent} {line}")
359+
indent = "..."
360+
if isinstance(failure, doctest.DocTestFailure):
361+
lines += checker.output_difference(
362+
example, failure.got, report_choice
363+
).split("\n")
364+
else:
365+
inner_excinfo = ExceptionInfo.from_exc_info(failure.exc_info)
366+
lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)]
367+
lines += [
368+
x.strip("\n") for x in traceback.format_exception(*failure.exc_info)
369+
]
370+
reprlocation_lines.append((reprlocation, lines))
371+
return ReprFailDoctest(reprlocation_lines)
372+
377373
def reportinfo(self):
378374
assert self.dtest is not None
379375
return legacy_path(self.path), self.dtest.lineno, "[doctest] %s" % self.name

0 commit comments

Comments
 (0)