Skip to content

Commit

Permalink
Add support for ExceptionGroup backport (through "exceptiongroup" lib)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Sep 8, 2023
1 parent a91c1cc commit 9da27db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
12 changes: 10 additions & 2 deletions loguru/_better_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ def is_exception_group(exc):
return isinstance(exc, ExceptionGroup)

else:
try:
from exceptiongroup import ExceptionGroup
except ImportError:

def is_exception_group(exc):
return False
def is_exception_group(exc):
return False

else:

def is_exception_group(exc):
return isinstance(exc, ExceptionGroup)


class SyntaxHighlighter:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"colorama==0.4.6 ; python_version>='3.8'",
"freezegun==1.1.0 ; python_version<'3.8'",
"freezegun==1.2.2 ; python_version>='3.8'",
"exceptiongroup==1.1.3 ; python_version>='3.7' and python_version<'3.11'",
# Type checking.
"mypy==v0.910 ; python_version<'3.6'",
"mypy==v0.971 ; python_version>='3.6' and python_version<'3.7'",
Expand Down
18 changes: 18 additions & 0 deletions tests/test_exceptions_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,21 @@ def test_exception_modern(filename, minimum_python_version):
pytest.skip("Feature not supported in this Python version")

compare_exception("modern", filename)


@pytest.mark.skipif(
not (3, 7) <= sys.version_info < (3, 11), reason="No backport available or needed"
)
def test_group_exception_using_backport(writer):
from exceptiongroup import ExceptionGroup

from loguru import logger

logger.add(writer, backtrace=True, diagnose=True, colorize=False, format="")

try:
raise ExceptionGroup("Test", [ValueError(1), ValueError(2)])
except Exception:
logger.exception("")

assert writer.read().strip().startswith("+ Exception Group Traceback (most recent call last):")

0 comments on commit 9da27db

Please sign in to comment.