Skip to content

Commit 73974e9

Browse files
authored
Improve warnings.catch_warnings (#8229)
1 parent f135c65 commit 73974e9

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

stdlib/warnings.pyi

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _warnings import warn as warn, warn_explicit as warn_explicit
33
from collections.abc import Sequence
44
from types import ModuleType, TracebackType
5-
from typing import Any, TextIO, overload
5+
from typing import Any, Generic, TextIO, TypeVar, overload
66
from typing_extensions import Literal, TypeAlias
77

88
__all__ = [
@@ -16,6 +16,7 @@ __all__ = [
1616
"catch_warnings",
1717
]
1818

19+
_W = TypeVar("_W", bound=list[WarningMessage] | None)
1920
_ActionKind: TypeAlias = Literal["default", "error", "ignore", "always", "module", "once"]
2021

2122
filters: Sequence[tuple[str, str | None, type[Warning], str | None, int]] # undocumented, do not mutate
@@ -56,56 +57,54 @@ class WarningMessage:
5657
source: Any | None = ...,
5758
) -> None: ...
5859

59-
class catch_warnings:
60+
class catch_warnings(Generic[_W]):
6061
if sys.version_info >= (3, 11):
6162
@overload
62-
def __new__(
63-
cls,
63+
def __init__(
64+
self: catch_warnings[None],
6465
*,
6566
record: Literal[False] = ...,
6667
module: ModuleType | None = ...,
6768
action: _ActionKind | None = ...,
6869
category: type[Warning] = ...,
6970
lineno: int = ...,
7071
append: bool = ...,
71-
) -> _catch_warnings_without_records: ...
72+
) -> None: ...
7273
@overload
73-
def __new__(
74-
cls,
74+
def __init__(
75+
self: catch_warnings[list[WarningMessage]],
7576
*,
7677
record: Literal[True],
7778
module: ModuleType | None = ...,
7879
action: _ActionKind | None = ...,
7980
category: type[Warning] = ...,
8081
lineno: int = ...,
8182
append: bool = ...,
82-
) -> _catch_warnings_with_records: ...
83+
) -> None: ...
8384
@overload
84-
def __new__(
85-
cls,
85+
def __init__(
86+
self: catch_warnings[list[WarningMessage] | None],
8687
*,
8788
record: bool,
8889
module: ModuleType | None = ...,
8990
action: _ActionKind | None = ...,
9091
category: type[Warning] = ...,
9192
lineno: int = ...,
9293
append: bool = ...,
93-
) -> catch_warnings: ...
94+
) -> None: ...
9495
else:
9596
@overload
96-
def __new__(cls, *, record: Literal[False] = ..., module: ModuleType | None = ...) -> _catch_warnings_without_records: ...
97+
def __init__(self: catch_warnings[None], *, record: Literal[False] = ..., module: ModuleType | None = ...) -> None: ...
9798
@overload
98-
def __new__(cls, *, record: Literal[True], module: ModuleType | None = ...) -> _catch_warnings_with_records: ...
99+
def __init__(
100+
self: catch_warnings[list[WarningMessage]], *, record: Literal[True], module: ModuleType | None = ...
101+
) -> None: ...
99102
@overload
100-
def __new__(cls, *, record: bool, module: ModuleType | None = ...) -> catch_warnings: ...
103+
def __init__(
104+
self: catch_warnings[list[WarningMessage] | None], *, record: bool, module: ModuleType | None = ...
105+
) -> None: ...
101106

102-
def __enter__(self) -> list[WarningMessage] | None: ...
107+
def __enter__(self) -> _W: ...
103108
def __exit__(
104109
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
105110
) -> None: ...
106-
107-
class _catch_warnings_without_records(catch_warnings):
108-
def __enter__(self) -> None: ...
109-
110-
class _catch_warnings_with_records(catch_warnings):
111-
def __enter__(self) -> list[WarningMessage]: ...

tests/stubtest_allowlists/py3_common.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ typing.type_check_only # typing decorator that is not available at runtime
216216
unittest.mock.patch # It's a complicated overload and I haven't been able to figure out why stubtest doesn't like it
217217
urllib.parse._DefragResultBase.__new__ # Generic NamedTuple is problematic in mypy, so regular tuple was used. See https://github.com/python/mypy/issues/685
218218
urllib.request.HTTPPasswordMgrWithPriorAuth.__init__ # Args are passed as is to super, so super args are specified
219-
warnings.catch_warnings.__init__ # Defining this ruins the __new__ overrides
220219
weakref.CallableProxyType.__getattr__ # Should have all attributes of proxy
221220
weakref.ProxyType.__getattr__ # Should have all attributes of proxy
222221
weakref.ReferenceType.* # Alias for _weakref.ReferenceType, problems should be fixed there

0 commit comments

Comments
 (0)