Skip to content

Commit 69f2855

Browse files
authored
fallback to native traceback when handling ExceptionGroup (take 2) [SQUASH] (#10209)
* Squashed commit of the following: commit 41d339c46763bbe26123e1e6504b6e32290e33e1 Author: Cheukting <[email protected]> Date: Thu Jun 23 17:01:04 2022 +0800 test in all py versions commit b3572a5a12672228c3276fc8c8e05980dfb7888a Author: Cheukting <[email protected]> Date: Thu Jun 23 16:41:06 2022 +0800 add test commit 7166a2a51e4f99046b028b663c193d8b558c7fd4 Author: Cheukting <[email protected]> Date: Thu Jun 23 16:00:07 2022 +0800 update changelog commit b958c73d489157f0c0d4e46425083a5e2e2bc851 Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu Jun 23 07:50:52 2022 +0000 [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci commit ea7f376c6ca37c40c83df0e4a1cfaaedb34bae91 Author: Cheukting <[email protected]> Date: Thu Jun 23 15:48:21 2022 +0800 Fix MyPy commit 97469beb1da40257e9a061a5e19548546c9312c4 Author: Cheukting <[email protected]> Date: Thu Jun 23 15:03:48 2022 +0800 fix if ExceptionGroup not exist commit 84e553642cd69b4d499231d733df91ebfa84c7ad Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu Jun 23 03:43:27 2022 +0000 [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci commit 76bbef449b88bbd74fb5cca3b5293337a624ef03 Author: Cheukting <[email protected]> Date: Thu Jun 23 11:40:41 2022 +0800 adding changelog commit db82bebc5a4969e2083adcd97bdfd2a63bb17d98 Author: Cheukting <[email protected]> Date: Thu Jun 23 11:33:10 2022 +0800 fall back to native when handeling to exception groups * Typed ExceptionGroupTypes and changed to BaseExceptionGroup, fixed exceptionchain (excinfo->excinfo_, set reprcrash. Extended tests, though they're wip. * added exceptiongroup to pre-commit-config, moved away from tuple to directly defining BaseExceptionGroup, added block comment, added match line for inner exception, changked mark.skipif to importorskip to not need top-level import, changed tox.ini a bit - only uncovered should now be py37 without exceptiongroup, due to hypothesis * added py311-exceptiongroup to github CI, exceptiongroup is now a hard dependency on py<3.11, renamed bad variable names * added use_coverage to ubuntu-py311 * import BaseExceptionGroup with explicit version check instead of try/catch * removed from CI, added comments to tox and pre-commit
1 parent 3039391 commit 69f2855

File tree

8 files changed

+117
-1
lines changed

8 files changed

+117
-1
lines changed

.github/workflows/test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ jobs:
114114
python: "3.11-dev"
115115
os: ubuntu-latest
116116
tox_env: "py311"
117+
use_coverage: true
117118
- name: "ubuntu-pypy3"
118119
python: "pypy-3.7"
119120
os: ubuntu-latest

.pre-commit-config.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ repos:
6868
- packaging
6969
- tomli
7070
- types-pkg_resources
71+
# for mypy running on python>=3.11 since exceptiongroup is only a dependency
72+
# on <3.11
73+
- exceptiongroup>=1.0.0rc8
7174
- repo: local
7275
hooks:
7376
- id: rst

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Jeff Rackauckas
168168
Jeff Widman
169169
Jenni Rinker
170170
John Eddie Ayson
171+
John Litborn
171172
John Towler
172173
Jon Parise
173174
Jon Sonesen

changelog/9159.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Showing inner exceptions by forcing native display in ``ExceptionGroups`` even when using display options other than ``--tb=native``. A temporary step before full implementation of pytest-native display for inner exceptions in ``ExceptionGroups``.

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ install_requires =
4747
pluggy>=0.12,<2.0
4848
py>=1.8.2
4949
colorama;sys_platform=="win32"
50+
exceptiongroup>=1.0.0rc8;python_version<"3.11"
5051
importlib-metadata>=0.12;python_version<"3.8"
5152
tomli>=1.0.0;python_version<"3.11"
5253
python_requires = >=3.7

src/_pytest/_code/code.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656

5757
_TracebackStyle = Literal["long", "short", "line", "no", "native", "value", "auto"]
5858

59+
if sys.version_info[:2] < (3, 11):
60+
from exceptiongroup import BaseExceptionGroup
61+
5962

6063
class Code:
6164
"""Wrapper around Python code objects."""
@@ -924,7 +927,21 @@ def repr_excinfo(
924927
while e is not None and id(e) not in seen:
925928
seen.add(id(e))
926929
if excinfo_:
927-
reprtraceback = self.repr_traceback(excinfo_)
930+
# Fall back to native traceback as a temporary workaround until
931+
# full support for exception groups added to ExceptionInfo.
932+
# See https://github.com/pytest-dev/pytest/issues/9159
933+
if isinstance(e, BaseExceptionGroup):
934+
reprtraceback: Union[
935+
ReprTracebackNative, ReprTraceback
936+
] = ReprTracebackNative(
937+
traceback.format_exception(
938+
type(excinfo_.value),
939+
excinfo_.value,
940+
excinfo_.traceback[0]._rawentry,
941+
)
942+
)
943+
else:
944+
reprtraceback = self.repr_traceback(excinfo_)
928945
reprcrash: Optional[ReprFileLocation] = (
929946
excinfo_._getreprcrash() if self.style != "value" else None
930947
)

testing/code/test_excinfo.py

+87
Original file line numberDiff line numberDiff line change
@@ -1470,3 +1470,90 @@ def __getattr__(self, attr):
14701470
with pytest.raises(RuntimeError) as excinfo:
14711471
RecursionDepthError().trigger
14721472
assert "maximum recursion" in str(excinfo.getrepr())
1473+
1474+
1475+
def _exceptiongroup_common(
1476+
pytester: Pytester,
1477+
outer_chain: str,
1478+
inner_chain: str,
1479+
native: bool,
1480+
) -> None:
1481+
pre_raise = "exceptiongroup." if not native else ""
1482+
pre_catch = pre_raise if sys.version_info < (3, 11) else ""
1483+
filestr = f"""
1484+
{"import exceptiongroup" if not native else ""}
1485+
import pytest
1486+
1487+
def f(): raise ValueError("From f()")
1488+
def g(): raise BaseException("From g()")
1489+
1490+
def inner(inner_chain):
1491+
excs = []
1492+
for callback in [f, g]:
1493+
try:
1494+
callback()
1495+
except BaseException as err:
1496+
excs.append(err)
1497+
if excs:
1498+
if inner_chain == "none":
1499+
raise {pre_raise}BaseExceptionGroup("Oops", excs)
1500+
try:
1501+
raise SyntaxError()
1502+
except SyntaxError as e:
1503+
if inner_chain == "from":
1504+
raise {pre_raise}BaseExceptionGroup("Oops", excs) from e
1505+
else:
1506+
raise {pre_raise}BaseExceptionGroup("Oops", excs)
1507+
1508+
def outer(outer_chain, inner_chain):
1509+
try:
1510+
inner(inner_chain)
1511+
except {pre_catch}BaseExceptionGroup as e:
1512+
if outer_chain == "none":
1513+
raise
1514+
if outer_chain == "from":
1515+
raise IndexError() from e
1516+
else:
1517+
raise IndexError()
1518+
1519+
1520+
def test():
1521+
outer("{outer_chain}", "{inner_chain}")
1522+
"""
1523+
pytester.makepyfile(test_excgroup=filestr)
1524+
result = pytester.runpytest()
1525+
match_lines = []
1526+
if inner_chain in ("another", "from"):
1527+
match_lines.append(r"SyntaxError: <no detail available>")
1528+
1529+
match_lines += [
1530+
r" + Exception Group Traceback (most recent call last):",
1531+
rf" \| {pre_catch}BaseExceptionGroup: Oops \(2 sub-exceptions\)",
1532+
r" \| ValueError: From f\(\)",
1533+
r" \| BaseException: From g\(\)",
1534+
r"=* short test summary info =*",
1535+
]
1536+
if outer_chain in ("another", "from"):
1537+
match_lines.append(r"FAILED test_excgroup.py::test - IndexError")
1538+
else:
1539+
match_lines.append(
1540+
rf"FAILED test_excgroup.py::test - {pre_catch}BaseExceptionGroup: Oops \(2.*"
1541+
)
1542+
result.stdout.re_match_lines(match_lines)
1543+
1544+
1545+
@pytest.mark.skipif(
1546+
sys.version_info < (3, 11), reason="Native ExceptionGroup not implemented"
1547+
)
1548+
@pytest.mark.parametrize("outer_chain", ["none", "from", "another"])
1549+
@pytest.mark.parametrize("inner_chain", ["none", "from", "another"])
1550+
def test_native_exceptiongroup(pytester: Pytester, outer_chain, inner_chain) -> None:
1551+
_exceptiongroup_common(pytester, outer_chain, inner_chain, native=True)
1552+
1553+
1554+
@pytest.mark.parametrize("outer_chain", ["none", "from", "another"])
1555+
@pytest.mark.parametrize("inner_chain", ["none", "from", "another"])
1556+
def test_exceptiongroup(pytester: Pytester, outer_chain, inner_chain) -> None:
1557+
# with py>=3.11 does not depend on exceptiongroup, though there is a toxenv for it
1558+
pytest.importorskip("exceptiongroup")
1559+
_exceptiongroup_common(pytester, outer_chain, inner_chain, native=False)

tox.ini

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ envlist =
1717
docs
1818
docs-checklinks
1919

20+
# checks that 3.11 native ExceptionGroup works with exceptiongroup
21+
# not included in CI.
22+
py311-exceptiongroup
23+
2024

2125

2226
[testenv]
@@ -46,6 +50,7 @@ setenv =
4650
extras = testing
4751
deps =
4852
doctesting: PyYAML
53+
exceptiongroup: exceptiongroup>=1.0.0rc8
4954
numpy: numpy>=1.19.4
5055
pexpect: pexpect>=4.8.0
5156
pluggymain: pluggy @ git+https://github.com/pytest-dev/pluggy.git

0 commit comments

Comments
 (0)