Skip to content

Commit b7f1988

Browse files
committed
Modernize type stub
* Import from collections.abc/re/contextlib instead of typing where possible. * Use builtin types where possible. * Use "|" operator instead of Optional and Union. * Tighten JSON type. * Use object instead of Any where applicable. * Replace typing.Text with str.
1 parent 8aa3393 commit b7f1988

File tree

2 files changed

+75
-77
lines changed

2 files changed

+75
-77
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Drop support for Python 3.6.
66

7+
### Other Changes
8+
9+
- Modernize the type stubs.
10+
711
## News in asserts 0.11.1
812

913
### API Additions

asserts/__init__.pyi

Lines changed: 71 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,163 @@
11
import datetime
2-
2+
from collections.abc import Callable, Container, Iterable
3+
from contextlib import AbstractContextManager as ContextManager
4+
from re import Pattern
35
from types import TracebackType
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-
)
6+
from typing import Any, Generic, NoReturn, TypeVar
207

218
_E = TypeVar("_E", bound=BaseException)
229
_S = TypeVar("_S")
2310

2411
class AssertRaisesContext(Generic[_E]):
25-
exception: Type[_E]
26-
msg_fmt: Text
27-
def __init__(self, exception: Type[_E], msg_fmt: Text = ...) -> None: ...
12+
exception: type[_E]
13+
msg_fmt: str
14+
def __init__(self, exception: type[_E], msg_fmt: str = ...) -> None: ...
2815
def __enter__(self: _S) -> _S: ...
2916
def __exit__(
3017
self,
31-
exc_type: Optional[Type[BaseException]],
32-
exc_val: Optional[BaseException],
33-
exc_tb: Optional[TracebackType],
18+
exc_type: type[BaseException] | None,
19+
exc_val: BaseException | None,
20+
exc_tb: TracebackType | None,
3421
) -> bool: ...
35-
def format_message(self, default_msg: Text) -> Text: ...
36-
def add_test(self, cb: Callable[[_E], Any]) -> None: ...
22+
def format_message(self, default_msg: str) -> str: ...
23+
def add_test(self, cb: Callable[[_E], object]) -> None: ...
3724
@property
3825
def exc_val(self) -> _E: ...
3926

4027
class AssertRaisesErrnoContext(AssertRaisesContext[_E]):
4128
expected_errno: int
4229
def __init__(
43-
self, exception: Type[_E], expected_errno: int, msg_fmt: Text = ...
30+
self, exception: type[_E], expected_errno: int, msg_fmt: str = ...
4431
) -> None: ...
4532

4633
class AssertRaisesRegexContext(AssertRaisesContext[_E]):
47-
pattern: Text
34+
pattern: str
4835
def __init__(
49-
self, exception: Type[_E], pattern: Text, msg_fmt: Text = ...
36+
self, exception: type[_E], pattern: str, msg_fmt: str = ...
5037
) -> None: ...
5138

5239
class AssertWarnsContext:
5340
def __init__(
54-
self, warning_class: Type[Warning], msg_fmt: Text = ...
41+
self, warning_class: type[Warning], msg_fmt: str = ...
5542
) -> None: ...
5643
def __enter__(self: _S) -> _S: ...
5744
def __exit__(
5845
self,
59-
exc_type: Optional[Type[BaseException]],
60-
exc_val: Optional[BaseException],
61-
exc_tb: Optional[TracebackType],
46+
exc_type: type[BaseException] | None,
47+
exc_val: BaseException | None,
48+
exc_tb: TracebackType | None,
6249
) -> None: ...
63-
def format_message(self) -> Text: ...
50+
def format_message(self) -> str: ...
6451
def add_test(self, cb: Callable[[Warning], bool]) -> None: ...
6552

6653
class AssertWarnsRegexContext(AssertWarnsContext):
67-
pattern: Text
54+
pattern: str
6855
def __init__(
69-
self, warning_class: Type[Warning], msg_fmt: Text = ...
56+
self, warning_class: type[Warning], msg_fmt: str = ...
7057
) -> None: ...
7158

72-
def fail(msg: Text = ...) -> NoReturn: ...
73-
def assert_true(expr: Any, msg_fmt: Text = ...) -> None: ...
74-
def assert_false(expr: Any, msg_fmt: Text = ...) -> None: ...
75-
def assert_boolean_true(expr: Any, msg_fmt: Text = ...) -> None: ...
76-
def assert_boolean_false(expr: Any, msg_fmt: Text = ...) -> None: ...
77-
def assert_is_none(expr: Any, msg_fmt: Text = ...) -> None: ...
78-
def assert_is_not_none(expr: Any, msg_fmt: Text = ...) -> None: ...
79-
def assert_equal(first: Any, second: Any, msg_fmt: Text = ...) -> None: ...
80-
def assert_not_equal(first: Any, second: Any, msg_fmt: Text = ...) -> None: ...
59+
def fail(msg: str = ...) -> NoReturn: ...
60+
def assert_true(expr: object, msg_fmt: str = ...) -> None: ...
61+
def assert_false(expr: object, msg_fmt: str = ...) -> None: ...
62+
def assert_boolean_true(expr: object, msg_fmt: str = ...) -> None: ...
63+
def assert_boolean_false(expr: object, msg_fmt: str = ...) -> None: ...
64+
def assert_is_none(expr: object, msg_fmt: str = ...) -> None: ...
65+
def assert_is_not_none(expr: object, msg_fmt: str = ...) -> None: ...
66+
def assert_equal(
67+
first: object, second: object, msg_fmt: str = ...
68+
) -> None: ...
69+
def assert_not_equal(
70+
first: object, second: object, msg_fmt: str = ...
71+
) -> None: ...
8172
def assert_almost_equal(
8273
first: float,
8374
second: float,
84-
msg_fmt: Text = ...,
75+
msg_fmt: str = ...,
8576
places: int = ...,
8677
delta: float = ...,
8778
) -> None: ...
8879
def assert_not_almost_equal(
8980
first: float,
9081
second: float,
91-
msg_fmt: Text = ...,
82+
msg_fmt: str = ...,
9283
places: int = ...,
9384
delta: float = ...,
9485
) -> None: ...
9586
def assert_dict_equal(
9687
first: dict,
9788
second: dict,
98-
key_msg_fmt: Text = ...,
99-
value_msg_fmt: Text = ...,
89+
key_msg_fmt: str = ...,
90+
value_msg_fmt: str = ...,
10091
) -> None: ...
10192
def assert_dict_superset(
10293
first: dict,
10394
second: dict,
104-
key_msg_fmt: Text = ...,
105-
value_msg_fmt: Text = ...,
106-
) -> None: ...
107-
def assert_less(first: Any, second: Any, msg_fmt: Text = ...) -> None: ...
108-
def assert_less_equal(
109-
first: Any, second: Any, msg_fmt: Text = ...
95+
key_msg_fmt: str = ...,
96+
value_msg_fmt: str = ...,
11097
) -> None: ...
111-
def assert_greater(first: Any, second: Any, msg_fmt: Text = ...) -> None: ...
98+
def assert_less(first: Any, second: Any, msg_fmt: str = ...) -> None: ...
99+
def assert_less_equal(first: Any, second: Any, msg_fmt: str = ...) -> None: ...
100+
def assert_greater(first: Any, second: Any, msg_fmt: str = ...) -> None: ...
112101
def assert_greater_equal(
113-
first: Any, second: Any, msg_fmt: Text = ...
102+
first: Any, second: Any, msg_fmt: str = ...
114103
) -> None: ...
115104
def assert_regex(
116-
text: Text, regex: Union[Text, Pattern[Text]], msg_fmt: Text = ...
105+
text: str, regex: str | Pattern[str], msg_fmt: str = ...
106+
) -> None: ...
107+
def assert_is(first: object, second: object, msg_fmt: str = ...) -> None: ...
108+
def assert_is_not(
109+
first: object, second: object, msg_fmt: str = ...
117110
) -> None: ...
118-
def assert_is(first: Any, second: Any, msg_fmt: Text = ...) -> None: ...
119-
def assert_is_not(first: Any, second: Any, msg_fmt: Text = ...) -> None: ...
120111
def assert_in(
121-
first: Any, second: Container[Any], msg_fmt: Text = ...
112+
first: Any, second: Container[Any], msg_fmt: str = ...
122113
) -> None: ...
123114
def assert_not_in(
124-
first: Any, second: Container[Any], msg_fmt: Text = ...
115+
first: Any, second: Container[Any], msg_fmt: str = ...
125116
) -> None: ...
126117
def assert_between(
127-
lower_bound: Any, upper_bound: Any, expr: Any, msg_fmt: Text = ...
118+
lower_bound: Any, upper_bound: Any, expr: Any, msg_fmt: str = ...
128119
) -> None: ...
129120
def assert_is_instance(
130-
obj: Any, cls: Union[type, Tuple[type, ...]], msg_fmt: Text = ...
121+
obj: object, cls: type | tuple[type, ...], msg_fmt: str = ...
131122
) -> None: ...
132123
def assert_not_is_instance(
133-
obj: Any, cls: Union[type, Tuple[type, ...]], msg_fmt: Text = ...
124+
obj: object, cls: type | tuple[type, ...], msg_fmt: str = ...
134125
) -> None: ...
135126
def assert_count_equal(
136-
sequence1: Iterable[Any], sequence2: Iterable[Any], msg_fmt: Text = ...
127+
sequence1: Iterable[Any], sequence2: Iterable[Any], msg_fmt: str = ...
128+
) -> None: ...
129+
def assert_has_attr(
130+
obj: object, attribute: str, msg_fmt: str = ...
137131
) -> None: ...
138-
def assert_has_attr(obj: Any, attribute: str, msg_fmt: Text = ...) -> None: ...
139132
def assert_datetime_about_now(
140-
actual: Optional[datetime.datetime], msg_fmt: Text = ...
133+
actual: datetime.datetime | None, msg_fmt: str = ...
141134
) -> None: ...
142135
def assert_datetime_about_now_utc(
143-
actual: Optional[datetime.datetime], msg_fmt: Text = ...
136+
actual: datetime.datetime | None, msg_fmt: str = ...
144137
) -> None: ...
145138
def assert_raises(
146-
exception: Type[BaseException], msg_fmt: Text = ...
139+
exception: type[BaseException], msg_fmt: str = ...
147140
) -> AssertRaisesContext: ...
148141
def assert_raises_regex(
149-
exception: Type[BaseException],
150-
regex: Union[Text, Pattern[Text]],
151-
msg_fmt: Text = ...,
142+
exception: type[BaseException],
143+
regex: str | Pattern[str],
144+
msg_fmt: str = ...,
152145
) -> AssertRaisesContext: ...
153146
def assert_raises_errno(
154-
exception: Type[BaseException], errno: int, msg_fmt: Text = ...
147+
exception: type[BaseException], errno: int, msg_fmt: str = ...
155148
) -> AssertRaisesContext: ...
156149
def assert_succeeds(
157-
exception: Type[BaseException], msg_fmt: Text = ...
150+
exception: type[BaseException], msg_fmt: str = ...
158151
) -> ContextManager: ...
159152
def assert_warns(
160-
warning_type: Type[Warning], msg_fmt: Text = ...
153+
warning_type: type[Warning], msg_fmt: str = ...
161154
) -> AssertWarnsContext: ...
162155
def assert_warns_regex(
163-
warning_type: Type[Warning], regex: Text, msg_fmt: Text = ...
156+
warning_type: type[Warning], regex: str, msg_fmt: str = ...
164157
) -> AssertWarnsContext: ...
165158
def assert_json_subset(
166-
first: Union[dict, list], second: Union[dict, list, str, bytes]
159+
first: dict[str, Any] | list[Any],
160+
second: dict[str, Any] | list[Any] | str | bytes,
167161
) -> None: ...
168162

169163
class Exists:

0 commit comments

Comments
 (0)