Skip to content

Commit 2a84dfd

Browse files
authored
Added typevar defaults to (Base)ExceptionGroup (#147)
This allows an `except BaseExceptionGroup` clause to pass Pyright >= 1.1.399. Fixes #146.
1 parent fb9133b commit 2a84dfd

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: trailing-whitespace
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.11.4
19+
rev: v0.11.7
2020
hooks:
2121
- id: ruff
2222
args: [--fix, --show-fixes]

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
77

88
- Added ``**kwargs`` to function and method signatures as appropriate to match the
99
signatures in the standard library
10+
- In line with the stdlib typings in typeshed, updated ``(Base)ExceptionGroup`` generic
11+
types to define defaults for their generic arguments (defaulting to
12+
``BaseExceptionGroup[BaseException]`` and ``ExceptionGroup[Exception]``)
13+
(PR by @mikenerone)
1014

1115
**1.2.2**
1216

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ authors = [{name = "Alex Grönholm", email = "[email protected]"}]
1818
license = {file = "LICENSE"}
1919
requires-python = ">=3.7"
2020
dynamic = ["version"]
21+
dependencies = [
22+
"typing-extensions >= 4.6.0; python_version < '3.13'", # Needed for default (Base)ExceptionGroup generics
23+
]
2124

2225
[project.urls]
2326
Changelog = "https://github.com/agronholm/exceptiongroup/blob/main/CHANGES.rst"

src/exceptiongroup/_exceptions.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
from __future__ import annotations
22

3+
import sys
34
from collections.abc import Callable, Sequence
45
from functools import partial
56
from inspect import getmro, isclass
67
from typing import TYPE_CHECKING, Generic, Type, TypeVar, cast, overload
78

8-
_BaseExceptionT_co = TypeVar("_BaseExceptionT_co", bound=BaseException, covariant=True)
9+
if sys.version_info < (3, 13):
10+
from typing_extensions import TypeVar
11+
12+
_BaseExceptionT_co = TypeVar(
13+
"_BaseExceptionT_co", bound=BaseException, covariant=True, default=BaseException
14+
)
915
_BaseExceptionT = TypeVar("_BaseExceptionT", bound=BaseException)
10-
_ExceptionT_co = TypeVar("_ExceptionT_co", bound=Exception, covariant=True)
16+
_ExceptionT_co = TypeVar(
17+
"_ExceptionT_co", bound=Exception, covariant=True, default=Exception
18+
)
1119
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)
1220
# using typing.Self would require a typing_extensions dependency on py<3.11
1321
_ExceptionGroupSelf = TypeVar("_ExceptionGroupSelf", bound="ExceptionGroup")

tests/check_types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# expected type error when passing a BaseException to ExceptionGroup
1414
ExceptionGroup("", (KeyboardInterrupt(),)) # type: ignore[type-var]
1515

16+
# (Base)ExceptionGroup types should reflect these defaults for their generic arguments
17+
default_base_eg: BaseExceptionGroup = BaseExceptionGroup("", (KeyboardInterrupt(),))
18+
default_eg: ExceptionGroup = ExceptionGroup("", (ValueError(),))
19+
assert_type(default_base_eg, BaseExceptionGroup[BaseException])
20+
assert_type(default_eg, ExceptionGroup[Exception])
1621

1722
# code snippets from the README
1823

0 commit comments

Comments
 (0)