Skip to content

Commit c8bdfcb

Browse files
authored
covariant warnings.catch_warnings type-parameter with default (#13675)
1 parent e18b99c commit c8bdfcb

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

stdlib/warnings.pyi

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import sys
33
from _warnings import warn as warn, warn_explicit as warn_explicit
44
from collections.abc import Sequence
55
from types import ModuleType, TracebackType
6-
from typing import Any, Generic, Literal, TextIO, TypeVar, overload
7-
from typing_extensions import LiteralString, TypeAlias
6+
from typing import Any, Generic, Literal, TextIO, overload
7+
from typing_extensions import LiteralString, TypeAlias, TypeVar
88

99
__all__ = [
1010
"warn",
@@ -21,7 +21,8 @@ if sys.version_info >= (3, 13):
2121
__all__ += ["deprecated"]
2222

2323
_T = TypeVar("_T")
24-
_W = TypeVar("_W", bound=list[WarningMessage] | None)
24+
_W_co = TypeVar("_W_co", bound=list[WarningMessage] | None, default=list[WarningMessage] | None, covariant=True)
25+
2526
if sys.version_info >= (3, 14):
2627
_ActionKind: TypeAlias = Literal["default", "error", "ignore", "always", "module", "once"]
2728
else:
@@ -66,7 +67,7 @@ class WarningMessage:
6667
source: Any | None = None,
6768
) -> None: ...
6869

69-
class catch_warnings(Generic[_W]):
70+
class catch_warnings(Generic[_W_co]):
7071
if sys.version_info >= (3, 11):
7172
@overload
7273
def __init__(
@@ -92,7 +93,7 @@ class catch_warnings(Generic[_W]):
9293
) -> None: ...
9394
@overload
9495
def __init__(
95-
self: catch_warnings[list[WarningMessage] | None],
96+
self,
9697
*,
9798
record: bool,
9899
module: ModuleType | None = None,
@@ -109,11 +110,9 @@ class catch_warnings(Generic[_W]):
109110
self: catch_warnings[list[WarningMessage]], *, record: Literal[True], module: ModuleType | None = None
110111
) -> None: ...
111112
@overload
112-
def __init__(
113-
self: catch_warnings[list[WarningMessage] | None], *, record: bool, module: ModuleType | None = None
114-
) -> None: ...
113+
def __init__(self, *, record: bool, module: ModuleType | None = None) -> None: ...
115114

116-
def __enter__(self) -> _W: ...
115+
def __enter__(self) -> _W_co: ...
117116
def __exit__(
118117
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
119118
) -> None: ...

0 commit comments

Comments
 (0)