diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index d9a6b9edb..2fdd8a086 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -725,11 +725,11 @@ class FormattedExcinfo: ) -> List[str]: """Return formatted and marked up source lines.""" lines = [] - if source is not None and line_index < 0: - line_index += len(source.lines) - if source is None or line_index >= len(source.lines) or line_index < 0: + if source is None or line_index >= len(source.lines): source = Source("???") line_index = 0 + if line_index < 0: + line_index += len(source) space_prefix = " " if short: lines.append(space_prefix + source.lines[line_index].strip())