Skip to content

Commit e318e24

Browse files
author
g97iulio1609
committed
fix: add BaseExceptionGroup compat import for Python 3.10 + ruff format
1 parent 603909d commit e318e24

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/mcp/shared/_exception_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77

88
from __future__ import annotations
99

10+
import sys
1011
from collections.abc import AsyncIterator
1112
from contextlib import asynccontextmanager
1213

1314
import anyio
1415
from anyio.abc import TaskGroup
1516

17+
if sys.version_info < (3, 11):
18+
from exceptiongroup import BaseExceptionGroup
19+
1620

1721
def collapse_exception_group(exc_group: BaseExceptionGroup[BaseException]) -> BaseException:
1822
"""Collapse a single-error exception group into the underlying exception.
@@ -31,9 +35,7 @@ def collapse_exception_group(exc_group: BaseExceptionGroup[BaseException]) -> Ba
3135
otherwise the original exception group unchanged.
3236
"""
3337
cancelled_class = anyio.get_cancelled_exc_class()
34-
real_errors: list[BaseException] = [
35-
exc for exc in exc_group.exceptions if not isinstance(exc, cancelled_class)
36-
]
38+
real_errors: list[BaseException] = [exc for exc in exc_group.exceptions if not isinstance(exc, cancelled_class)]
3739

3840
if len(real_errors) == 1:
3941
return real_errors[0]

tests/shared/test_exception_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
"""Tests for exception group collapsing utilities."""
22

3+
import sys
4+
35
import pytest
46

57
import anyio
68

9+
if sys.version_info < (3, 11):
10+
from exceptiongroup import BaseExceptionGroup
11+
712
from mcp.shared._exception_utils import collapse_exception_group, create_task_group
813

914

@@ -57,9 +62,7 @@ async def test_multiple_cancelled_one_real(self) -> None:
5762
cancelled_class = anyio.get_cancelled_exc_class()
5863
real_error = ConnectionError("lost connection")
5964

60-
group = BaseExceptionGroup(
61-
"test", [cancelled_class(), real_error, cancelled_class()]
62-
)
65+
group = BaseExceptionGroup("test", [cancelled_class(), real_error, cancelled_class()])
6366
result = collapse_exception_group(group)
6467

6568
assert result is real_error

0 commit comments

Comments
 (0)