Skip to content

Commit e3e96ed

Browse files
committed
Make AssertRaisesContext et al. generic
1 parent c4ee7c5 commit e3e96ed

3 files changed

Lines changed: 36 additions & 12 deletions

File tree

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
News in asserts 0.9.1
2+
=====================
3+
4+
Improvements
5+
------------
6+
7+
* `AssertRaisesContext` and sub-classes are now generic over the
8+
exception type.
9+
110
News in asserts 0.9.0
211
=====================
312

asserts/__init__.pyi

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
import datetime
22

33
from types import TracebackType
4-
from typing import \
5-
Any, Container, Type, Callable, Tuple, Union, ContextManager, \
6-
Pattern, Optional, Iterable, Text, NoReturn
4+
from typing import (
5+
Any,
6+
Callable,
7+
Container,
8+
ContextManager,
9+
Generic,
10+
Iterable,
11+
NoReturn,
12+
Optional,
13+
Pattern,
14+
Text,
15+
Tuple,
16+
Type,
17+
TypeVar,
18+
Union,
19+
)
720

8-
class AssertRaisesContext:
9-
exception: Type[BaseException]
21+
_E = TypeVar("_E", bound=BaseException)
22+
23+
class AssertRaisesContext(Generic[_E]):
24+
exception: Type[_E]
1025
msg_fmt: Text
11-
def __init__(self, exception: Type[BaseException], msg_fmt: Text = ...) -> None: ...
26+
def __init__(self, exception: Type[_E], msg_fmt: Text = ...) -> None: ...
1227
def __enter__(self) -> AssertRaisesContext: ...
1328
def __exit__(self, exc_type: Optional[Type[BaseException]],
1429
exc_val: Optional[BaseException],
1530
exc_tb: Optional[TracebackType]) -> Optional[bool]: ...
1631
def format_message(self, default_msg: Text) -> Text: ...
17-
def add_test(self, cb: Callable[[BaseException], None]) -> None: ...
32+
def add_test(self, cb: Callable[[_E], None]) -> None: ...
1833

19-
class AssertRaisesErrnoContext(AssertRaisesContext):
34+
class AssertRaisesErrnoContext(AssertRaisesContext[_E]):
2035
expected_errno: int
21-
def __init__(self, exception: Type[BaseException], expected_errno: int, msg_fmt: Text = ...) -> None: ...
36+
def __init__(self, exception: Type[_E], expected_errno: int, msg_fmt: Text = ...) -> None: ...
2237

23-
class AssertRaisesRegexContext(AssertRaisesContext):
38+
class AssertRaisesRegexContext(AssertRaisesContext[_E]):
2439
pattern: Text
25-
def __init__(self, exception: Type[BaseException], pattern: Text, msg_fmt: Text = ...) -> None: ...
40+
def __init__(self, exception: Type[_E], pattern: Text, msg_fmt: Text = ...) -> None: ...
2641

2742
class AssertWarnsContext:
2843
def __init__(self, warning_class: Type[Warning], msg_fmt: Text = ...) -> None: ...

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def read(fname):
1010

1111
setup(
1212
name="asserts",
13-
version="0.9.0",
13+
version="0.9.1",
1414
description="Stand-alone Assertions",
1515
long_description=read("README.rst"),
1616
author="Sebastian Rittau",

0 commit comments

Comments
 (0)