From c16a9cf789be913d994d31891e4071e240563081 Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Sat, 13 Aug 2022 15:25:03 +0200
Subject: [PATCH] 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
---
.pre-commit-config.yaml | 1 +
src/_pytest/_code/code.py | 23 +++++++++++------------
testing/code/test_excinfo.py | 17 ++++++++---------
tox.ini | 4 ++--
4 files changed, 22 insertions(+), 23 deletions(-)
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 694498b08..cb26b74f5 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -68,6 +68,7 @@ repos:
- packaging
- tomli
- types-pkg_resources
+ - exceptiongroup>=1.0.0rc8
- repo: local
hooks:
- id: rst
diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py
index 0c4041e84..34bf7bce8 100644
--- a/src/_pytest/_code/code.py
+++ b/src/_pytest/_code/code.py
@@ -56,18 +56,14 @@ if TYPE_CHECKING:
_TracebackStyle = Literal["long", "short", "line", "no", "native", "value", "auto"]
-ExceptionGroupTypes: Tuple[Type[BaseException], ...] = ()
-
-if sys.version_info >= (3, 11):
- ExceptionGroupTypes = (BaseExceptionGroup,) # type: ignore # noqa: F821
+BaseExceptionGroup: Optional[Type[BaseException]]
try:
- import exceptiongroup
-
- ExceptionGroupTypes += (exceptiongroup.BaseExceptionGroup,)
-except ModuleNotFoundError:
- # no backport installed - if <3.11 that means programs can't raise exceptiongroups
- # so we don't need to handle it
- pass
+ BaseExceptionGroup = BaseExceptionGroup # type: ignore
+except NameError:
+ try:
+ from exceptiongroup import BaseExceptionGroup
+ except ModuleNotFoundError:
+ BaseExceptionGroup = None
class Code:
@@ -937,7 +933,10 @@ class FormattedExcinfo:
while e is not None and id(e) not in seen:
seen.add(id(e))
if excinfo_:
- if isinstance(e, tuple(ExceptionGroupTypes)):
+ # Fall back to native traceback as a temporary workaround until
+ # full support for exception groups added to ExceptionInfo.
+ # See https://github.com/pytest-dev/pytest/issues/9159
+ if BaseExceptionGroup is not None and isinstance(e, BaseExceptionGroup):
reprtraceback: Union[
ReprTracebackNative, ReprTraceback
] = ReprTracebackNative(
diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py
index 2a032b021..2e5cc03e5 100644
--- a/testing/code/test_excinfo.py
+++ b/testing/code/test_excinfo.py
@@ -11,11 +11,6 @@ from typing import Tuple
from typing import TYPE_CHECKING
from typing import Union
-try:
- import exceptiongroup # noqa (referred to in strings)
-except ModuleNotFoundError:
- pass
-
import _pytest
import pytest
from _pytest._code.code import ExceptionChainRepr
@@ -28,6 +23,7 @@ from _pytest.pathlib import import_path
from _pytest.pytester import LineMatcher
from _pytest.pytester import Pytester
+
if TYPE_CHECKING:
from _pytest._code.code import _TracebackStyle
@@ -1526,7 +1522,12 @@ def _exceptiongroup_common(
"""
pytester.makepyfile(test_excgroup=filestr)
result = pytester.runpytest()
- match_lines = [
+ match_lines = []
+ if inner_chain in ("another", "from"):
+ match_lines.append(r"SyntaxError: ")
+
+ match_lines += [
+ r" + Exception Group Traceback (most recent call last):",
rf" \| {pre2}BaseExceptionGroup: Oops \(2 sub-exceptions\)",
r" \| ValueError: From f\(\)",
r" \| BaseException: From g\(\)",
@@ -1550,10 +1551,8 @@ def test_native_exceptiongroup(pytester: Pytester, outer_chain, inner_chain) ->
_exceptiongroup_common(pytester, outer_chain, inner_chain, native=True)
-@pytest.mark.skipif(
- "exceptiongroup" not in sys.modules, reason="exceptiongroup not installed"
-)
@pytest.mark.parametrize("outer_chain", ["none", "from", "another"])
@pytest.mark.parametrize("inner_chain", ["none", "from", "another"])
def test_exceptiongroup(pytester: Pytester, outer_chain, inner_chain) -> None:
+ pytest.importorskip("exceptiongroup")
_exceptiongroup_common(pytester, outer_chain, inner_chain, native=False)
diff --git a/tox.ini b/tox.ini
index a80a76782..9a30bbc3a 100644
--- a/tox.ini
+++ b/tox.ini
@@ -9,9 +9,9 @@ envlist =
py39
py310
py311
- py311-exceptiongroup
pypy3
- py37-{pexpect,xdist,unittestextras,numpy,pluggymain,exceptiongroup}
+ py37-{pexpect,xdist,unittestextras,numpy,pluggymain}
+ py311-exceptiongroup
doctesting
plugins
py37-freeze