Skip to content

fix: don't enter the debugger for a syntax error - #15239

Open
ShahabAhmed01 wants to merge 2 commits into
pytest-dev:mainfrom
ShahabAhmed01:fix/3649-pdb-syntax-error
Open

ShahabAhmed01 wants to merge 2 commits into
pytest-dev:mainfrom
ShahabAhmed01:fix/3649-pdb-syntax-error

Conversation

@ShahabAhmed01

@ShahabAhmed01 ShahabAhmed01 commented Oct 21, 2026 •

Copy link
Copy Markdown

Summary

  • fix: don't enter the debugger for a syntax error

When a test module cannot be imported because of a syntax error, pytest re-raises the error as a CollectError. With --pdb the debugger was then entered at the place where the error is re-raised, which is not helpful: the actual syntax error is only visible in the traceback that pytest prints.

The CollectError keeps the original SyntaxError as its cause, so the debugger is now skipped for exactly this case. The error is reported as before.

Other collection errors (for example the NameError of #181) still open the debugger.

Closes #3649

Changelog

  • changelog/3649.bugfix.rst

Checklist

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • Add text like closes #3649 to the PR description and/or commits.
  • Add a new changelog file in the changelog directory.
  • Add yourself to AUTHORS in alphabetical order.
  • If AI agents were used, they are credited in Co-authored-by commit trailers.

Testing

  • pytest testing/ — 4698 passed, 47 skipped, 13 xfailed
  • pytest testing/test_debugging.py — 54 passed, 1 skipped
  • ruff check src/_pytest/runner.py testing/test_debugging.py — clean
  • ruff format --check src/_pytest/runner.py testing/test_debugging.py — clean
  • mypy src/_pytest/runner.py — clean

The new test testing/test_debugging.py::TestPDB::test_pdb_not_entered_on_syntax_error fails without the change.

AI assistance

This change was prepared with AI assistance (OpenCode, using the MiniMax M3.1 Flash model) and was reviewed, tested and submitted by a human contributor. The commit carries a Co-authored-by trailer.

I first tried to open an issue comment with the root-cause analysis, but this account is not allowed to comment on other users issues in this repository, so the analysis is in this description instead.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 20:53
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@ShahabAhmed01

Copy link
Copy Markdown
Author

Local validation for this change, run on c9bf1ed (upstream main tip) with Python 3.12.15 on Linux:

testing/                    4698 passed, 47 skipped, 13 xfailed, 7 xpassed
testing/test_debugging.py     54 passed, 1 skipped, 6 xpassed
ruff check   src/_pytest/runner.py testing/test_debugging.py   -> All checks passed
ruff format --check src/_pytest/runner.py testing/test_debugging.py -> 2 files already formatted
mypy src/_pytest/runner.py                                      -> Success: no issues found in 1 source file

testing/test_debugging.py::TestPDB::test_pdb_not_entered_on_syntax_error fails without the change (the debugger is entered and the run reports Enter: Quitting debugger) and passes with it.

The full testing/ run is green before and after the change; the only error in a first run was testing/test_legacypath.py::test_cache_makedir, which comes from running pytest with -p no:cacheprovider (that plugin provides the cache fixture) and passes without that flag.

The debugger is still entered for other collection errors — test_pdb_interaction_on_collection_issue181 (a NameError at import) and test_pdb_collection_failure_is_shown both still pass unchanged.

@ShahabAhmed01

Copy link
Copy Markdown
Author

Follow-up on the second commit (28d81d9).

The first version of the helper had a if call.excinfo is None: return False guard that can never be taken — the only caller has already checked that an exception was raised. pytest's codecov.yml sets a 100% patch target, so that dead line is a patch-coverage miss on its own, which is why it was removed rather than left in place.

A local coverage run with the repository's own configuration (patch = ["subprocess"], branch = true) shows every added line executed:

coverage run -m pytest testing/test_debugging.py
coverage combine
coverage report

The subprocess coverage matters here: the syntax-error path only runs in the child process that runpytest_subprocess starts, so without patch = ["subprocess"] the new helper looks uncovered.

codecov/patch on this head currently reports "80.00% of diff hit (target 100.00%)" from a run that started one minute after the push, before the build matrix had uploaded any coverage. The same check reported 76.47% mid-rollup on #15228 and succeeded once the remaining uploads landed.

ShahabAhmed01 and others added 2 commits December 5, 2026 17:52
When a test module cannot be imported because of a syntax error, pytest
re-raises the error as a CollectError. With --pdb the debugger was then
entered at the place where the error is re-raised, which is not helpful:
the actual syntax error is only visible in the traceback.

Detect the syntax error -- the CollectError keeps it as its cause -- and
skip the debugger in that case. The error is reported as before.

Closes pytest-dev#3649

Co-authored-by: MiniMax M3.1 Flash <noreply@minimax.io>
The helper is only called once an exception was raised, so the
"did not raise" branch can never be taken. pytest's codecov/patch target
is 100% of the changed lines, so the dead branch has to go.

Co-authored-by: MiniMax M3.1 Flash <noreply@minimax.io>
@ShahabAhmed01
ShahabAhmed01 force-pushed the fix/3649-pdb-syntax-error branch from 28d81d9 to 866e99a Compare December 5, 2026 12:52

This branch has not been deployed

No deployments
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.

pytest syntax_error.py --pdb should not drop into pdb?

2 participants