From 240096aeff54fe782a86a75a398873bc2d3c023d Mon Sep 17 00:00:00 2001 From: ShahabAhmed01 <192491541+ShahabAhmed01@users.noreply.github.com> Date: Thu, 22 Oct 2026 01:52:03 +0500 Subject: [PATCH 1/2] 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. 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 #3649 Co-authored-by: MiniMax M3.1 Flash --- AUTHORS | 1 + changelog/3649.bugfix.rst | 1 + src/_pytest/runner.py | 19 ++++++++++++++++++- testing/test_debugging.py | 11 +++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 changelog/3649.bugfix.rst diff --git a/AUTHORS b/AUTHORS index f04403e2d5c..92d47f5bbac 100644 --- a/AUTHORS +++ b/AUTHORS @@ -467,6 +467,7 @@ Segev Finer SemTiOne Serhii Mozghovyi Seth Junot +Shahab Ahmed Shantanu Jain Sharad Nair Shaygan Hooshyari diff --git a/changelog/3649.bugfix.rst b/changelog/3649.bugfix.rst new file mode 100644 index 00000000000..77bb5fa4444 --- /dev/null +++ b/changelog/3649.bugfix.rst @@ -0,0 +1 @@ +The debugger is no longer entered when a collection error is caused by a syntax error, as the traceback would only show where the error is re-raised instead of the syntax error itself. diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 629796598df..8d4cdd6d34f 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -285,6 +285,19 @@ def check_interactive_exception(call: CallInfo[object], report: BaseReport) -> b return True +def is_syntax_error(call: CallInfo[object]) -> bool: + """Check whether the call failed because of a syntax error. + + A syntax error is re-raised as a ``CollectError`` which keeps the original + ``SyntaxError`` as its cause, so both are checked here. + """ + if call.excinfo is None: + # Didn't raise. + return False + exc = call.excinfo.value + return isinstance(exc, SyntaxError) or isinstance(exc.__cause__, SyntaxError) + + TResult = TypeVar("TResult", covariant=True) @@ -610,5 +623,9 @@ def collect_one_node(collector: Collector) -> CollectReport: rep: CollectReport = ihook.pytest_make_collect_report(collector=collector) call = rep.__dict__.pop("call", None) if call and check_interactive_exception(call, rep): - ihook.pytest_exception_interact(node=collector, call=call, report=rep) + # The debugger is not entered for a syntax error: the traceback would + # point at the place where the error is re-raised instead of at the + # syntax error itself, which is not helpful (see #3649). + if not is_syntax_error(call): + ihook.pytest_exception_interact(node=collector, call=call, report=rep) return rep diff --git a/testing/test_debugging.py b/testing/test_debugging.py index 3a67cfa8cd6..4034f7eb7ed 100644 --- a/testing/test_debugging.py +++ b/testing/test_debugging.py @@ -838,6 +838,17 @@ def test_pdb_collection_failure_is_shown(self, pytester: Pytester) -> None: ["E NameError: *xxx*", "*! *Exit: Quitting debugger !*"] # due to EOF ) + def test_pdb_not_entered_on_syntax_error(self, pytester: Pytester) -> None: + """The debugger is not entered for a syntax error (see #3649). + + The traceback of a syntax error is still reported, entering the + debugger would only show where the error is re-raised. + """ + p1 = pytester.makepyfile("syntax error") + result = pytester.runpytest_subprocess("--pdb", p1) + result.stdout.fnmatch_lines(["E SyntaxError: invalid syntax"]) + result.stdout.no_fnmatch_line("*entering PDB*") + @pytest.mark.parametrize("post_mortem", (False, True)) def test_enter_leave_pdb_hooks_are_called( self, post_mortem, pytester: Pytester From 866e99a3eb33542383aa3213917433ea372b9da9 Mon Sep 17 00:00:00 2001 From: ShahabAhmed01 <192491541+ShahabAhmed01@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:02:19 +0500 Subject: [PATCH 2/2] fix: drop the unreachable branch from is_syntax_error 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 --- src/_pytest/runner.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 8d4cdd6d34f..7e4f2dba74b 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -285,16 +285,12 @@ def check_interactive_exception(call: CallInfo[object], report: BaseReport) -> b return True -def is_syntax_error(call: CallInfo[object]) -> bool: - """Check whether the call failed because of a syntax error. +def is_syntax_error(exc: BaseException) -> bool: + """Check whether the exception was caused by a syntax error. A syntax error is re-raised as a ``CollectError`` which keeps the original ``SyntaxError`` as its cause, so both are checked here. """ - if call.excinfo is None: - # Didn't raise. - return False - exc = call.excinfo.value return isinstance(exc, SyntaxError) or isinstance(exc.__cause__, SyntaxError) @@ -626,6 +622,7 @@ def collect_one_node(collector: Collector) -> CollectReport: # The debugger is not entered for a syntax error: the traceback would # point at the place where the error is re-raised instead of at the # syntax error itself, which is not helpful (see #3649). - if not is_syntax_error(call): + assert call.excinfo is not None + if not is_syntax_error(call.excinfo.value): ihook.pytest_exception_interact(node=collector, call=call, report=rep) return rep