From: Josenilson Ferreira da Silva Date: Mon, 27 Apr 2025 22:00:00 -0300 Subject: Workaround for pytest 9.0.x IndexError with Python 3.14 Forwarded: not-needed Monkey-patch get_statement_startend2 to guard against IndexError when insert_index is 0, triggered by Python 3.14 AST changes. Fixed in pytest 9.1+. --- Index: python-friendly-traceback/tests/conftest.py =================================================================== --- /dev/null +++ python-friendly-traceback/tests/conftest.py @@ -0,0 +1,31 @@ +# Workaround for pytest 9.0.x bug with Python 3.14 +# Fixed in pytest 9.1+ +import ast +import bisect +import _pytest._code.source as _source + + +def _patched_get_statement_startend2(lineno, node): + values = [] + + for x in ast.walk(node): + if isinstance(x, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)): + for d in x.decorator_list: + values.append(d.lineno - 1) + if hasattr(x, "lineno"): + values.append(x.lineno - 1) + for name in ("finalbody", "orelse"): + val = getattr(x, name, None) + if isinstance(val, list) and val and hasattr(val[0], "lineno"): + values.append(val[0].lineno - 1 - 1) + + values.sort() + insert_index = bisect.bisect_right(values, lineno) + if insert_index == 0: + return lineno, None + start = values[insert_index - 1] + end = None if insert_index >= len(values) else values[insert_index] + return start, end + + +_source.get_statement_startend2 = _patched_get_statement_startend2