Skip to content

Omit redundant File block in SyntaxError output when crash line has location - #15085

Closed
Dextheking1 wants to merge 27 commits into
pytest-dev:mainfrom
Dextheking1:fix/syntaxerror-redundant-file-block-14994
Closed

Dextheking1 wants to merge 27 commits into
pytest-dev:mainfrom
Dextheking1:fix/syntaxerror-redundant-file-block-14994

Conversation

@Dextheking1

Copy link
Copy Markdown

Stacked on #14899 (which makes the crash line carry the error's own file:line:col); closes #14994.

Before

E     File "package/lib.py", line 1
E       def broken(:
E                  ^
E   SyntaxError: invalid syntax
package/lib.py:1:12: SyntaxError: invalid syntax

After

E   SyntaxError: invalid syntax
package/lib.py:1:12: SyntaxError: invalid syntax

Rationale

traceback.format_exception_only() emits a File "...", line N / source / caret block for SyntaxError, which ExceptionInfoFormatter.get_exconly() renders as E-lines. Once the crash line reports the error's own file:line:column (#14899), that block only repeats the same location, so it is now dropped in get_exconly() via a small _strip_syntax_error_file_block() helper (keeps the exception line itself verbatim; never strips when the exception line can't be identified).

When the error does not carry usable location info (no filename/lineno/offset), the crash line falls back to the traceback entry and the block is kept, so no information is ever lost:

E     File "weird.py", line 3
E       src
E   SyntaxError: custom import error

Closes #14994.

SemTiOne and others added 24 commits August 18, 2026 12:52
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
…ocation

When the crash line already carries the SyntaxError's own file:line:column
(see pytest-dev#14899), the File "...", line N / source / caret block emitted by
traceback.format_exception_only() in the E-lines is redundant. Drop it in
ExceptionInfoFormatter.get_exconly, keeping it on the fallback path (no
location info) so no information is lost.

Closes pytest-dev#14994.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 22, 2026
Dextheking1 and others added 3 commits September 23, 2026 14:00
)

The File/source/caret block is now intentionally omitted from SyntaxError
collection errors when the crash line carries the error's own
file:line:column location. Update the test to assert the new output
instead of the removed source line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redundant File ..., line N block in SyntaxError output

3 participants